简体   繁体   中英

C# Add Controls To Panel In a Loop

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.

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.

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.

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

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.

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

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.

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