Karam Ramadan
Karam Ramadan

Reputation: 29

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 . . . :-)

Upvotes: 0

Views: 1358

Answers (1)

C Sharp Conner
C Sharp Conner

Reputation: 378

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.

Upvotes: 0

Related Questions