简体   繁体   中英

C# winforms add controls to dynamic created tabpages

I have problem when i want to add controls to dynamic created tabpages. My software is POS system for restaurants. My idea is first create restaurant regions and regions has diferent number of the desks. Example: Region for diner have 15 desks, caffe bar region has 22 desks. Somthing like category and products in this case regions has desks. So to do that i use tabcontrol component! All tabpages is region name and tabpage content must be desks showed as buttons ...

All regions and desks is stored in the MySql database.

So if tab_1 (Region #1) is selected as content loop all desks buttons on page.

My code all time return just one desk but i have 10 desks for region 1..

Code for listing regions:

private void RegionList()
{
    try
    {
        using (var conn = new MySqlConnection(Properties.Settings.Default["connectionstring"].ToString()))
        {
            conn.Open();
            using (MySqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = "SELECT * FROM regions";
                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    TabPage tabPage = new TabPage();
                    string name = reader["name"].ToString();
                    int id = Convert.ToInt32(reader["id"]);

                    tabPage.Name = "tab_" + id;
                    tabPage.Text = name;

                    foreach (var desk in DeskList(id))
                    {
                        tabPage.Controls.Add(desk);
                    }
                    tabControl1.TabPages.Add(tabPage);
                }
                reader.Close();
            }
            conn.Close();
        };
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

And here is code for listing desks on specific region:

private List<Button> DeskList(int regionID)
{
    List<Button> desks = new List<Button>();

    try
    {
        using (var conn = new MySqlConnection(Properties.Settings.Default["connectionstring"].ToString()))
        {
            conn.Open();

            using (MySqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = "SELECT * FROM desks WHERE region_id = @id";
                cmd.Parameters.AddWithValue("id", regionID);

                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    Button btn = new Button();
                    string name = reader["name"].ToString();

                    btn.Text = name;
                    btn.Name = "desk_" + reader["id"];
                    btn.Size = new Size(100, 60);

                    desk.Add(btn);

                }
                reader.Close();
            };
            conn.Close();
        };
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

    return desks;
}

So when i loop this i get just in all regions only 1 desk but correct result. All regions has min 10 desks. What i do wrong? Maybie my appoarch is wrong

I think the previous statement regarding the placement of the buttons is probably the problem. Try adding the following which will change the placement of each button.

        int i = 0;
        while (reader.Read())
        {
            Button btn = new Button();
            string name = reader["name"].ToString();

            btn.Text = name;
            btn.Name = "desk_" + reader["id"];
            btn.Size = new Size(100, 60);
            btn.Location=new Point(100, 100+i);

            desk.Add(btn);
            i += 10;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM