c# Changing Textbox.Text from different class

This is a discussion on c# Changing Textbox.Text from different class within the C# Programming forums, part of the General Programming Boards category; I have the function AddToText which is located in the Form1 Partial Class Code: public void AddToText(string text) { this.txtStatus.Text ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    23

    Question c# Changing Textbox.Text from different class

    I have the function AddToText which is located in the Form1 Partial Class

    Code:
    public void AddToText(string text)
    {
    this.txtStatus.Text = this.txtStatus.Text + "\r\n";
    }
    I want to call this from a different class so I do:
    Code:
    Form1 frm = new Form1();
    frm.AddToText("Test");
    I click a button called start and it calls the procedure on a different class and runs the code above. But nothing appears in my textbox. It works when I call it from the Partial Class Though.

    WTF?

  2. #2
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    hmmm maybe you should look over your AddToText function again, i dont see where you're using that text paramater.
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    oh my bad, Its in there on my actually code though and its not working. I read something about Form1 frm = new Form1(); creates a new form and the old one isnt motified or something, dunno if that is relevant.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Also I just added a MessageBox.Show(txtStatus.Text);

    and it displayed my status so something is in the textbox, but I cant see it...

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Okay so I figured it out. I typed frm.Show() after I initiate Form1 frm = new Form1 how do I do that, like make reference to the form without actually creating a new one??

  6. #6
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    sounds like you're referring to the wrong instance of form1, if you're using vc# 2005-2008 you will see the program starts the form like this..

    Code:
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
    you need to referrence to that form1, one way of doing this which isn't the best idea but will be suitable for your case, is by creating a static object for the form within the Program class by doing this.

    Code:
            static Form1 mainForm;
            [STAThread]
            static void Main()
            {
                
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                
                mainForm = new Form1();
    
                Application.Run(mainForm);
                
            }
    then you can change your textbox by going Program.mainForm.textBox.Text = "something";
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    You will need to pass it in to the form or class you want to modify it in.
    Code:
    //In a class or another form besides Form1
    public Form1 Opener
    {
        set{this.mOpener = value;}
    }
    Woop?

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Quote Originally Posted by prog-bman View Post
    You will need to pass it in to the form or class you want to modify it in.
    Code:
    //In a class or another form besides Form1
    public Form1 Opener
    {
        set{this.mOpener = value;}
    }
    I dont understand that. I tried the post above but mainForm doesn't show up when I type Program.________ it should appear there but it doesn't.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Nevermind I got it, I had to add a public beside the static Main();. Also What is the downside of doing this? as you stated above you said its not the best idea.

  10. #10
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    It's just not good practice to make objects static, but it's about all you can do in this case.
    Good Help Source all round
    Good help for win programmers
    you will probably find something in here
    this thing also helps

    if you have never tried any of the above then maybe you should, they help alot

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Form1 has a button that opens Form2
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ExampleFormOpener
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //
                //Create the second form and set the opener to this instance.
                //
                Form2 mySecondForm = new Form2();
                mySecondForm.Opener = this;
    
                mySecondForm.Show();
            }
    
            public void SetText(string customText)
            {
                this.textBox1.Text = customText;
            }
        }
    }
    Form2 has a button that sets Form1's text field
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ExampleFormOpener
    {
        public partial class Form2 : Form
        {
            private Form1 mOpener = null;
    
            public Form2()
            {
                InitializeComponent();
            }
    
            public Form1 Opener
            {
                set { this.mOpener = value; }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (this.mOpener != null)
                {
                    this.mOpener.SetText("Text set from another form!");
                }//if
            }
        }
    }
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  2. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  3. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  4. Changing a derived class to a base class?
    By roktsyntst in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2002, 08:40 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21