C# Add Controls To Panel In a Loop

2019-01-19 02:07发布

I wish to add a button for every line in a file to a panel. My code so far is:

StreamReader menu = new StreamReader("menu.prefs");
int repetition = 0;

while(!menu.EndOfStream)
{
    Button dynamicbutton = new Button();
    dynamicbutton.Click += new System.EventHandler(menuItem_Click);
    dynamicbutton.Text = menu.ReadLine();
    dynamicbutton.Visible = true;
    dynamicbutton.Location = new Point(4+repetition*307, 4);
    dynamicbutton.Height = 44;
    dynamicbutton.Width = 203;
    dynamicbutton.BackColor = Color.FromArgb(40,40,40);
    dynamicbutton.ForeColor = Color.White;
    dynamicbutton.Font = new Font("Lucida Console", 16);
    dynamicbutton.Show();
    menuPanel.Controls.Add(dynamicbutton);
    repetition++;
    MessageBox.Show(dynamicbutton.Location.ToString());
}
menu.Close();

The problem is that only the first control gets created.

6条回答
在下西门庆
2楼-- · 2019-01-19 02:40

Each time it is generating the same name for dynamic controls. That's the reason why it is showing only the last one. It simply overwrites the previous control each time.

查看更多
Deceive 欺骗
3楼-- · 2019-01-19 02:44

The code looks fine but there could be a following situations.

1.You might have only one entry in the file, so you are experiencing only One Button added to the panel.

2.Your panel width is smaller than the sum of all the dynamic buttons width.

I suspect no 2 is the main reason that is causing problem.

So, I recommend that you use FlowLayoutPanel. To add a dynamic content as it automatically layout all the child controls.

查看更多
干净又极端
4楼-- · 2019-01-19 02:46

Try this code

        StreamReader menu = new StreamReader("menu.prefs");
        var str = menu.ReadToEnd();
        var items = str.Split(new string[] {"\r\n" } , StringSplitOptions.RemoveEmptyEntries);
        foreach (var item in items)
        {
           Button dynamicbutton = new Button();
           dynamicbutton.Click += new System.EventHandler(menuItem_Click);
           dynamicbutton.Text = item;
           dynamicbutton.Visible = true;
           dynamicbutton.Location = new Point(4+repetition*307, 4);
           dynamicbutton.Height = 44;
           dynamicbutton.Width = 203;
           dynamicbutton.BackColor = Color.FromArgb(40,40,40);
           dynamicbutton.ForeColor = Color.White;
           dynamicbutton.Font = new Font("Lucida Console", 16);
           dynamicbutton.Show();
           menuPanel.Controls.Add(dynamicbutton);
           repetition++;
        }
查看更多
对你真心纯属浪费
5楼-- · 2019-01-19 02:59

I also have similar problems with panels. For what you are doing it could be useful to just add strings to a listbox rather than using labels and a panel. That should be simpler.

查看更多
看我几分像从前
6楼-- · 2019-01-19 03:00

The problem with Panel and similar controls other than the FlowLayoutPanel is when you create a control and a second one, the second is created at the same position if you are not changing it's location dynamically or setting it according to the other already added controls. Your control is there, it's in the back of the first control.

A flowLayoutPanel is better as it will add the controls next to each other as you add them while compromising more finer control at their positioning.

查看更多
虎瘦雄心在
7楼-- · 2019-01-19 03:06
int x = 4;
int y = 4;
foreach(PhysicianData pd in listPhysicians)
{
   x = 4;
   y = panPhysicians.Controls.Count * 30;
   RadioButton rb = new RadioButton();
   rb.CheckedChanged += new System.EventHandler(rbPhysician_CheckedChanged);
   rb.Text = pd.name;
   rb.Visible = true;
   rb.Location = new Point(x, y);
   rb.Height = 40;
   rb.Width = 200;
   rb.BackColor = SystemColors.Control;
   rb.ForeColor = Color.Black;
   rb.Font = new Font("Microsoft Sans Serif", 10);
   rb.Show();
   rb.Name = "rb" + panPhysicians.Controls.Count;
   panPhysicians.Controls.Add(rb);
}
查看更多
登录 后发表回答