简体   繁体   English

C#将控件添加到循环中的面板

[英]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. 1.您的文件中可能只有一个条目,因此您只能在面板中添加一个按钮。

2.Your panel width is smaller than the sum of all the dynamic buttons width. 2.您的面板宽度小于所有动态按钮宽度的总和。

I suspect no 2 is the main reason that is causing problem. 我怀疑没有2是造成问题的主要原因。

So, I recommend that you use FlowLayoutPanel . 所以,我建议你使用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. 除了FlowLayoutPanel之外,Panel和类似控件的问题在于,当您创建控件和第二个控件时,如果您没有动态更改它的位置或根据其他已添加的控件设置它,则在同一位置创建第二个控件。 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. flowLayoutPanel更好,因为它会在您添加控件时将控件添加到彼此旁边,同时在定位时损害更精细的控制。

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. 这应该更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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