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

I was trying to load a user control by clicking a button from another user control. Basically, I have 1 panel. From this panel I can click Order which will take me to a order user control. Now, in this user control I created a button which should clear the content of the panel and replace it with a new user control called checkout.

What I have tried:

I have tried:
C#
private void btnCheckout_Click(object sender, EventArgs e)
              {
              Menu panel = new Menu(); //called the Menu form to allow me to call the panel from that form.

              panel.pnlContent.Controls.Clear(); //I wanted to clear the content of a panel

              panel.pnlContent.Controls.Add(new Checkout()); //add a new user control to the panel
              }


Whenver I am clicking the button nothing happens. It do not clear the content and it do not load the checkout user control.
Posted
Updated 17-May-17 6:12am
Comments
[no name] 17-May-17 11:00am    
How do you know? Does the Controls collection contain an instance of Checkout? Did you refresh the form? Did you set the location for Checkout so that is located in a visible portion of the panel?
Member 13081540 17-May-17 11:06am    
It does contain instance of checkout and the checkout location is set in a visible portion of the panel.
[no name] 17-May-17 11:39am    
So then your code is working just like it should, not "nothing". You need to debug your code and find out why the Checkout control isn't visible if that is indeed your actual problem.

1 solution

If you debug your code, it should be perfectly obvious what the problem is:

You create a new instance of Menu, change its contents, and then throw it away.

You either need to display the new Menu instance, or get hold of an existing instance somehow.

We can't tell you how to do that, because we can't see the code that creates the existing instance, and you haven't told us what the Menu class is.

For example, if Menu is a Form, and you want to show the new instance, you need to call panel.Show(); or panel.ShowDialog(); to show it.
 
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