Click here to Skip to main content
15,909,325 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,
i am new to C#, i am having a little problem in handling user controls

i did following steps:

<b>when i opened a new window project. i created two panels in it. one for buttons and other for user controls</b>

<b>added two button in panel1 and user controls named usercontrol1 & usercontrol2</b>

my idea to do this is, when i click on the button1, usercontrol1 should display on the second panel, and when i click on the button2, usercontrol2 should display on the second panel.

kindly help me handling their events.
Posted

Suggest using a TabControl[^]. It has the buttons already built-in in a row on one of its sides, usually at the top.
 
Share this answer
 
v2
Comments
BillWoodruff 26-Oct-11 10:15am    
+5 I like your idea of using a TabControl ! Why have a separate Panel with Buttons just to control which if two UserControls is visible.
Place both of your user controls inside panel2 only, with setting the visibility property to Visible="false". on the click events of your buttons, just set visibility property to true/false for the relevant user controls.

mark as answer if solves ur problem.
 
Share this answer
 
v2
Place your UserControls within the user controls panel. They don't need to sit anywhere special, just so you reach any of them in design mode in case you want to change something.

When starting your application, tell each of them to cover the whole user controls panel, but at the same time go invisible.
C#
public class Form1
{
    protected override OnLoad( object sender, EventArgs e)
    {
        foreach( Control control in userControlsPanel.Controls)
        {
            control.Dock = DockStyle.Fill;
            control.Visible = false;
        }
    }
}
Then create event handler methods for your buttons. One of them could look like this
C#
public class Form1
{
    private void Button1_Click( object sender, EventArgs e)
    {
        foreach( Control control in userControlPanel.Controls)
        {
            control.Visible = AreAssociated( Button1, control);
        }
    }
}
There are several ways how you could associate a button with one of the user controls.

You could use the button's Tag property in such a way:
C#
Button1.Tag = UserControl1;
Above code would then change to
control.Visible = ( control.Equals(Button1.Tag));
Furthermore, it would be nicer not to have one event handler per button, but to have one event handler that handles all the Click events of the similarly used buttons. You can attach one event handler so several button click events. The handler could then look like
C#
public class Form1
{
    private void AnyButton_Click( object sender, EventArgs e)
    {
        if(!(sender is Button))
        {
            throw new ArgumentException("AnyButton_Click() has to be called by a Button.");
        }


        foreach( Control control in userControlPanel.Controls)
        {
            control.Visible = control.Equals((Button)sender).Tag;
        }
    }
}
 
Share this answer
 
v2
Comments
Dalek Dave 23-Sep-11 4:23am    
Good Answer.
Just to round out the answers given here, which are all good:

If there's ever a case where you want to show both UserControls at the same time, you might consider using a SplitContainer:

You can program it so that in response to a certain mouse event (or keystroke combination), like a double-click, you can toggle it between showing one UserControl at a time, and use the same, or a different 'direct action,' to put it back in its default mode where you can have some portion of each UserControl visible depending on where the user moves the splitter.

imho this would be most suited to a scenario in which there is some interaction between the two UserControls.
 
Share this answer
 

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