How to dynamically load a panel in Windows Forms?

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:

Answers


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.


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.


For example if you have two form Form1 and Form2 and you want to load form2 inside from1. when you press a button to load form2 the code is like this

private void button1_Click(object sender, EventArgs e)
{
     Form2 form2 = new Form2();
     this.Controls.Clear();
     foreach(Control c in this.Controls)
     {
         this.Controls.Add(c);
     }
}

this code will load all the controls in the form2 into form1.


Need Your Help

FileReader returns empty result for file from the bundle

iphone cordova filereader

I've tried to read a file from the app bundle using phonegap's FileReader class:

Two PHP Forms that Redirect to x/y URL on the same page

php forms url redirect if-statement

I am trying to create a 'simple' redirect for clients who use either Wordpress or Indexhibit to manage there sites.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.