【发布时间】:2009-07-10 23:18:01
【问题描述】:
我有一个控件,上面有几个输入控件、一些文本框、一个组合框和一个列表框等等。一个文本框、一个组合框、一个列表框和几个命令按钮被锚定,以便它们随表单调整大小。组合框锚定在左侧、顶部和右侧,列表框锚定在所有四个侧面,按钮锚定在底部和右侧。他们都正确调整大小并保持其位置。问题是我试图锚定的文本框,就像组合框一样。当锚定到右侧时,它会在运行时从表单的右侧运行。但是,我对窗体上锚定在右侧的任何现有控件都没有这个问题,它们都保持在应有的位置。
我尝试添加一个额外的文本框和组合框并将它们锚定在右侧,但它们也超出了表单的右边缘。为什么现有的控件,除了一个之外,都像他们应该做的那样工作,但是一个控件和新控件变得太宽了?
作为参考,我已经操纵了设计器生成的代码来重新排列语句的顺序,以便在设置位置和宽度之后发生锚定,但这没有用。这是行为不端的文本框的代码:
//
// txtComments
//
this.txtComments.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtComments.Location = new System.Drawing.Point(430, 6);
this.txtComments.Name = "txtComments";
this.txtComments.Size = new System.Drawing.Size(166, 20);
this.txtComments.TabIndex = 25;
以及行为正确的组合框的代码:
//
// cbMacroList
//
this.cbMacroList.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.cbMacroList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbMacroList.FormattingEnabled = true;
this.cbMacroList.Location = new System.Drawing.Point(128, 78);
this.cbMacroList.Name = "cbMacroList";
this.cbMacroList.Size = new System.Drawing.Size(468, 21);
this.cbMacroList.TabIndex = 30;
this.cbMacroList.SelectedIndexChanged += new System.EventHandler(this.cbMacroList_SelectedIndexChanged);
最后一个背景是,当我确定我需要在另一个表单中重用表单提供的功能时,所有这些控件都来自表单。我选择了所有控件,将它们剪切,然后粘贴到新的 UserControl 上。在迁移到 UserControl 之前,它们都运行良好。这在迁移后开始出现。
【问题讨论】:
标签: c# winforms visual-studio-2008 layout