Displaying a second user control in a form
-
I'm having a problem making a User Control display upon a form in C#. My form starts off with a single UC on it. Its called UCInit. UCInit contains some text and a couple of buttons. When one of the buttons is clicked, I want UCInit to remove itself from the form, and a second User Control, called UCCreate, to appear. I can get rid of the first UserControl, but UCCreate never appears. This is my code so far for when the button in UCInit is clicks: private void btnCreate_Click(object sender, System.EventArgs e) { this.Hide(); UCCreate newPanel = new UCCreate(); newPanel.Location = new Point (376, 8); newPanel.Show(); } UCInit removes itself but the instance of UCCreate never shows itself. What am I doing wrong?
-
I'm having a problem making a User Control display upon a form in C#. My form starts off with a single UC on it. Its called UCInit. UCInit contains some text and a couple of buttons. When one of the buttons is clicked, I want UCInit to remove itself from the form, and a second User Control, called UCCreate, to appear. I can get rid of the first UserControl, but UCCreate never appears. This is my code so far for when the button in UCInit is clicks: private void btnCreate_Click(object sender, System.EventArgs e) { this.Hide(); UCCreate newPanel = new UCCreate(); newPanel.Location = new Point (376, 8); newPanel.Show(); } UCInit removes itself but the instance of UCCreate never shows itself. What am I doing wrong?
-
You also need to do:
newPanel.Size=[whatever-the-size-is];
this.Controls.Add(newPanel);"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma GandhiStill no love. This is my amended code: private void btnCreate_Click(object sender, System.EventArgs e) { this.Hide(); UCCreate newPanel = new UCCreate(); newPanel.Location = new Point(376, 8); newPanel.Size = new Size (280, 140); this.Controls.Add(newPanel); newPanel.Show(); }
-
Still no love. This is my amended code: private void btnCreate_Click(object sender, System.EventArgs e) { this.Hide(); UCCreate newPanel = new UCCreate(); newPanel.Location = new Point(376, 8); newPanel.Size = new Size (280, 140); this.Controls.Add(newPanel); newPanel.Show(); }
You're adding UCCreate to the control you are hiding, you need to add it to the controls collection of the form instead.
this.Parent.Controls.Add(newPanel);
should give you what you want. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him -
You're adding UCCreate to the control you are hiding, you need to add it to the controls collection of the form instead.
this.Parent.Controls.Add(newPanel);
should give you what you want. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him