留言与评论(共有 0 条评论) |
发布时间:2020-04-12 08:41:15
用代码向窗体添加控件步骤如下
(1)实例化一个控件;
(2)设置控件实例属性;
(3)将控件实例添加到窗体的Controls集合中
【示例】用代码向窗体添加一个命令按钮,单击这个按钮关闭窗口并退出
(1)在Visual Studio中新建一个“Windos 窗体应用程序”
(2)窗体代码Form1.cs如下:
using System;using 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)运行效果
窗体启动后
点击“退出”按钮后,窗口关闭。
留言与评论(共有 0 条评论) |
全站搜索