tabcontrol动态生成选项卡,并且在选项卡中添加窗体
http://blog.csdn.net/zx13525079024/article/details/6084733
今天在论坛上看到有人问到,如果在点击TRVEVIEW时动态生成tabcontrol的选项卡,并在选项卡中添加窗体,于是写了一个DEMO,供大家参考
demo下载:http://download.csdn.net/source/2920757
- public partial class Form5 : Form
- {
- public Form5()
- {
- InitializeComponent();
- }
- private void Form5_Load(object sender, EventArgs e)
- {
- TreeNode root = new TreeNode("电话缴费");
- root.Name = "root";
- treeView1.Nodes.Add(root);
- TreeNode tnMobile = new TreeNode("中国移动");
- tnMobile.Name = "mobile";
- root.Nodes.Add(tnMobile);
- TreeNode tnUnion = new TreeNode("中国联通");
- tnUnion.Name = "union";
- root.Nodes.Add(tnUnion);
- root.Expand();
- }
- private TabPage tbMobile = null;
- private TabPage tbUnion = null;
- private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
- {
- if (treeView1.SelectedNode.Name=="mobile")
- {
- if (ErgodicModiForm("tbmobile", tabControl1))
- {
- tbMobile = new TabPage("中国移动");
- tbMobile.Name = "tbmobile";
- tabControl1.Controls.Add(tbMobile);
- Form6 form = new Form6();
- form.TopLevel = false;
- form.BackColor = Color.White;
- form.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
- form.FormBorderStyle = FormBorderStyle.None;
- form.Show();
- tbMobile.Controls.Add(form);
- }
- tabControl1.SelectedTab = tbMobile;
- }
- else if (treeView1.SelectedNode.Name=="union")
- {
- if (ErgodicModiForm("tbunion", tabControl1))
- {
- tbUnion = new TabPage("中国联通");
- tbUnion.Name = "tbunion";
- tabControl1.Controls.Add(tbUnion);
- Form2 form = new Form2();
- form.TopLevel = false;
- form.BackColor = Color.White;
- form.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
- form.FormBorderStyle = FormBorderStyle.None;
- form.Show();
- tbUnion.Controls.Add(form);
- }
- tabControl1.SelectedTab = tbUnion;
- }
- }
- /// </summary>
- /// <param name="MainTabControlKey">选项卡的键值</param>
- /// <param name="objTabControl">要添加到的TabControl对象</param>
- /// <returns></returns>
- private Boolean ErgodicModiForm(string MainTabControlKey, TabControl objTabControl)
- {
- //遍历选项卡判断是否存在该子窗体
- foreach (Control con in objTabControl.Controls)
- {
- TabPage tab = (TabPage)con;
- if (tab.Name == MainTabControlKey)
- {
- return false;//存在
- }
- }
- return true;//不存在
- }
- }
清空回声