Form.Dock(公共属性)(从 Control 继承)获取或设置控件停靠到父容器的哪一个边缘。 下面例子虽然是控件停靠,但用法是一样的。private void AddMyGroupBox() { // Create a GroupBox and add a TextBox to it. GroupBox groupBox1 = new GroupBox(); TextBox textBox1 = new TextBox(); textBox1.Location = new Point(15, 15); groupBox1.Controls.Add(textBox1); // Set the Text and Dock properties of the GroupBox. groupBox1.Text = "MyGroupBox"; groupBox1.Dock = DockStyle.Top; // Disable the GroupBox (which disables all its child controls) groupBox1.Enabled = false; // Add the Groupbox to the form. this.Controls.Add(groupBox1); }
下面例子虽然是控件停靠,但用法是一样的。private void AddMyGroupBox()
{
// Create a GroupBox and add a TextBox to it.
GroupBox groupBox1 = new GroupBox();
TextBox textBox1 = new TextBox();
textBox1.Location = new Point(15, 15);
groupBox1.Controls.Add(textBox1); // Set the Text and Dock properties of the GroupBox.
groupBox1.Text = "MyGroupBox";
groupBox1.Dock = DockStyle.Top; // Disable the GroupBox (which disables all its child controls)
groupBox1.Enabled = false; // Add the Groupbox to the form.
this.Controls.Add(groupBox1);
}
在mainform.cs中添加代码:
Form1 form1 = new Form1();
this.Controls.Add(f1);
f1.Dock = DockStyle.Left;运行时提示有错,错误原因为:不能将顶级控件添加到一个控件上。不知道是不是我理解错了44404的意思