Referring to main form from nested controls?

  • Thread starter Thread starter Brian Link
  • Start date Start date
B

Brian Link

The design of my app is such that I have a main form which loads a
variety of user controls. Sometimes these controls will contain their
own constituent sub-controls.

The main form should respond to changes in the constituent controls,
either by showing/hiding menus, or updating status bar text.

Currently I'm rippling up events through the usercontrols back to the
main form. This is proving more and more cumbersome. In VB6, I could
refer to another form directly, but since the main form is
instantiated under vb.net it's not necessarily visible to the
sub-controls in any obvious way.

Does anyone have a strategy for calling a procedure in another form?
Where is the reference to my main form? I'd like to be able to either
call a global routine which fires off a routine in the main form, or
refer to the main form directly.

Thanks

Brian Link, Minnesota Countertenor
----------------------------------
"There are things that we know, and then there are known
unknowns. That is to say, there are things that we now
know that we don't know. But there are also unknown unknowns.
There are things we do not know we don't know."
- U.S. War Secretary, 2003
 
* Brian Link said:
The design of my app is such that I have a main form which loads a
variety of user controls. Sometimes these controls will contain their
own constituent sub-controls.

The main form should respond to changes in the constituent controls,
either by showing/hiding menus, or updating status bar text.

Currently I'm rippling up events through the usercontrols back to the
main form. This is proving more and more cumbersome. In VB6, I could
refer to another form directly, but since the main form is
instantiated under vb.net it's not necessarily visible to the
sub-controls in any obvious way.

You can call the control's 'FindForm' method.
 
You can call the control's 'FindForm' method.

Hm. That would require that the control knew how nested it was. This
is not always the case - sometimes the control is seated on the form,
sometimes on another control on the form, sometimes on a dialog.

Plus then you start getting into the
MyControl.FindForm.Findorm.FindForm.FindForm.MySub() type syntax.

Doesn't the Application keep a reference to the main form instance?

Brian Link, Minnesota Countertenor
----------------------------------
"There are things that we know, and then there are known
unknowns. That is to say, there are things that we now
know that we don't know. But there are also unknown unknowns.
There are things we do not know we don't know."
- U.S. War Secretary, 2003
 
Hi,

Thanks for posting in the community.

I think we can use the FindForm to retrieve the form where the control on
even if the control is on another control.
e.g. button1 on tabpage, tabpage on usercontrol, and usercontrol on form.
the button1.findform will return the form.

From the MSDN,
Retrieves the form that the control is on.
Control.FindForm Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformscontrolclassfindformtopic.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi,

Thanks for posting in the community.

I think we can use the FindForm to retrieve the form where the control on
even if the control is on another control.
e.g. button1 on tabpage, tabpage on usercontrol, and usercontrol on form.
the button1.findform will return the form.

From the MSDN,
Retrieves the form that the control is on.
Control.FindForm Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformscontrolclassfindformtopic.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Eureka!

That's it. Thanks a lot!

Brian Link, Minnesota Countertenor
----------------------------------
"There are things that we know, and then there are known
unknowns. That is to say, there are things that we now
know that we don't know. But there are also unknown unknowns.
There are things we do not know we don't know."
- U.S. War Secretary, 2003
 
* (e-mail address removed) (Peter Huang) scripsit:
I think we can use the FindForm to retrieve the form where the control on
even if the control is on another control.
e.g. button1 on tabpage, tabpage on usercontrol, and usercontrol on form.
the button1.findform will return the form.

Little addition:

This information can be easily found by typing 'FindForm' in the text
editor of VS.NET, setting the caret on it and pressing the F1 key.
 
Back
Top