1 窗体
1.1 新建窗体
新建项目 选择Windows 窗体应用 .NET Framework版本
重命名:
按字母排序,修改标题名称:
1.2 windows程序与窗口的关系
windows程序也称为winform程序、桌面程序、C/S(Client/Sever)程序
程序启动Application.Run(new FrmMain());
Application.Run是一个默认的类,.Run表示程序运行,FrmMain()是主窗口,程序运行启动过程首先打开FrmMain,在这个窗体也可以调用其它子窗体。如果这个窗体关闭则整个程序退出。Windows窗体继承Form
选中窗口,单击F7
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1 //主画面的命名空间
{
public partial class FrmMain: Form //Form是一个窗体类,创建的任何窗体都必须具有Form的属性、方法、事件
{
public FrmMain()
{
InitializeComponent();
}
}
}
- 对窗体进行的更改都存在Desigener中,是后台自动生成的。 FrmMain会自动调用这个程序(InitializeComponent();)。因此Desigener中添加的任何控件都是窗体程序的成员变量,并且在窗体的构造方法中创建了对象。
1.3 windows程序的事件
事件:比较重要的事情(event)
事件机制:windows操作系统整个任务的操作都是基于事件机制完成的(消息驱动机制)
事件机制模型(四个条件)
【1】定义事件:规定事件是什么类型的操作,比如Click 表示点击操作;
【2】处理事件:事件响应,用户对控件执行的动作的时候,程序具体业务的处理,处理方法必须与事件委托规定的参数类型和个数一样;
【3】关联事件:将处理事件的方法与对应的事件关联;
【4】激发事件:由用户规定的去执行事件规定的动作;
Main:
//事件响应
private void button1_Click(object sender, EventArgs e)
{
//调用对话框,做事件响应当button1_Click执行MessageBox.
MessageBox.Show("事件机制","事件信息",MessageBoxButtons.OKCancel);
}
Main.Designer :
//关联事件,只起到一个中转的作用
this.button1.Click += new System.EventHandler(this.button1_Click);//把事件委托给EventHandler
事件委托:
public delegate void EventHandler(object sender, EventArgs e);
1.3.1 定义事件与处理事件
step1:主画面FrmMain新建6个按钮
按钮1的属性设置:Tag:BTN1_数据标题1 ,TEXT:数据1
按钮2的属性设置:Tag:BTN2_数据标题2 ,TEXT:数据2
按钮3的属性设置:Tag:BTN3_数据标题3 ,TEXT:数据3
按钮4的属性设置:Tag:btn4_数据标题4 ,TEXT:数据4
按钮5的属性设置:不设置
按钮6的属性设置:Tag:btn6_数据标题6 ,TEXT:数据6
TEXT是可视化界面直接现实的额数据,Tag主要是用来对按钮进行分类的属性,方便遍历按钮。FrmMain.Designer可以直接在代码层修改里面的属性。FrmMain.Designer是系统默认生成的,因此不建议在里面修改程序。FrmMain中所有的控件属性代码都在FrmMain.Designer中。
FrmMain.Designer中的方法 private void InitializeComponent()在FrmMain.cs中被使用。
FrmMain.Designer.cs:
using System.Windows.Forms;
namespace WindowsFormsApp1
{
partial class FrmMain
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()//在这里面写代码和在public FrmMain()里面写是一样的
{
this.components = new System.ComponentModel.Container();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// contextMenuStrip1
//
this.contextMenuStrip1.ImageScalingSize = new System.Drawing.Size(24, 24);
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(61, 4);
//
// button1
//
this.button1.Location = new System.Drawing.Point(20, 25);
this.button1.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(59, 21);
this.button1.TabIndex = 1;
this.button1.Tag = "BTN1_数据1标题";
this.button1.Text = "数据1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(20, 57);
this.button2.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(59, 21);
this.button2.TabIndex = 2;