C# Add Controls To Panel In a Loop

核能气质少年 提交于 2019-11-27 06:11:51

问题


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.


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/6406868/c-sharp-add-controls-to-panel-in-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!