Windows窗体应用程序设计
8.1 窗体设计
8.2 常用的控件设计
8.3 多文档窗体
8.4 窗体设计的事件机制
窗体设计
窗体(Form)是一个窗口或对话框,是存放各种控件(包括
标签、文本框、命令按钮等)的容器,可用来向用户显示信息。
8.1.1 创建Windows窗体应用程序的过程
添加一个窗体的操作步骤是:选择“项目”|“添加Windows
窗体”菜单命令,在出现的 “添加新项”对话框中,选中
“Windows窗体”,输入相应的名称(这里为Form2.cs),单击
“添加”按钮。
一个Windows应用程序可以包含多个窗体。
窗体类型
在C#中,窗体分为如下两种类型:
(1)普通窗体,也称为单文档窗体(SDI),前面所
有创建的窗体均为普通窗体。普通窗体又分为如下两种:
● 模式窗体。这类窗体在屏幕上显示后用户必须响应,
只有在它关闭后才能操作其他窗体或程序。
● 无模式窗体。这类窗体在屏幕上显示后用户可以不
必响应,可以随意切换到其他窗体或程序进行操作。通常
情况下,当建立新的窗体时,都默认设置为无模式窗体。
(2)MDI父窗体,即多文档窗体,其中可以放置普通
子窗体。
窗体的常用属性
1. 布局属性
2. 窗口样式属性
3. 外观样式属性
4. 行为属性
8.1.4 窗体的常用事件
8.1.5 窗体的常用方法
1. Form1窗体:
(1)设计界面
(2)事件过程:
Form1.cs文件:
//引用部分
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Proj8_1
{ public partial class Form1 : Form //从Form类继承Form1窗体
{ public Form1()
{ InitializeComponent();
//Form1类构造函数
//调用初始化方法,其代码在Form1.Designer.cs文件中
myform.ShowDialog(); //以模式窗体方式调用
}
private void button1_Click(object sender, EventArgs e)
{ Form myform = new Form1_1();//定义Form1_1类对象
}
private void button2_Click(object sender, EventArgs e)
{ Form myform = new Form1_2();//定义Form1_2类对象
}
}
}
//以无模式窗体方式调用
myform.Show();
Form1.Designer.cs 文件:
namespace Proj8_1
{ partial class Form1
{ ///
///必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
///清理所有正在使用的资源。
///
///
如果应释放托管资源,为true;
{
{ if (disposing && (components != null))
components.Dispose();
}
base.Dispose(disposing); //调用基类的Dispose()方法
}
protected override void Dispose(bool disposing)
///否则为false。
//重写基类Dispose()方法
#region Windows 窗体设计器生成的代码
///
///设计器支持所需的方法 - 不要
///使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{ this.button1 = new System.Windows.Forms.button();
this.button2 = new System.Windows.Forms.button();
this.SuspendLayout();
// button1
this.button1.Font = new System.Drawing.Font("宋体", 9F,
//初始化方法
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(28, 21);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(117, 33);
this.button1.TabIndex = 0;
this.button1.Text = "调用模式窗体";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);