tabcontrol动态生成选项卡,并且在选项卡中添加窗体

http://blog.csdn.net/zx13525079024/article/details/6084733

今天在论坛上看到有人问到,如果在点击TRVEVIEW时动态生成tabcontrol的选项卡,并在选项卡中添加窗体,于是写了一个DEMO,供大家参考

demo下载:http://download.csdn.net/source/2920757

tabcontrol动态生成选项卡,并且在选项卡中添加窗体

  1. public partial class Form5 : Form
  2. {
  3. public Form5()
  4. {
  5. InitializeComponent();
  6. }
  7. private void Form5_Load(object sender, EventArgs e)
  8. {
  9. TreeNode root = new TreeNode("电话缴费");
  10. root.Name = "root";
  11. treeView1.Nodes.Add(root);
  12. TreeNode tnMobile = new TreeNode("中国移动");
  13. tnMobile.Name = "mobile";
  14. root.Nodes.Add(tnMobile);
  15. TreeNode tnUnion = new TreeNode("中国联通");
  16. tnUnion.Name = "union";
  17. root.Nodes.Add(tnUnion);
  18. root.Expand();
  19. }
  20. private TabPage tbMobile = null;
  21. private TabPage tbUnion = null;
  22. private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
  23. {
  24. if (treeView1.SelectedNode.Name=="mobile")
  25. {
  26. if (ErgodicModiForm("tbmobile", tabControl1))
  27. {
  28. tbMobile = new TabPage("中国移动");
  29. tbMobile.Name = "tbmobile";
  30. tabControl1.Controls.Add(tbMobile);
  31. Form6 form = new Form6();
  32. form.TopLevel = false;
  33. form.BackColor = Color.White;
  34. form.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
  35. form.FormBorderStyle = FormBorderStyle.None;
  36. form.Show();
  37. tbMobile.Controls.Add(form);
  38. }
  39. tabControl1.SelectedTab = tbMobile;
  40. }
  41. else if (treeView1.SelectedNode.Name=="union")
  42. {
  43. if (ErgodicModiForm("tbunion", tabControl1))
  44. {
  45. tbUnion = new TabPage("中国联通");
  46. tbUnion.Name = "tbunion";
  47. tabControl1.Controls.Add(tbUnion);
  48. Form2 form = new Form2();
  49. form.TopLevel = false;
  50. form.BackColor = Color.White;
  51. form.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
  52. form.FormBorderStyle = FormBorderStyle.None;
  53. form.Show();
  54. tbUnion.Controls.Add(form);
  55. }
  56. tabControl1.SelectedTab = tbUnion;
  57. }
  58. }
  59. /// </summary>
  60. /// <param name="MainTabControlKey">选项卡的键值</param>
  61. /// <param name="objTabControl">要添加到的TabControl对象</param>
  62. /// <returns></returns>
  63. private Boolean ErgodicModiForm(string MainTabControlKey, TabControl objTabControl)
  64. {
  65. //遍历选项卡判断是否存在该子窗体
  66. foreach (Control con in objTabControl.Controls)
  67. {
  68. TabPage tab = (TabPage)con;
  69. if (tab.Name == MainTabControlKey)
  70. {
  71. return false;//存在
  72. }
  73. }
  74. return true;//不存在
  75. }
  76. }
tabcontrol动态生成选项卡,并且在选项卡中添加窗体

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;//不存在
}
}

 

上一篇:PyQt(Python+Qt)学习随笔:QTabWidget选项卡部件设置tabsClosable后不能关闭选项卡的原因


下一篇:【AIX 命令学习】lsfs 查看显示文件系统信息