tedvangageldonk tedvangageldonk - 1 month ago 14
C# Question

Put form components in tab at runtime

I have a project with multiple windows forms, each of which is used to edit records of different database tables. Some of those database tables have a column named

mycolumn
, which has not been used by the implementation of the project, until now.

For the database tables which have column
mycolumn
, I would like to add a tab control to the windows form that corresponds to the database table. I would like to do this programatically, at runtime.

I have found some Stackoverflow answers that allow you to add a tab control (at runtime) to a form that already has one tab. Unfortunately, that doesn't work for me, because all forms do not have tabs yet. A call such as
tabControl.Controls.Add(tabpagename)
does not work, because I have no tabControls yet.

Thus, I am looking for a way to programatically create a tab control and put all existing form components inside the first tab, such that I can use this to add tabs to the tab control programatically in the future.

Pseudo code looks like:

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

}


How do I do this?

Answer

Here is a sample that I made for you,

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

Method to call 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);
        }

result; enter image description here Tried to explain with comments,

Update 1

So I assume that you don't need to loop in tables, you just need to create a tabcontrol and put all controls inside a new tab.

Form_Load should be like this then,

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


        }

Hope helps,