close form from its usercontrol
-
Hi All, Just another question. I have several winforms with a usercontrol on it. I re-use these usercontrols on several different forms. SO far so good, but what i would like to do is close the usercontrol AND form on which it resides from a button on the usercontrol. I have been experimenting with events but until now i cannot get it done. Any ideas? suggestions?? Best regads, marcel
Kind regards, Marcel Vreuls MarcelVreuls.com <<A good idea can change your life>>
-
Hi All, Just another question. I have several winforms with a usercontrol on it. I re-use these usercontrols on several different forms. SO far so good, but what i would like to do is close the usercontrol AND form on which it resides from a button on the usercontrol. I have been experimenting with events but until now i cannot get it done. Any ideas? suggestions?? Best regads, marcel
Kind regards, Marcel Vreuls MarcelVreuls.com <<A good idea can change your life>>
-
So using a button on the UserControl you want to close its parent form? I think it would be as simple as calling "this.Parent.Close()". I don't know how this wouldn't work since the UserControl's parent should be the form that contains it.
One small issue with this is that the parent might not be the Form, it might be, for example, a Panel that might also be a hosted in another Panel. So if this scenario is possible (now or later in the future), the OP should either keep looking for the Form as in the code below or just give the UserControl a reference to the Form during initialization or something so it closes it directly, which is better (constant time instead of linear time).
var parent = this.Parent;
while (!(parent is Form))
parent = parent.Parent;
parent.Close();Eslam Afifi
-
Hi All, Just another question. I have several winforms with a usercontrol on it. I re-use these usercontrols on several different forms. SO far so good, but what i would like to do is close the usercontrol AND form on which it resides from a button on the usercontrol. I have been experimenting with events but until now i cannot get it done. Any ideas? suggestions?? Best regads, marcel
Kind regards, Marcel Vreuls MarcelVreuls.com <<A good idea can change your life>>
as the others said, however you could take advantage of
Control.TopLevelControl
to get to the parent Form. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
as the others said, however you could take advantage of
Control.TopLevelControl
to get to the parent Form. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Hi All, Just another question. I have several winforms with a usercontrol on it. I re-use these usercontrols on several different forms. SO far so good, but what i would like to do is close the usercontrol AND form on which it resides from a button on the usercontrol. I have been experimenting with events but until now i cannot get it done. Any ideas? suggestions?? Best regads, marcel
Kind regards, Marcel Vreuls MarcelVreuls.com <<A good idea can change your life>>
Thanks Guys, With your help i have found a working solution for me. .NET 2.0 framework. The var i cannot use yet :laugh: private void butSave_Click(object sender, EventArgs e) { try { bool YesNo = xxxSaveData(); if (YesNo) { if (Parent is Form) { Form myParent = (Form)Parent; myParent.Close(); } } } catch (Exception ex) { ExceptionManager.Publish(ex); } }
Kind regards, Marcel Vreuls MarcelVreuls.com <<A good idea can change your life>>
-
One small issue with this is that the parent might not be the Form, it might be, for example, a Panel that might also be a hosted in another Panel. So if this scenario is possible (now or later in the future), the OP should either keep looking for the Form as in the code below or just give the UserControl a reference to the Form during initialization or something so it closes it directly, which is better (constant time instead of linear time).
var parent = this.Parent;
while (!(parent is Form))
parent = parent.Parent;
parent.Close();Eslam Afifi
Oh no! Don't do it like this!!! use
Control.TopLevelControl
property!!! -
Thanks Guys, With your help i have found a working solution for me. .NET 2.0 framework. The var i cannot use yet :laugh: private void butSave_Click(object sender, EventArgs e) { try { bool YesNo = xxxSaveData(); if (YesNo) { if (Parent is Form) { Form myParent = (Form)Parent; myParent.Close(); } } } catch (Exception ex) { ExceptionManager.Publish(ex); } }
Kind regards, Marcel Vreuls MarcelVreuls.com <<A good idea can change your life>>
Haven't you read the comments? Change to:
private void butSave_Click(object sender, EventArgs e)
{
try
{
bool YesNo = xxxSaveData();
if (YesNo)
{
Control ctrlSender = sender;
(ctrlSender.TopLevelControl as Form).Close();
}
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
}
}or your code depends on the control beeing placed on the form directly.
-
Oh no! Don't do it like this!!! use
Control.TopLevelControl
property!!!Yeah, Luc Pattyn's reply[^] reminded me of it. Of course a direct reference to the Form is better, that's what I said in my reply, I just forgot that the reference is already there as the TopLevelControl property provided by the framework.
Eslam Afifi