
(1)实例化一个控件;
(2)设置控件实例属性;
(3)将控件实例添加到窗体的Controls集合中
【示例】用代码向窗体添加一个命令按钮,单击这个按钮关闭窗口并退出
(1)在Visual Studio中新建一个“Windos 窗体应用程序”
(2)窗体代码Form1.cs如下:
using Systemusing System.Collections.Generic
using System.Windows.Forms
using System.Drawing
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent()
//实例化一个命令按钮
Button btn = new Button()
//设置命令按钮的属性
btn.Location = new Point(50, 50)
btn.Size = new Size(80, 25)
btn.Text = "退出"
btn.Click += btn_Click
//添加到窗口的Controls集合中
this.Controls.Add(btn)
}
void btn_Click(object sender, EventArgs e)
{
this.Close()
}
}
}
(3)运行效果
窗体启动后
点击“退出”按钮后,窗口关闭。
panel1.Controls.Add(new userControl())引用的时候直接:
userControl uc=(userControl)panel1.Controls[i]
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)