Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Changing a textbox on form1 from form2

I have a text box control  on form1 that I would like to change from form2. I have the text box modifier property on form1 set to public.  I then have a button on form2 to change the text of the text box on form1. I can not get it to work. So the question is this; How do I access the the controls on other forms.

Stan Anderson
Saturday, December 11, 2004

You need a handle to form1 included in form2.  That means the current, running form, not instantiated a new one, so set the handle in form two from form1 before opening form 2.  The syntax for that will vary depending on which specific .net language you are using, but that's the general idea.

Joel Coehoorn
Sunday, December 12, 2004

I think to get a better answer you're going to need to tell us what language you're working in and just how these two forms get on the screen; does one open the other, or does a third form open both of them, or what?  And of course that will vary depending whether you're using VB .NET or C# or VB6 or Access VBA.

Kyralessa
Sunday, December 12, 2004

One way of thinking about it is to forget they are forms and just pretend they are objects. If you had Foo class, and you wanted Bar class to be able to change a property of an instance you created, it could only do it if a) it had a reference to it or b) they were both public in the same class.

How to get a reference to the form depends on the way you are opening the forms. If Form1 is opening Form2, and always will, just modify Form2's constructor to accept a reference to a form. Then you can access any public methods from that object in Form2.

class Form2
{
  private Form1 form1;
  public Form2(Form1 form1)
  {
    this.form1 = form1;
  }

  private ChangeText_OnClick(object sender, EventArgs e)
  {
    form1.MyTextbox.Text = this.OtherTextbox.Text;
  }
}

(You'll have to forgive the event handler)

Ultimately you are creating a new type when you create Form1 and Form2, they just happen to show up as Winforms when you instatiate them.

Cory Foy (cornetdesign.com)
Sunday, December 12, 2004

By e-mail Stan said:

"I am using VB.NET and form1 opens form2. So I would like to access the controls on form1 from form2."

Another way to do this is to have, in Form1 somewhere (e.g. attached to a button click):

Dim f As New Form2()
f.ShowDialog(Me)

And in Form2 under the click event for its button:

CType(Me.Owner, Form1).txtPutTextHere.Text = "Hello."

The Owner property just gives you a generic form, so you have to cast it to a Form1 (at least if you have Option Strict On, as I always do).  The "ShowDialog(Me)" ensures that Form2's Owner is Form1.

The drawback to this method is that the user can't interact with Form1 again until Form2 is closed.  But if that satisfies your requirements...

Kyralessa
Tuesday, December 14, 2004

However, I should add that I'd probably sooner do what the other two suggest, passing a reference to Form1 into Form2 either in the constructor or in a public property.  That'll give you more flexibility than ShowDialog.

Here's another thought: If Form1 doesn't have to show the text right away, but only after you close Form2, then you can still set properties in Form1 from Form2 even after the ShowDialog statement.  After ShowDialog, Form2 will be closed, but it will still be in memory until the end of your subroutine, so you can still access stuff on it:

[from within Form1:]

Dim f As New Form2()
f.ShowDialog(Me)
Me.txtPutTextHere.Text = f.btnPutText.Text

[etc. to end of Sub]

Even if you change things on Form2 from their default values, they'll persist until the reference to f goes out of scope, at the end of the Sub, so you can use them to fill Form1.  If, again, that meets your requirements.

Kyralessa
Tuesday, December 14, 2004

*  Recent Topics

*  Fog Creek Home