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

时间:2016-09-05 13:04:02

标签: c# forms winforms tabs runtime

我有一个包含多个窗体的项目,每个窗体用于编辑不同数据库表的记录。其中一些数据库表有一个名为mycolumn的列,直到现在还没有被项目的实现使用。

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

我找到了一些Stackoverflow答案,允许您在已有一个选项卡的表单中添加一个选项卡控件(在运行时)。不幸的是,这对我不起作用,因为所有表单都没有标签。诸如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

    }

我该怎么做?

1 个答案:

答案 0 :(得分:0)

以下是我为您制作的示例,

 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);
        }

结果; enter image description here 试图用评论来解释,

更新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.
            }


        }

希望有所帮助,

相关问题