Click here to Skip to main content
16,004,974 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I just want to open a Usercontrol (which contains buttons and textboxes) in a windows form when I click a button.

I created form1 with button1 and botton2 and also created Usercontrol1 and Usercontrol2 . now in form1 I want to call Usercontrol1 (open it in form1) when button1 is pressed and then press button2 to show Usercontrol2 and hide Usercontrol1 but I dont know how to do it. I created an object for Usercontrol1 in form1 (Usercontrol1.visible = true) but it didnt work.
please help.
Thank you
Posted

Try:
C#
MyUserControl1Name.Visible = true;
MyUserControl2Name.Visible = false;
To show 1 and hide 2, and:
C#
MyUserControl1Name.Visible = false;
MyUserControl2Name.Visible = true;
To hide 1 and show 2.
The two names must be those you gave to the control instances that you dropped on the form in the designer - so if your control classes are UserControlA and UserControlB, the instances are probably called userControlA1 and userControlB1 (but change them to reflect the use, you should never leave controls with the default names that VS assigns - in the same way that a Button shouldn't be "button1" but "butShowControlA").

You probably need to set both of the controls Visible property to false in the designer to start with.
 
Share this answer
 
Comments
naouf10 9-Nov-15 14:45pm    
Hi OriginalGriff. I already tried your code as i said in my question but still didnt work. any idea. thank you.
OriginalGriff 9-Nov-15 14:49pm    
"Didn't work" is a useless error report!

What did it do that you didn't expect, or not do that you did?
Any error messages?

Remember, I can't see your screen, access your HDD, or read your mind...:laugh:

Edit: tablet autocorrect!
naouf10 9-Nov-15 15:03pm    
we are discussing programming language issue here NOT English language errors....so if you can't help don't make jokes......
OriginalGriff 9-Nov-15 15:45pm    
No joke intended: I entered that on my tablet and it interpreted "did" as "idiot". It also interprets "code" as "coffee" 9 times of of 10, but I'm used to that one...
If I caused offence, I can only apologise.
naouf10 9-Nov-15 16:03pm    
that is fine...
Run-Time creation and siting of UserControls

1. key elements

a. create the instance:

UserControl1 myUC1 = new UserControl1();

b. add the instance to a Form or other ContainerControl's ControlCollection:

form1.Controls.Add(myUC1);

c. set the location and/or z-order of the added UserControl:

myUC1.Location = new Point(200,200);<br />
myUC1.BringToFront(); // make sure it's in front


If you really want to destroy/clear an existing UserControl to make sure each time you create the UserControl at run-time you have a "fresh" instance:
C#
UserControl1 myUC1;

private void button1_Click(object sender, EventArgs e)
{
    if(myUC1 != null) myUC1.Dispose();
    if(myUC2 != null) myUC2.Dispose();

    myUC1 = new UserControl1();
    Controls.Add(myUC1);
    myUC1.BringToFront();
}

UserControl2 myUC2;

private void button2_Click(object sender, EventArgs e)
{
    if(myUC1 != null) myUC1.Dispose();
    if(myUC2 != null) myUC2.Dispose();

    myUC2 = new UserControl2();
    Controls.Add(myUC2);
    
    myUC2.BringToFront();
        }
Re-using Design-Time sited UserControls

I suggest you create both UserControls at design-time by drag-dropping them from the ToolBox onto the Form you wish them to appear on. You will need to compile your project once before they will appear in the ToolBox.

Position them where you want them to appear at run-time.

Then, for both UserControls, set their 'Visible property to 'false in their Properties Window. That way, initially they will not be visible.

So: in your code for the Buttons Click Event:
C#
// assuming you have userControl11, and userControl21 instances:

// code in the Form that hosts the instances of the UserControls

private void btnShowUC1_Click(object sender, EventArgs e)
{
     // hide userControl21 ?
     userControl21.Hide();

     userControl11.Show();
     userControl11.BringToFront();
}

private void btnShowUC2_Click(object sender, EventArgs e)
{

     // hide userControl11
     userControl11.Hide();

     userControl21.Show();
     userControl21.BringToFront();
}
The only "downside" I can see to re-using design-time created UserControls like this is when you need to clear any fields, or any user data entered into Controls in the UserControls , before you re-use them.

For example, if you had a UserControl with a TextBox, and a CheckBox (with the CheckBox 'ThreeState property set to 'true), you could call a method like this from the Form where the UserControl is sited:
C#
// code in the UserControl
public void Clear()
{
    textBox1.Clear();
    checkBox1.CheckState = CheckState.Indeterminate;
}
Or, you could define an EventHandler for the 'VisibleChanged event of the UserControls, and, when the UserControl became visible, then clear field, re-set Controls, etc.

Since what one may wish to do when a Form, or UserControl is shown may vary, and may include wanting to show initial values, how you will handle fields and Controls as you re-use them will vary.

You could change the 'Clear example shown here, and, instead of clearing, reset whatever to pre-defined states, or values.
 
Share this answer
 
v6
Comments
naouf10 9-Nov-15 15:13pm    
Hi BillWoodruff. I don't want to drag the usercontrols to the form because I am planning to create 10 user controls with buttons and textboxes on them so if I drag all the 10 user controls to form1 it won't fit. so I just want to add user controls to form using code. can you please tell me how to do it? thank you
BillWoodruff 9-Nov-15 15:25pm    
Do keep in mind that even though your Form might appear "crowded" if you do drag-and-drag a lot of UserControls onto it at design-time, that by setting the UserControl's 'Visible property to 'false, you can hide them at run-time.

But, yes, there are many good reasons to create them dynamically at run-time.

I will add a paragraph on run-time creation to my answer above.
naouf10 9-Nov-15 18:41pm    
I tried your code and it worked as I wanted and you explained it well. I added the user control at run time in form 1 in the events. but one more thing please: now, userControl2.BringToFront() shows up the userControl2 and kind of hide the userControl1, do you know another way to remove the userControl1 instead of using userControl2.BringToFront()? thank you very much
BillWoodruff 9-Nov-15 20:32pm    
If you have two Controls of the same size occupying the same location, then, of course, only one of them is going to be visible. So set the 'Location property of your two UserControls so if both are visible at the same time, they do not occur in front of each other.

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