Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

controlling windows forms....

Status
Not open for further replies.

xpios

Programmer
Jan 25, 2005
4
US
Ok in VB and VB.net I know how to control other forms and communicate with them. For example, if I want to refresh a control on another form (formB) from formA I could write something like this in the code of formA:

formB.somecontrol.requery

How do you do the equivalent thing in C# windows forms? I have two forms open (formA and formB) and I want the second form (formB) to call a method of a control on formA to refresh or requery it before formB closes itself. How do you do these sorts of things in C#.

I know this may seem like a dumb question to the gurus but I'm a VB programmer trying to learn C# and translate the concepts. Thanks in advance
 
I'd like to think you can safely ask questions on this board. :) Besides, that isn't a stupid question. I'm not sure what all you've tried in C#, so here is a general tutorial.

Say you've added two forms. FormA is going to be our parent form and FormB is going to be our child. For the sake of easy demonstration, we'll say we have TextBoxA on FormA and TextBoxB on FormB and we'd like to talk back and forth between A and B using the textboxes. We'll also make the textboxes public, but you could just as easily create a method by which to set the textboxes.

Getting from A to B is easy because A creates B.
Code:
// FormA.CS:
...
FormB b = new FormB();
b.TextBoxB.Text = "This message set by FormA.";
b.Show();
...

So far so good. From an event or anywhere within FormA we have control of FormB's textbox. Now the part that might have tricked you up is getting from FormB to FormA. The answer is you don't need to. The FormB.CS file represents the class for FormB but not the instantiated object. Therefore, the communication from FormB can still just take place from FormA. Let's add an event to FormB that changes FormA's textbox.
Code:
// Also FormA.CS:
...
b.Closed += new System.EventHandler(this.FormBClosed);
...
void FormBClosed(object sender, System.EventArgs e)
  {
  TextBoxA.Text = "Form B has closed.";
  }
...

Hope that makes more sense now. Welcome to C#, I think once you get use to it, you won't want to look back.
 
See
"C# and VB.Net Conversion Pocket Reference

FROM THE PUBLISHER
Though most programmers use two or more languages, they usually have a mastery of one. Although Microsoft has advertised that the .NET runtime is language agnostic and that C# and Visual Basic .NET are so close that switching between the two is really quite easy, that's only true up to a point. Some of the differences are obvious, but others are very subtle. C# & VB.NET Conversion Pocket Reference helps you easily make the switch from one language to another. "

Compare Code
 
Ok, thanks for the input... I've also figured out another way to get what I want. I'm still going to try your idea though to see if it works. What I did was this:

in formA I open form b like this

Form2 myform = new Form2();
if (myform.DialogResult==DialogResult.ok)
{
//if i get an ok dialog result back from form2 when
//it closes refresh the listbox control on this
//main parent form
BindListBox();
}

I then set the Form2 class up to designate one of the buttons as the Accept button on the form and on form load set the buttons DialogResult property to OK. This worked well... but seems more convoluted than your way so I'll try your way also and see if it works ok.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top