there are some buttons on the top of the winform, and when I click one of them, the panel below will load different predefined panel, how can I implement this ?
please see this example: http://img203.imageshack.us/img203/6348/32753641.png
there are some buttons on the top of the winform, and when I click one of them, the panel below will load different predefined panel, how can I implement this ?
please see this example: http://img203.imageshack.us/img203/6348/32753641.png
I don't know exactly what you're trying to do, but if you've got a Panel on your form named contentArea
and a bunch of user controls created (but not on the form), then you could use this as an event handler for a button:
public void myButton_Click(object sender, EventArgs e) {
contentArea.Controls.RemoveAt(0);
contentArea.Controls.Add(new MyUserControl());
}
...though as other people have said, a tab control would be better in this case.
What you can do is have them each in a separate Panel
. Set the Visible
property to false
to each. When the Click
event on the button, set the Visible
property of all of them to false
and set the one you want shown's Visible
to true
.
Here's a solution using a standard WinForms TabControl, where the Tabs are hidden at run-time, but of course they are available at design-time.
Assumptions :
You don't want to get into creating OwnerDrawn Tabs, which is possible.
A standard WinForms TabControl will meet all your design-time needs.
Code :
In the Form Load event of the Form that hosts your TabControl use code like this :
tabControl1.Region = new Region(tabControl1.DisplayRectangle);
To hide the Tabs.
Then, "wire" up your buttons to handle selecting the different TabPages in the TabControl. Obviously you could do this in a more elegant way than this :
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages[0];
}
private void button2_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages[1];
}
Note : if you want to insert secondary Forms or UserControls into the TabPages of a TabControl : that's not a problem : of course simpler to use UserControls. Insert them into the Controls collection of each TabPage and set their 'Dock Property to 'DockStyle.Fill.
Note : there are fancier ways you can hide the Tabs, like using a derived TabControl as shown here on CodeProject : TabControl on a WinForm without showing the Tab header? There are other solutions out there that use a modified WndProc. They're not hard to find.