动态创建TabPage


 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
1
  public partial class frmTabPage : Form
 2    {
 3        private TabControl tabControl1;
 4        private TabPage tabPage1;
 5        private TabPage tabPage2;
 6        private Button button1;
 7        private Button button2;
 8        public frmTabPage()
 9        {
10            InitializeComponent();
11            tabControl1 = new System.Windows.Forms.TabControl();
12            tabPage1 = new System.Windows.Forms.TabPage();
13            tabPage2 = new System.Windows.Forms.TabPage();
14            button1 = new System.Windows.Forms.Button();
15            button2 = new System.Windows.Forms.Button();
16            this.CreateNewTabpage();
17        }

18  private void CreateNewTabpage()
19        {          
20            this.addPages(tabPage1, button1, "tabPage1");
21            this.addPages(tabPage2, button2, "tabPage2");
22            tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
23        }

24
25        /// <summary>
26        /// 动态创建TabPage
27        /// </summary>
28        /// <param name="tp">TabPage</param>
29        /// <param name="btn">Button</param>
30        /// <param name="sName">名称</param>

31        private void addPages(TabPage tp, Button btn, string sName)
32        {
33            int i = 0;
34            tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {tp});
35            tp.Controls.AddRange(new System.Windows.Forms.Control[] { btn });
36            tp.TabIndex = i;
37            tp.Text = "动态创建" + sName;
38            btn.Location = new System.Drawing.Point(6472);
39            btn.Click += new System.EventHandler(this.button1_Click);
40            btn.Text = "btn" + sName;
41            btn.Width = 90;
42            Controls.AddRange(new System.Windows.Forms.Control[] { tabControl1 });
43            i = i + 1;
44            tabControl1.TabIndex = 0;
45            tabControl1.SelectedIndex = 0;// (TabPage.GetTabPageOfComponent(btn)).TabIndex;
46            tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
47        }

48
49        //添加事件
50        private void button1_Click(object sender, EventArgs e)
51        {
52            MessageBox.Show("helloword!");
53        }
posted @ 2007-08-16 17:35  突破666  阅读(1336)  评论(0编辑  收藏  举报
郴州东江湖农家乐联盟:www.djhnjllm.com 郴州东江湖农家乐联盟