简体   繁体   中英

How to change a UserControl from another UserControl and get back to the first one ( C# )?

C# WindowsFormsApplication

So let's say we have 3 User Controls and 3 buttons . So i programed the Buttons to change the User Controls on Click_event . so what happens is that it changes the first User Control to the Second User Control and from the Second to the third . But when i go from the first one to the second one and then back from the second to the first User Control then i get an error . and that error is :

System.ArgumentException: 'A circular control reference has been made. A control cannot be owned by or parented to itself.'

and this is my code in the Buttons :

if (!this.Controls.Contains(Example.Instance))
{
    this.Controls.Add(Example.Instance);               
    Example.Instance.Dock = DockStyle.Fill;
    Example.Instance.BringToFront();
}
else
{
    Example.Instance.BringToFront();
}

and this is my code in all 3 User control :

private static Example _instance;
public static Example Instance
{
    get
    {
        if (_instance == null)
            _instance = new Example ();
        return _instance;

    }
}

And since 3 Days i did nothing but to search for a solution .. so please .. can you help me !?

And sorry for my bad Englisch . . . :-)

What is the goal you are trying to accomplish? You want to dynamically change a user control based on a button that is clicked? You could accomplish this task with loading forms into a panel and having each control on different form to load.

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