在运行时锚定到流布局 [英] Anchor to Flow layout at runtime
本文介绍了在运行时锚定到流布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我在将控件固定在流布局面板的左右边缘时遇到困难.
我无法在设计器中查看控件的设置,因为它们在运行时动态加载,因此需要找到一种方法.
我已经尝试过的代码应该可以正常工作,我看不到出了什么问题=/
Hi all,
I am experiencing difficulty in anchoring a control within a flow layout panel to the right and left edges.
I cannot view the controls to set this in the designer as they are loaded dynamically at run time so need to find a way of doing this.
The code i have already tried is which should work, i cannot see what''s going wrong =/
private void flowLayoutPanel1_ControlAdded(object sender, ControlEventArgs e)
{
e.Control.Anchor = ((System.Windows.Forms.AnchorStyles)
(((System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
}
谢谢George
Thanks George
推荐答案
感谢约翰尼斯内斯特勒(Johannesnestler)发布的链接,我得以设计出一种解决方案.
链接: http://msdn.microsoft.com/en-us/library /ms171633(v=VS.90).aspx [ ^ ]
(执行此操作可能有更有效的方法,但是它适用于我的应用程序的范围,该应用程序的流程布局中将包含3个控件)
Thanks to the link posted by Johannesnestler i was able to devise a solution.
Link: http://msdn.microsoft.com/en-us/library/ms171633(v=VS.90).aspx[^]
(There may be a more efficient way of doing this however it works for the scope of my application which will have 3 controls in the flow layout)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Populate flow layout
flowLayoutPanel1.Controls.Add(new Button());
flowLayoutPanel1.Controls.Add(new Button());
flowLayoutPanel1.Controls.Add(new Button());
flowLayoutPanel1.Controls.Add(new Button());
}
private void flowLayoutPanel1_ControlAdded(object sender, ControlEventArgs e)
{
// Set the width of the first control added to the flow layout and the anchor of the rest
if (e.Control.Equals(flowLayoutPanel1.Controls[0]))
{
e.Control.Size = new Size(flowLayoutPanel1.Width - 4,
flowLayoutPanel1.Controls[0].Height);
}
else
{
// Anchor will span the area of the first control in the list view
e.Control.Anchor = ((System.Windows.Forms.AnchorStyles)
(((System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if(flowLayoutPanel1.Controls.Count > 0)
{
// Set size of the first control in the flow layout as the rest will anchor depending
// on its width. (4) represents border offset
flowLayoutPanel1.Controls[0].Size = new Size(flowLayoutPanel1.Width - 4,
flowLayoutPanel1.Controls[0].Height);
return;
}
}
}
这篇关于在运行时锚定到流布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文