Passing values between a class and a form

12,160

Any control created by the designer (e.g. dropped from the Toolbox at design-time) is automatically set as private. Therefore, you can't access it from another form.

You don't want to start messing with the designer, instead - create a property on your Form2 which will allow you to modify the text of the Textbox from Form1.

A short example is something like this:

Form2:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }


    // When modifying the Text property it will change the text in textbox1
    public string Text
    {
        get { return textBox1.Text; }
        set { textBox1.Text = value; }
    }
}

Then, in Form1:

Form2 frm2 = new Form2();
frm2.Text = "123"; // Uses the public Text property declared in Form2
frm2.Show();
Share:
12,160

Related videos on Youtube

user2129872
Author by

user2129872

Updated on June 07, 2022

Comments

  • user2129872
    user2129872 almost 2 years

    I'm pretty new to C# - but it all went quite well - to this point.

    I start with a form and a class which does most of the work (non static and in the program part). I instantiate an object of the class in form 1 and do a log in.

    Then I switch over to the next form, form2. Actually, the the class does that. I have a method there, that contains the line:

    this.f2 = new Form2();
    

    and then:

    f2.Show();
    

    f2 is a class member of the type Form2 - and all just works fine - up to this point.

    This Form2 just consists of a big text box in which I want to display network events. The event handler works just fine - but the reference to the form just doesn't seem to work. If I do

    f2.tetBox1.Text = "Some text";
    

    it just won't change the the text in the text box.

    What am I doing wrong here?

    Here is a more detailed description of what I'm doing:

    Form1 passes some log in information to myProg, being an instance of MyClass. If the login was successful, Form1 calls myProg.makeForm();

    This is what the method in MyClass looks like:

    public void makeForm() {
                this.f2 = new Form2();
                f2.Show();
                this.sendString("start f2");
    }
    

    This is MyClass.sendString():

    private void sendString(string text) {
                SystemSounds.Beep.Play();
                this.f2.setTextBox(text);
    }
    

    This calls, as you see, setTextBox() of Form2 - which I implemented as proposed here. The strange thing is, that up to this point all works well. The Form2 gets shown an textBox1 contains "start f2" - as expected. But when an event comes in, the text in textBox1 doesn't change. the beep get played all right - so the method sendString() gets called alright.

    One thing I have observed: If the beep line is placed after the call to this.f2.setTextBox(text);, it doesn't get played if the method is called from the event handler.

    f2, btw., is a private member of MyClass:

    private Form2 f2;
    
    • Yuck
      Yuck about 11 years
      Please show some of the code - but not a whole dump of both forms' .cs files.
  • Servy
    Servy about 11 years
    Also note the OP will want to give the property a more meaningful name than just Text. First off, Control already has a Text property this would shadow, and that's...undesirable, and in any case it should just be more descriptive as to what the text represents. (Obviously only the OP knows what that is in his case.)