Tabpage的个数不定, Tabpage里面控件时一样的(含button1,datagridview1,textbox各一个 datagridview1显示的数据不同,分别取不同的数据库表)。我想实现 窗体启动的时候, 根据数据库表(select storecode, storeqty from storecode)的storeqty数量自动
产生相应的tabpage。也就是tabpage 复制,tabpage1是固定的,动态的tabpage都复制tabpage1的界面,里面显示数据时不一样的。不知道说明白没有?能否提供一个例子?vb bet 或者c#都可以,谢谢!如果不好贴,也可以把例子发邮件[email protected],谢谢!

解决方案 »

  1.   

    Tabpage tp=new TabPage();
    Button btn=new Button();
    ....
    btn.Text=button1.Text;
    ....
    tp.Controls.Add(btn);
    ....tabControl.Controls.Add(tp);  
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  2.   

    新建TabControlTabControl.Name 为DocumentUpStandardConditions_TabControl 
    Size为629, 112以下为动态生成TabPage代码          
                       for (int i = 1; i <= 10; i++)
                       {
                        TextBox tb = new TextBox();
                        tb.Location = new System.Drawing.Point(3, 3);
                        tb.Multiline = true;
                        tb.Name = "StandardTb" + i.ToString();
                        tb.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
                        tb.Size = new System.Drawing.Size(615, 81);
                        tb.Text = "TabPage "+i;
                        tb.ReadOnly = true;                    TabPage tp = new TabPage();
                        tp.Controls.Add(tb);
                        tp.Location = new System.Drawing.Point(4, 21);
                        tp.Name = "StandardTp" + i.ToString();
                        tp.Padding = new System.Windows.Forms.Padding(3);
                        tp.Size = new System.Drawing.Size(621, 87);
                        tp.Text = "TabPage" + i.ToString(); ;
                        tp.UseVisualStyleBackColor = true;                    this.DocumentUpStandardConditions_TabControl.Controls.Add(tp);
                    }
      

  3.   

    直接这样做事做不到的,TabPage并没有实现IClone接口,无法复制。
    我的建议:你的TabPage1的界面初始化,使用代码来完成,增加一个方法,里面控件的命名要与TabPage1的名称相关联,意在避免命名重复。
    TabPage ConstructTabPage()
    {
        ...
    }你如果需要一个tabpage的话,就调用该方法,返回一个,然后将该TabPage添加到TabControl里面就可以了
    最好实现预绑定,也就是在构造TabPage的时候,把数据也传进去,如:
    TabPage ConstructTabPage(DataTable dt)
    {
        //初始化TabPage
        //初始化控件
        //添加到TabPage
        //将数据绑定到ListView/DataGrid控件    //返回TabPage
    }
      

  4.   

    还有一种方法,把(button1,datagridview1,textbox各一个 datagridview1)放到一个自定以控件,每次只用加载一个控件就行啦
      

  5.   


    private void button2_Click(object sender, EventArgs e)
            {
                DataTable dt = null;
                TabPage page = constructTabPage(dt, tabControl1.TabPages.Count);            tabControl1.TabPages.Add(page);
            }        private static TabPage constructTabPage(DataTable dt,int index)
            {
                TabPage tabPage;
                Button button;
                TextBox textBox;
                DataGridView dataGridView;            tabPage = new TabPage();
                dataGridView = new DataGridView();
                textBox = new TextBox();
                button = new Button();            // 
                // tabPage1
                // 
                tabPage.Controls.Add(button);
                tabPage.Controls.Add(textBox);
                tabPage.Controls.Add(dataGridView);
                tabPage.Location = new Point(4, 21);
                tabPage.Name = string.Format("tabPage{0}", index + 1);
                tabPage.Padding = new Padding(3);
                tabPage.Size = new Size(475, 327);
                tabPage.TabIndex = 0;
                tabPage.Text = string.Format("tabPage{0}", index + 1);
                tabPage.UseVisualStyleBackColor = true;            // 
                // dataGridView1
                // 
                dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                dataGridView.Location = new Point(6, 40);
                dataGridView.Name = string.Format("dataGridView{0}_1",index+1);
                dataGridView.RowTemplate.Height = 23;
                dataGridView.Size = new Size(463, 281);
                dataGridView.TabIndex = 0;
                // 
                // textBox1
                // 
                textBox.Location = new System.Drawing.Point(6, 13);
                textBox.Name = string.Format("textBox1{0}_1", index+1);
                textBox.Size = new Size(378, 21);
                textBox.TabIndex = 1;
                // 
                // button1
                // 
                button.Location = new System.Drawing.Point(390, 11);
                button.Name = string.Format("button1{0}_1", index+1);
                button.Size = new Size(79, 23);
                button.TabIndex = 2;
                button.Text = "button1";
                button.UseVisualStyleBackColor = true;            return tabPage;
            }
      

  6.   

    将tabControl1的所有Page全部删除掉,然后楼主把如上代码拷贝进去,然后跑一下看看,我试了,是OK的。
    只是在绑定数据的地方我没有实现,传入的DataTable 是null,你可以追加一些实现。
      

  7.   

    整个form的代码:
    public partial class Form5 : Form
        {
            public Form5()
            {
                InitializeComponent();
            }        private void button2_Click(object sender, EventArgs e)
            {
                DataTable dt = null;
                TabPage page = constructTabPage(dt, tabControl1.TabPages.Count);            tabControl1.TabPages.Add(page);
            }        private static TabPage constructTabPage(DataTable dt,int index)
            {
                TabPage tabPage;
                Button button;
                TextBox textBox;
                DataGridView dataGridView;            tabPage = new TabPage();
                dataGridView = new DataGridView();
                textBox = new TextBox();
                button = new Button();            // 
                // tabPage1
                // 
                tabPage.Controls.Add(button);
                tabPage.Controls.Add(textBox);
                tabPage.Controls.Add(dataGridView);
                tabPage.Location = new Point(4, 21);
                tabPage.Name = string.Format("tabPage{0}", index + 1);
                tabPage.Padding = new Padding(3);
                tabPage.Size = new Size(475, 327);
                tabPage.TabIndex = 0;
                tabPage.Text = string.Format("tabPage{0}", index + 1);
                tabPage.UseVisualStyleBackColor = true;            // 
                // dataGridView1
                // 
                dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                dataGridView.Location = new Point(6, 40);
                dataGridView.Name = string.Format("dataGridView{0}_1",index+1);
                dataGridView.RowTemplate.Height = 23;
                dataGridView.Size = new Size(463, 281);
                dataGridView.TabIndex = 0;
                // 
                // textBox1
                // 
                textBox.Location = new System.Drawing.Point(6, 13);
                textBox.Name = string.Format("textBox1{0}_1", index+1);
                textBox.Size = new Size(378, 21);
                textBox.TabIndex = 1;
                // 
                // button1
                // 
                button.Location = new System.Drawing.Point(390, 11);
                button.Name = string.Format("button1{0}_1", index+1);
                button.Size = new Size(79, 23);
                button.TabIndex = 2;
                button.Text = "button1";
                button.UseVisualStyleBackColor = true;            return tabPage;
            }
        }partial class Form5
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.tabControl1 = new System.Windows.Forms.TabControl();
                this.button2 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // tabControl1
                // 
                this.tabControl1.Location = new System.Drawing.Point(55, 41);
                this.tabControl1.Name = "tabControl1";
                this.tabControl1.SelectedIndex = 0;
                this.tabControl1.Size = new System.Drawing.Size(483, 352);
                this.tabControl1.TabIndex = 0;
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(459, 12);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(75, 23);
                this.button2.TabIndex = 1;
                this.button2.Text = "Add New Page";
                this.button2.UseVisualStyleBackColor = true;
                this.button2.Click += new System.EventHandler(this.button2_Click);
                // 
                // Form5
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(590, 425);
                this.Controls.Add(this.button2);
                this.Controls.Add(this.tabControl1);
                this.Name = "Form5";
                this.Text = "Form5";
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.TabControl tabControl1;
            private System.Windows.Forms.Button button2;
        }
      

  8.   

    建议:
    TabPage page2 = new TabPage("名称1");
                TextBox txt = new TextBox();
                DataGridView dgv = new DataGridView();
                //数据绑定到datagridview里
                Button btn = new Button();
                page2.Controls.Add(txt);
                page2.Controls.Add(dgv);
                page2.Controls.Add(btn);            tabControl1.TabPages.Add(page2);动态的加载
      

  9.   

    请问在Web页面里怎么实现这个效果?
      

  10.   

    懒人的方法:先通过VS布局一个TabPage,然后把它的代码拷贝下来封装成一个自定义控件类,然后用时new一个添加到Tabcontrol中即可