Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

i have a main window and i use user control such as a form that open inside window,

i have close button inside user control, how can i remove or clear opened user control and open another user control?

What I have tried:

private void btnCancel_Click(object sender, RoutedEventArgs e)

((Panel)this.Parent).Children.Remove(this); // remove opened user control

winMain w = new winMain();
OpenUserControls.OpenUCMain(w.MainContent, new ucMain()); // dont open another user control

// my method for open user control (ucMain)

public static void OpenUCMain(Grid grd, UserControl uc)
if (grd.Children.Count == 0)
Updated 6-Apr-17 14:08pm

1 solution

If the control you want to remove is not a child on the UserControl that wants it removed, it should NOT be doing it.

What you should be doing is exposing an event in your UserControl that requests the other control to be removed. This event is subscribed to by the parent form. The parent form would then decide if it's appropriate and safe to remove the other control and then do it.

The owner (parent) of a control should be handling the child control, not left up to another child control.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900