5
votes

I have a windows form and i dont want to make any other windows forms just one windows form and different user controls how can i change between user controls for example hide one and show the other user control programmatically ?


private void Btt_info_Click(object sender, EventArgs e)
{
    Frm_Main frm_main = new Frm_Main();
    frm_main.Controls["panel1"].Controls.Clear();
    UC_Info uc_info = new UC_Info();
    frm_main.Controls["panel1"].Controls.Add(uc_info);
}

i added this but it doesnt work

4
I cant pile all the user controls on each other then use visibleDeadlyDagger
i want sth to load and unload themDeadlyDagger

4 Answers

13
votes

Add a container control (if I remember correctly, there's a containers section in the toolbox?), like a panel. Create usercontrols for what you want to dynamically switch around. So make like a 'HomePage' usercontrol and a 'LoginPage' usercontrol. Dynamically add the usercontrol that you want to display to the container. WHen you want, remove it from the container and add a different usercontrol:

Panel myPanel = new Panel();
LoginPage ctlLoginPage = new LoginPage();
HomePage ctlHomePage = new HomePage();

//add the loginpage to the panel first
myPanel.Controls.Add(ctlLoginPage);

...do stuff...

//remove whatever control is currently in the Panel
myPanel.Controls.Clear();
//add the other control, the HomePage control instead now
myPanel.Controls.Add(ctlHomePage);

..do other stuff...

I usually do it this way so you leave your form itself open to add common controls and stuff that might be shared between your different 'pages'.

EDIT: Note that I normally would add the panel in the designer and not create it dynamically in the code. This was just an example.

EDIT: The interaction between your mainform and usercontrols can be handled in a few different ways, and I am not saying that any of these is the correct method.

  • You create a static property for your Panel on the Mainform, so that you can always access it to swap your controls around.

In this example I'll also add a static method for it

enum PanelControlsEnum {HomePage, LoginPage};
public static Panel MyContainerPanel {get;set;}
public static void SwitchPanelControls(PanelControlsEnum selControl){
  ..put your switch panels code here..
}

Then inside your usercontrol you call a predefined method, something like:

MainForm.SwitchPanelControls(PanelControlsEnum.HomePage);
  • Another method is to bind the button click event on your mainform instead of inside the form.

Like This:

HomePage ctlHomePage = new HomePage();
ctlHomePage.Click += MyClickEvent;
myPanel.Controls.Add(ctlHomePage)

...

private void MyClickEvent(object sender, RoutedEventArgs e)
{
  ..switch user control code here...
}
2
votes

Create a method that returns a UserControl object. Then put conditions in that method as to which control you want to load at a specific condition and then in your main form code.

UserControl control = GetControlFromMyMethod();
form1.Controls.Add(control);

where 'control' is the returned control from your method.

To remove the existing one you have to loop through the form1.Controls and find out the control and call 'Remove'.

Update: Mike C has a better idea of adding a panel and loading your desired control on the panel as then it's easy to remove your control and you then don't have to loop through the forms controls to find it and then remove it.

2
votes

Try this:

this.Controls.Clear();
usercontrol load = new usercontrol ();
this.Controls.Add(load);
load.Show();
0
votes

you could try this it will definitely help you as it did helped me a lot it short and straight to the point hope that will help