流布局面板 [英] Flow Layout Panel
本文介绍了流布局面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Windows窗体应用程序
如何在flowLayoutPanel中创建两个标签?
我希望它们成为flowLayoutPanel中的第一个和最后一个控件,以便添加的任何控件都不会影响它们的位置.
感谢您的帮助.
Windows Form Application
How do I create two labels in the flowLayoutPanel?
I want them to be the first and last control in flowLayoutPanel so that any controls added won''t affect their positions.
Thanks for your help.
推荐答案
您不能,FlowLayoutPanel不能那样工作.
您可以做的是编写一个自定义的AddMethod,该方法将最后一项删除并读取,如下所示:
You can''t, FlowLayoutPanel doesn''t work that way.
What you could do is write a custom AddMethod that takes the last item and removes it and readds it, something like this:
private static void AddStuff(FlowLayoutPanel panel, Control control)
{
Control last = panel.Controls[panel.Controls.Count - 1];
panel.Controls.Remove(last);
panel.Controls.Add(control);
panel.Controls.Add(last);
}
或类似的东西.
希望这会有所帮助,
Fredrik Bornander
Or something like it.
Hope this helps,
Fredrik Bornander
尝试一下(SetChildIndex可以解决问题)
Try this (SetChildIndex does the trick)
using System;
using System.Windows.Forms;
namespace FlowLayoutPanelTest
{
static class Program
{
static void Main()
{
Application.Run(new TestForm());
}
class TestForm : Form
{
public TestForm()
{
FlowLayoutPanel flowlayoutpanel = new FlowLayoutPanel();
flowlayoutpanel.Dock = DockStyle.Fill;
Button buttonAddControl = new Button();
buttonAddControl.Text = "Add Control";
buttonAddControl.Dock = DockStyle.Bottom;
buttonAddControl.Click += delegate(object obj, EventArgs ea)
{
AddControlToFlowLayoutPanel(flowlayoutpanel, new Button()); // add any control
};
Label labelFirst = new Label();
labelFirst.Text = "First";
Label labelLast = new Label();
labelLast.Text = "Last";
flowlayoutpanel.Controls.Add(labelFirst);
flowlayoutpanel.Controls.Add(labelLast);
Controls.Add(flowlayoutpanel);
Controls.Add(buttonAddControl);
}
void AddControlToFlowLayoutPanel(FlowLayoutPanel flowlayoutpanel, Control ctrl)
{
flowlayoutpanel.Controls.Add(ctrl);
// SetChildIndex does the trick
if (flowlayoutpanel.Controls.Count > 2)
flowlayoutpanel.Controls.SetChildIndex(ctrl, flowlayoutpanel.Controls.Count - 2);
}
}
}
}
这篇关于流布局面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文