简体   繁体   中英

How to find that Form is loaded or the UserControl c#

I am using a winform application c#. In the application we are using Lots of UserControls and Forms.

I have a parent form, where I am using UserControl as a MainDashBoard. But for other business requirements using Forms as well. On the userControl there is a button called LoadProperties, which will load another form displaying some properties of some object.

What I want to know is that how can I find out that currently active control is UserControl or Form. Because when I am pressing the button and saying

Form1 form = new Form1();
form.MDIParent= this;
form.show();

It is loading the form, but the this.ActiveControl is giving me the button as an active control not the form.

I want to get the currently loaded form how can I do this.

Can anybody help me out...here.

Regards Shax.

Every form has an ActiveControl, the control that will get the focus when the form is activated. To get the active form you should use the static Form.ActiveForm property.

Sorry I dont quite understand.

For finding out which MDI child is active use:

Form activeChild = this.ActiveMdiChild;

If you need to check what type of object an object is you can use:

if([variable] is Form)
{
}

if([variable] is Button)
{
}

只要保持对所有打开的表单的引用即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM