我对C#用户控件有一个相当奇怪的问题。基本上它是一个面板,您可以通过按下按钮添加控件行。
当用户按下按钮时,会添加一组新控件作为行。代码非常简单,只需设置属性并将它们添加到面板,如下所示:
private void AddCommentControls()
{
_noOfComments++;
ComboBox tempCbCategory = new ComboBox();
TextBox tempTxtRoomNo = new TextBox();
ComboBox tempPositive = new ComboBox();
TextBox tempTxtComment = new TextBox();
tempCbCategory.Name = "cbCategory" + _noOfComments.ToString();
tempTxtRoomNo.Name = "txtRoomNo" + _noOfComments.ToString();
tempPositive.Name = "cbPositive" + _noOfComments.ToString();
tempTxtComment.Name = "txtComment" + _noOfComments.ToString();
tempCbCategory.Location = controls["cbCategory" + (_noOfComments - 1).ToString()].Location;
tempCbCategory.FormattingEnabled = true;
tempCbCategory.Size = new Size(119, 21);
tempCbCategory.Location = new Point(tempCbCategory.Location.X, (tempCbCategory.Location.Y + tempCbCategory.Height + 50));
controls.Add("cbCategory" + _noOfComments.ToString(), tempCbCategory);
pnlBg.Controls.Add(tempCbCategory);
tempTxtRoomNo.Location = controls["txtRoomNo" + (_noOfComments - 1).ToString()].Location;
tempTxtRoomNo.Size = new Size(68, 21);
tempTxtRoomNo.Location = new Point(tempTxtRoomNo.Location.X, (tempTxtRoomNo.Location.Y + tempTxtRoomNo.Height + 50));
controls.Add("txtRoomNo" + _noOfComments.ToString(), tempTxtRoomNo);
pnlBg.Controls.Add(tempTxtRoomNo);
tempPositive.Location = controls["cbPositive" + (_noOfComments - 1).ToString()].Location;
tempPositive.Size = new Size(46, 21);
tempPositive.FormattingEnabled = true;
tempPositive.Items.AddRange(new object[] {"Yes", "No"});
tempPositive.Text = "Yes";
tempPositive.Location = new Point(tempPositive.Location.X, (tempPositive.Location.Y + tempPositive.Height + 50));
controls.Add("cbPositive" + _noOfComments.ToString(), tempPositive);
pnlBg.Controls.Add(tempPositive);
tempTxtComment.Location = controls["txtComment" + (_noOfComments - 1).ToString()].Location;
tempTxtComment.Size = new Size(437, 57);
tempTxtComment.Location = new Point(tempTxtComment.Location.X, (tempTxtComment.Location.Y + tempTxtComment.Height + 50));
tempTxtComment.Multiline = true;
controls.Add("txtComment" + _noOfComments.ToString(), tempTxtComment);
pnlBg.Controls.Add(tempTxtComment);
}
我的问题是在添加了几个控件之后,组合框(或文本框,我无法确定哪个)随着“行”数量的增加而上升/下降的速度越来越大,如此处所示:
我已经经历了这么多次并且必须错过一些明显的东西,但我需要对它进行排序以便我继续前进!
非常感谢任何帮助。
问候,丹尼尔。
编辑:如果有人感兴趣,这是在实施接受的答案后的结果:
答案 0 :(得分:2)
FlowLayoutPanel
从上到下排列您的行+如果它会溢出主容器,您将拥有滚动条。答案 1 :(得分:0)
将tempCbCategory;tempTxtRoomNo;tempPositive;tempTxtComment
放入用户控件中。或者在设置尺寸之前将tempTxtRoomNo
和tempTxtComment
:AutoSize
设置为False
。