流式布局控件在WinForms中添加控件时方向错误。

17

我在WinForms中使用流式布局控件,我已将其流向设置为TopDown,但它仍然从左到右添加控件,同时autoscroll也设置为true。

flowLayoutPanel1.Controls.Clear();    
Label labelInput = new Label();
ListBox listBoxNewInput = new ListBox();

//Initialize label's property
labelInput.Text = " #" + Convert.ToInt32(sequence);
labelInput.AutoSize = true;

//Initialize textBoxes Property
listBoxNewInput.HorizontalScrollbar = false;

listBoxNewInput.Items.Add(efforts);
//Add the newly created text box to the list of input text boxes
inputTextBoxesList.Add(listBoxNewInput);

//Add the labels and text box to the form
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.Controls.Add(labelInput);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.Controls.Add(listBoxNewInput);

我想要的是,如果它们在垂直方向上不适合,它应该生成一个滚动条,但它把它们放在右边。 - PUG
1个回答

30

flowLayoutPanel1WrapContents 属性设置为 false,这样如果控件不能适合容器的大小时就不会移动到右侧。为了能够滚动被裁剪的内容,您可以将 AutoScroll 属性设置为 true

以下是代码:

flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.WrapContents = false;
flowLayoutPanel1.AutoScroll = true;
flowLayoutPanel1.Controls.Add(labelInput);
flowLayoutPanel1.Controls.Add(listBoxNewInput);

1
哦,如果没有直接指导,我要花好几周才能解决这个问题。谢谢! - ErTR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接