1

我有这样的事情:

class MyPanel : FlowLayoutPanel
{
     public MyPanel()

     {
        this.BackColor = Color.Red;
        this.FlowDirection = System.Windows.Forms.FlowDirection.LeftToRight;

        listBox = new ListBox();

        editButton = new Button();
        //editButton.Click += editButton_Click;

        this.Controls.Add(listBox);
        this.Controls.Add(editButton);
     }
 }

然后我将它动态添加到我的表单中。问题是 listBox 与按钮重叠。但是,如果我尝试添加 2 个按钮而不是列表,它会按预期工作 - 按钮从左到右排列在 1 行中。我想到达列表旁边的按钮。有人可以给我建议吗?谢谢

4

2 回答 2

1

试试这个代码:

class MyPanel : FlowLayoutPanel
{
     public MyPanel()

     {
        this.BackColor = Color.Red;
        this.FlowDirection = System.Windows.Forms.FlowDirection.LeftToRight;

        listBox = new ListBox();

        this.WrapContents = false;  // Use this for control not wrapped
        editButton = new Button();


        this.Controls.Add(listBox);
        this.Controls.Add(editButton);
     }
 }
于 2013-05-13T11:40:54.200 回答
0

为您动态添加的MyPanel对象指定大小。它会解决你的问题。

private void Form1_Load(object sender, EventArgs e)
{
    MyPanel p = new MyPanel();
    p.Size = new Size(500, 200); //give size
    this.Controls.Add(p); // add to form 
}
于 2013-05-13T12:54:32.960 回答