【发布时间】:2016-04-07 16:23:40
【问题描述】:
我正在尝试将自定义容器创建为UserControl。
我的目标:我希望能够在设计器中拖动控件并在我的用户控件的代码中处理传入的控件。
示例:我将容器放在某处,然后添加一个按钮。在这个moemt中,我希望我的用户控件自动调整此按钮的宽度和位置。这就是我卡住的地方。
我的代码:
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public partial class ContactList : UserControl
{
public ContactList()
{
InitializeComponent();
}
private void ContactList_ControlAdded(object sender, ControlEventArgs e)
{
e.Control.Width = 200; // Nothing happens
e.Control.Height = 100; // Nothing happens
MessageBox.Show("Test"); // Firing when adding a control
}
}
MessageBox 运行良好。 width 和 height 集合被忽略。
问题只是“为什么?”。
编辑
我刚刚注意到,在放置按钮并使用 F6 重新编译时,按钮的大小会调整为 200x100。为什么放置时这不起作用?
我的意思是...FlowLayoutPanel 在您放置时处理添加的控件。这就是我正在寻找的确切行为。
【问题讨论】:
-
您的代码的主要问题是它没有应用大小。要使其应用大小,您应该使用
BeginInvoke设置大小。
标签: c# winforms user-controls windows-forms-designer designer