1. Create a winForm project.
2. Drag a SplitContainer onto the Form1.
3. Drag a TreeView onto the left panel of the SplitContainer and add 2 sub nodes: this.splitContainer1.Panel1.Controls.Add(this.treeView1).
4. Deal with the NodeMouseClick event (we need to clean the old controls on the Panel first then load another container) :
Form1.cs
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
string nodeName = e.Node.Name;
this.splitContainer1.Panel2.Controls.Clear();
switch (nodeName)
{
case "Node1":
LoadGroupBox1();
break;
case "Node2":
LoadGroupBox2();
break;
default:
LoadGroupBox1();
break;
}
}
5. Drag a GroupBox onto the right panel of the SplitContainer.
a. Design this GroupBox : add TextBox, buttons and so on controls and register its event.
b. After finish the design of this GroupBox, then open the Form1.Designer.cs file and cut out everything related to this GroupBox.
c. Create a new file Group1.cs:
partial class Form1
d. Past all the code above we cut from the Form1.Designer.cs file into the GroupBox1.cs file like this, and then we can dynamic load this GroupBox and its controls with calling the LoadGroupBox1() method:
partial class Form1
{
private GroupBox groupBox1;
private Label label1;
private Button button1;
private TextBox textBox1;
private void LoadGroupBox1()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.groupBox1.Location = new System.Drawing.Point(0, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(458, 516);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(81, 83);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(7, 21);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(129, 83);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = Properties.Settings.Default.textBox1Value;
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.splitContainer1.Panel2.Controls.Add(this.groupBox1);
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "g1";
Properties.Settings.Default.textBox1Value = this.textBox1.Text;
}
}
6. We use Settings.settings to save our TextBox values (note: set the “Scope” to “user”, then we can dynamic set its value at runtime).
7. And do the same sub steps in step 5, you can design the second or more containers to your project. And with the step 6 to set and save our value.
8. After above steps we had finished this demo.
Using Settings in C#
Partial Classes and Methods (C# Programming Guide)
Download: http://cid-bb789f72272d4858.office.live.com/self.aspx/.Documents/Dynamic%20load%20containers.zip
PS:我的同事开发了一个MSDN论坛的小工具,有兴趣的朋友可以试试,此工具已开始在国内推行: