blah blah blah is here! blah blah » Close

up0down
link

hi, i am having problem with closing user control.
i have some function in my parent form(with panel2):
public void ActivateUserControlOne()
{
panel2.Controls.Clear();
UserControlOne = new UserControlOne();
panel2.Controls.Add(this.UserControlOne);
}
...and UserControlOne appears.
i want to close(remove) UserControlOne on Cancel()(Cancel is function in UserControlOne) and load another User Control (UserControl2) to panel2:
public void Cancel()
{
this.Parent.Controls.Remove(this);
}
any ideas how to do that?!

last answered 2 years ago

1 answers

up0down
link

I'd do both things from the parent form. The following assumes you've defined a private field called UserControl2 already:
public void ActivateUserControl2()
{
panel2.Controls.Remove(UserControlOne); // or use Clear to unload all controls
UserControl2 = new UserControl2(); // or whatever the classname is
panel2.Controls.Add(this.UserControl2);
}

up0down
link

if you want something more advanced, try making an event for this.
In the usercontrol class write this:
public delegate void UserContolClosing(UserControl uc);
public event UserContolClosing ControlClosing;
In the cancel function write this:
public void Cancel()
{
ControlClosing.Invoke(this);
}
Now in the form class add the event handler of ControlClosing and then do whatever you want with the closing control since you have its reference like remove it by this.Panel2.Controls.Remove(uc).

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback