Controling Multiple Windows Forms

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

Situation:
I have a drop down list on a main form in my program. I have another form
for entering a new item that will reside in the drop down list of the main
form. When the Add item button is clicked on the second form, the second
form closes, but I also want the main form to rebind the data in the drop
down list.

I'm not sure how to tell the main form to rebind the data from the
form2_Closed event.

When I google this problem I get a ton of info on MDI, but that is not the
situation I'm looking for.

I've also looked into form1.FromHandle(form2) but I don't know how to get
the handle of a form, or even if this is the appropriate way of going about
this.

Thanks in advance,

- Paul
 
I might need to rephrase this...

I would like to access functions I've created in a parent form from a child
form (not MDI parent/child, but form1 instantiates a new form2).

Thanks,

- Paul
 
I found a way around this problem... On form1, I call the logic I want to
occur when form2 or 3 or 4 closes when form1_Activated occurs.

Still curious about how to do this though...

Thanks
 
Greetings Paul,

Question: Is the Form2 shown as Modal? If so you can use the
DialogResult to check if the user closed the dialog in a workable way.
Just make suere that when using Modal, add Me.Hide() on the OnClose
event.

Regards,

Paulo
 
Hi Paul,

Simply, each form has a property called "Owner", from the main form and just
before showing the child form, set the child form "Owner" property to "this"
(C#) or "Me" (VB.NET).

From inside MainForm code, do this:

C#
----
ChildForm cf = new ChildForm();
cf.Owner = this;

VB.NET
-------
dim cf as new ChildForm()
cf.Owner = Me

After that, at any time you want to reach a method in main form inside
ChildForm, just call the Owner property of the child and cast its refrence to
MainForm and call the method like this:

((MainForm)this.Owner).MethodName();

or use Me and CType for VB.NET

The logic is calling the Owner property from inside child code, and you
original set its reference to MainForm from MainForm it self.

Good Luck, hope that work with you
 
Thanks Ahmed... this worked great!

(FYI for others who come across this article: I also had to make the method
 
Back
Top