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

Can I load another User Control in my Windows Form application by pressing an button in my initial control? For example, I have a Windows Login Form when I click the Sign In button the form must be cleared and the other User Control must be loaded in my Windows Form.

Thanks in advance!
Posted
Updated 7-Apr-11 5:47am
v2

yeah! you can because Each control that is located in the Toolbox is a member of the Control class that is part of the System.Windows.Forms namespace. Each of these controls is a class, so therefore you can dynamically create controls in code at runtime
Adding a Control to a Form at run time uses a similar method to adding a Control to a Container at run time.
Step1:- create the control that you wish add to the Form.
Step2:- initialize the control
Step3:- add the control to the Form.
this.Controls.Add(Object of Control);

Example:-
Button obj = new Button();
obj.Text = "New Control";
this.Controls.Add(obj);
 
Share this answer
 
v3
The best way (I think) is to have one Form and place a Panel that fills the form.
Create n x 1 UserControl and swap the Panel with new UserConrol as needed.

In your UserControl, design and create all you "Forms" with all necessary code to make the UserControl functional.

In the Form.cs

this.panel.Controls.Clear();
this.panel.Controls.Add(new UserControl_Login());


And when you need to replace the panel.

this.panel.Controls.Clear();
this.panel.Controls.Add(new UserControl_ShowAllRecords());
 
Share this answer
 
Comments
Member 7762422 7-Apr-11 14:58pm    
Thanks to all !! Kim your solution is working in my situation, many thanks :-)
Kim Togo 8-Apr-11 1:58am    
Good to know :-)
Yes you can. Your next step is to look around for how to do such a thing (for instance, you can look at the designer file for your form to see how it's initially constructed) and then come back with specific questions if you run into trouble implementing what you want.
 
Share this answer
 
Yes its possible. Essentially what you are doing is creating the user control via code (rather than dragging and dropping from the toolbox).
 
Share this answer
 
ITS SIMPLE
panel_load_usrctrl.Controls.Clear();<br />
User_Control UsrCtrl=new User_Control();<br />
panel_load_usrctrl.Controls.Add(UsrCtrl);
 
Share this answer
 
v2

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