Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows form C# change user control by code

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

like image 752
DeadlyDagger Avatar asked Mar 04 '13 15:03

DeadlyDagger


People also ask

What is Windows Forms application in C#?

A Windows form in C# application is one that runs on the desktop of a computer. Visual Studio Form along with C# can be used to create a Windows Forms application. Controls can be added to the Windows forms C# via the Toolbox in Visual Studio. Controls such as labels, checkboxes, radio buttons, etc.


3 Answers

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...
}
like image 110
Mike C. Avatar answered Sep 30 '22 23:09

Mike C.


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.

like image 41
Azhar Khorasany Avatar answered Oct 01 '22 01:10

Azhar Khorasany


Try this:

this.Controls.Clear();
usercontrol load = new usercontrol ();
this.Controls.Add(load);
load.Show();
like image 38
user5912760 Avatar answered Sep 30 '22 23:09

user5912760