object.TopLevel control

  • Thread starter Thread starter Levis
  • Start date Start date
L

Levis

Hi,
I inherited Button and I created my component (MyButton)
In this button onclick event I want show this objects parent form. When my
form is not MDIChild this codes run correct. But when my form is MDIChild
this codes show MDIParents name.
How can I show parent form name (not MDIParent)

public class MyButton: System.Windows.Forms.Button{
{
....
protected override void OnClick(EventArgs e)
{
if (this.TopLevelControl is Form)
{
Form Form_ = (Form)this.TopLevelControl;
MessageBox.Show(Form_.Name);
}
base.OnClcik(e;)
}
....
}
 
The TopLevelControl property for controls on MDI childs will always be
the MDI Parent form. You can iterate through the parents of your control
and return the first parent that is a Form.

protected override void OnClick(EventArgs e)
{
Control parent = this.Parent;
while( !(parent is Form) && (parent != null))
parent = parent.Parent;

if(parent != null)
{
MessaegBox.Show(parent.Name);
}

base.OnClcik(e;)
}

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
Hi Levis,

Try calling button's FindForm method.
If you read in the docs about TopLevelControl you'll find out that what
you've got with this property is exactly what it supposed to return
 
Back
Top