0

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

Karam Ramadan
  • 29
  • 3
  • 8

1 Answers1

0

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.

C Sharp Conner
  • 378
  • 2
  • 11
  • I made a panel and then i called a Form into this panel . this works great .. but i want to call another form to replace this form .. this is the code .. `panel.Controls.Clear(); Form Example = new Form (); Example.TopLevel = false; panel.Controls.Add(Example); Example.Show(); ` ....................... this was my code on both forms .. but it didnot work moving from the first form to another form . – Karam Ramadan Jul 04 '18 at 18:03
  • If you have 2 different forms, you will need "Form Example = new Form ();" and "OtherForm Example = new OtherForm();" to load both, otherwise you are just loading one particular form. – C Sharp Conner Jul 04 '18 at 20:16