tags:

views:

601

answers:

3

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

+1  A: 

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.

icktoofay
+1. The `Control` class implements `IDisposable`. Therefore it would be better to dispose the child controls instead of just removing them. If one of the controls is using any managed/unmanaged resources those would not be released otherwise and frequent switching between the panels might lead to problems.
0xA3
if I just release them, why does the child controls don't know to dispose by themself ?
MemoryLeak
+2  A: 

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.

Daniel A. White
yeah, I also thought this idea, but with this, I can not design the layout in the designer and save the layout in seperated files.
MemoryLeak
Or use the panels of a tab controls and activate the corresponding tab in the button's click event handler. A common scenario is to store the index of the tab in the button's tag field and just use a single event handler for all buttons.
0xA3
@MemoryLeak: Why do you need to store the layout in different files? If you are going the reuse the "panels" in another form then you probably should create user controls as suggested by icktoofay.
0xA3
if I use the tab control, is there any method that I can change the tab button in tab control to a image ? because the build in tab control is really to ugly.In delphi, I can create multiple winform and dynamic load them I think this is more convenient for management.
MemoryLeak
+1  A: 

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 :

  1. You don't want to get into creating OwnerDrawn Tabs, which is possible.

  2. A standard WinForms TabControl will meet all your design-time needs.

Code :

  1. 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.

  2. 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.

BillW