user2129872
user2129872

Reputation: 41

Passing values between a class and a form

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;

Upvotes: 4

Views: 13147

Answers (2)

NDJ
NDJ

Reputation: 5194

By default a textbox is private when you place it via the designer, which means you can't access textBox1 from outside of Form2 (it's only 'visible' to code within the Form2 class). You could change the textbox to be internal or public (which would let you do the following (from Form1):

f2.tetBox1.Text = "Some text";

BUT that would be exposing parts of Form2 which only Form2 should really know about...so it isn't the cleanest solution (though it would be likely the quickest))

A better solution might be to create a method or property (internal or public) in Form2 which will set the text value. E.g.:

internal  void SetText(string value)
{
   textBox1.Text = value;
}

As the method (or property) resides in Form2, it has access to the text box, so it can set the value while textBox1 is still private.

It's safer exposing this method to 'external' code (as opposed to making the textbox visible to them) as all they can do is change the text - whereas exposing the whole textbox might let callers try to change background colour, etc.

Upvotes: 0

Blachshma
Blachshma

Reputation: 17385

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();

Upvotes: 3

Related Questions