Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a user control and in that user control i have a button if button is click it load another user control but i do not know how to do this.

What I have tried:

public partial class ManageTenant : UserControl
{
public ManageTenant()
{

InitializeComponent();

}
private void btnAddNew_Click(object sender, EventArgs e)
{
// this is my second User Control Name is TenantSetup
var control = new TenantSetup();

}

}
Posted
Updated 4-Sep-16 4:42am
Comments
Karthik_Mahalingam 5-Sep-16 5:15am    
check this https://www.youtube.com/watch?v=mECkft9LG4k

Exactly what you want to do depends on where the new usercontrol is supposed to go: if it's inside your existing control, then it's no problem:
C#
TenantSetup ts = new TenantSetup();
ts.Location = new Point(100,100);
Controls.Add(ts);
If you want it added to the form that contains the original UserControl, then that's a little more complicated, because you need to ask teh for to do that for you. Have a look here: Transferring information between two forms, Part 2: Child to Parent[^] - it's forms based, but if you mentaly replace "child form" with "my UserControl" it's exactly the same procedure, and the same code as well.
 
Share this answer
 
Whenever you create an instance of a UserControl in code at run-time using 'new, you then need to add that UserControl to some other object that has a Controls collection. That could be a Form, a Panel, or another UserControl, or the UserControl could be inserted into a DataGridView, a TableLayoutPanel, etc. as an element. After you add the newly created instance of the UserControl, you will need to set its location in the hosting Form/Control.

You create instances of your Project's UserControls at design-time by drag-dropping their ToolBox icon onto a Form, or onto another Control on the Form that allows hosting other Controls; the drag-drop invokes the Load event of the UserControl in which you can establish whatever communication with the hosting Form/Control you need, and the dropped UserControl is automatically added to the hosting control's ControlCollection.

In either case you need have the UserControl "publish" ... provide a means of access to ... data, or events, that you will want the hosting Form/Control to use. This can be done in many ways, by public Fields, Properties, defining public Events, etc.

If you clarify your question to more clearly explain how the use of the two UserControls is managed, I think you'll get more specific feedback. Describe the Form you are using, and when/how the UserControls are shown, and used.
 
Share this answer
 
Comments
Xen Cloete 15-Nov-19 0:50am    
Show by way of example. Explaining in this way has no value at all.

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