1、窗体的设计视图状态时通过控件工具箱上的工具按钮插入相应的控件(文本框、组合框等等)
2、然后双击控件打开属性窗口
3、对控件进行进一步的设置
4、通过指定数据选项卡上的记录源来把该控件绑定到窗体数据。
用代码向窗体添加控件步骤如下(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)运行效果
窗体启动后
点击“退出”按钮后,窗口关闭。
用代码向窗体添加控件步骤如下
(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)运行效果
窗体启动后
点击“退出”按钮后,窗口关闭。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)