运行时将表单组件放入选项卡

Put form components in tab at runtime

我有一个项目有多个windows表单,每个表单用于编辑不同数据库table的记录。其中一些数据库 table 有一个名为 mycolumn 的列,直到现在该项目的实施还没有使用它。

对于具有列 mycolumn 的数据库 table,我想在对应于数据库 table 的 windows 表单中添加一个选项卡控件。我想在运行时以编程方式执行此操作。

我找到了一些 Whosebug 答案,这些答案允许您将选项卡控件(在运行时)添加到已有一个选项卡的表单中。不幸的是,这对我不起作用,因为所有表单都没有标签。 tabControl.Controls.Add(tabpagename) 之类的调用不起作用,因为我还没有 tabControls。

因此,我正在寻找一种以编程方式创建选项卡控件并将所有现有表单组件放入第一个选项卡中的方法,这样我就可以在将来使用它以编程方式向选项卡控件添加选项卡。

伪代码如下:

    form1constructor() {
        InitializeComponent();

        if (TableHasColumn("mycolumn")) {
            PutAllExistingComponentsInTabControl(); // Don't know how to do this
            AddTabControlWithName("myColumn"); // DO know how to do this, not part of the question

    }

我该怎么做?

这是我为您制作的示例,

 List<string> tableNames; // for table Names
 string columnName = "TestName"; // your ColumnName
        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection("Server=.;Database=dbName;Trusted_Connection=True;"); // connection string
            cnn.Open();
            tableNames = cnn.GetSchema("Tables").AsEnumerable().Select(x=> x[2].ToString()).ToList(); // get all table Names and put them into string List.
            SqlDataAdapter adp;
            DataTable dt;
            foreach (var item in tableNames) // loop in names
            {   dt =  new DataTable(); 
                adp = new SqlDataAdapter("Select * from dbo.[" + item + "]", cnn);
                adp.Fill(dt);
                if (dt.Columns.Contains(columnName)) // check columnName in loop
                {
                    TabPage tb = new TabPage(); //If we had a match then we need a tabpage
                    tb.Text = columnName; // Named with columnName
                    tabControl1.TabPages.Add(tb); // Add to TabControl
                    PutControls(tb); // Then call our method to add components
                }
            }
        }

调用组件的方法,

public void PutControls(TabPage tb) {
            Button btn = new Button();
            btn.Text = "testButton";
            btn.Height = 20;
            btn.Width = 150;
            btn.Location = new Point(4, 26);
            TextBox txt = new TextBox();
            txt.Text = "test";
            txt.Location =new Point(34, 126);
            tb.Controls.Add(btn);
            tb.Controls.Add(txt);
        }

结果; 试图用评论解释,

更新 1

所以我假设你不需要在表中循环,你只需要创建一个 tabcontrol 并将所有控件放在一个新选项卡中。

Form_Load 应该是这样,

 string columnName = "TestName"; // your ColumnName
        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection("Server=.;Database=db;Trusted_Connection=True;"); // connection string
            cnn.Open();
            SqlDataAdapter adp;
            DataTable dt;
            string tableName = "tableNane";
            dt = new DataTable();
            adp = new SqlDataAdapter("Select * from " + tableName, cnn);
            adp.Fill(dt);
            if (dt.Columns.Contains(columnName)) 
            {
                TabControl tbControl = new TabControl();
                tbControl.Height = 100;
                tbControl.Width = 200;
                tbControl.Name = columnName;
                this.Controls.Add(tbControl);
                TabPage tb = new TabPage();
                tb.Text = columnName;
                tbControl.TabPages.Add(tb);
                PutControls(tb); // the method defined at the top of the answer.
            }


        }

希望有所帮助,