如何:在运行时添加 Windows 窗体控件
更新:2007 年 11 月
此示例在运行时创建 Windows 窗体 TextBox 控件的新实例。
示例
private void Form1_Load(object sender, System.EventArgs e)
{
TextBox MyTextBox = new System.Windows.Forms.TextBox();
MyTextBox.Location = new System.Drawing.Point(64, 40);
MyTextBox.Name = "MyTextBox";
MyTextBox.Size = new System.Drawing.Size(88, 21);
MyTextBox.TabIndex = 0;
MyTextBox.Text = "MyTextBox";
Controls.Add(MyTextBox);
// Adding the DoubleClick event.
MyTextBox.DoubleClick += new EventHandler(MyTextBox_DoubleClick);
}
private void MyTextBox_DoubleClick(object sender, System.EventArgs e)
{
MessageBox.Show("MyTextBox doubleClick event.");
}
编译代码
- 本示例需要引用 System.Windows.Forms 命名空间。