Dynamic load containers

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

 

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:

 

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论坛的小工具,有兴趣的朋友可以试试,此工具已开始在国内推行:

MSDN论坛好帮手


posted @ 2010-11-14 12:49  Mike Dos Zhang  阅读(552)  评论(1编辑  收藏  举报