第 6 章 多窗体工程

21
第 6 第 第第 第第

Upload: stillman-tyrone

Post on 03-Jan-2016

58 views

Category:

Documents


6 download

DESCRIPTION

第 6 章 多窗体工程. 项目 — 添加 Windows 窗体. 每个窗体都被保存为 3 个独立的文件,扩展名分别为 . cs . designer.cs . resx ,窗体的所有信息都保存在这三个文件中. 解决方案资源管理器 文档窗口标签 活动文件按钮. 窗体间的切换. 添加现有项. 项目 — 添加现有项. About 对话框. Displaying an About Form. // Create a new instance of the AboutBox1 form class. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 第 6 章 多窗体工程

第 6 章 多窗体工程

Page 2: 第 6 章 多窗体工程

项目—添加 Windows 窗体

Page 3: 第 6 章 多窗体工程

每个窗体都被保存为 3 个独立的文件,扩展名分别为 .cs .designer.cs .resx ,窗体的所有信息都保存在这三个文件中

Page 4: 第 6 章 多窗体工程

窗体间的切换解决方案资源管理器

文档窗口标签活动文件按钮

Page 5: 第 6 章 多窗体工程

项目—添加现有项

添加现有项

Page 6: 第 6 章 多窗体工程

About 对话框

Page 7: 第 6 章 多窗体工程

创建新实例

Show 方法或者 showdialog 显示对象

Displaying an About Form

// Create a new instance of the AboutBox1 form class. AboutBox1 aboutForm = new AboutBox1();

// Show the new aboutForm object. aboutForm.ShowDialog();

Page 8: 第 6 章 多窗体工程

Close 方法对非模态窗体( Show 方法)与模态窗体( Showdialog 方法)表现不同行为。对于非模态窗体, Close 方法销毁窗体实例,并将其从内存中删除;对模态窗体而言, Close 方法只是将窗体隐藏而已。

使用窗体的方法和事件

Page 9: 第 6 章 多窗体工程

Hide 方法,隐藏某个窗体,但继续将其保留在内存中,为再次显示做准备。

Hide ()

Page 10: 第 6 章 多窗体工程

FormName.Load 和 FormName.Actiated

响应窗体事件

窗体被加载到内存

发生于 Load之后,每次显

示窗体Actiated 事件

再次发生

Page 11: 第 6 章 多窗体工程

事件Load 发生于初次显示窗体之前,该事件只发生一次,除非关闭而

非隐藏窗体。

Activated 显示窗体时都会发生。Paint 当窗体的任何部分被重画时发生Deactivate 发生与窗体不再是活动窗体时FormClosing 窗体即将关闭时发生。FormClosed 窗体关闭之后发生。

窗体事件顺序 P211

Page 12: 第 6 章 多窗体工程

在类中创建属性 只读属性 只写属性 P213 在多个窗体间传递汇总值( static 静态变量)

多窗体过程中的变量和常量

Page 13: 第 6 章 多窗体工程

The Property Block - Example

private string lastNameString;

public string LastName{ get { return lastNameString; } set { lastNameString = value; }}

Name of propertyRetrieve the value of the property

Assign a value to the property

Holds the value of the property

Page 14: 第 6 章 多窗体工程

In some instances, a property can be retrieved by an object but not changed

Write a property block that contains only a get (creates a read-only property)

Read-Only Properties

// Private class-level variable to hold the property value.private decimal payDecimal;

// Property block for read-only property.public decimal Pay{ get { return payDecimal; }}

Page 15: 第 6 章 多窗体工程

A property that can be assigned by an object but not retrieved

Write a property block that contains only a set (creates a write-only property)

Write-only Properties

// Private class-level variable to hold the property value.private decimal hoursDecimal;

// Property block for write-only property.public decimal Hours{ set { hoursDecimal = value; }}

Page 16: 第 6 章 多窗体工程

The Program.cs file with the code added to show the splash form

Making the Splash Form Display First - 2

static void Main(){ Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); SplashForm aSplashForm = new SplashForm(); aSplashForm.ShowDialog(); Application.Run(new MainForm());}

MainForm is the name of the startup form – Change this if you change the startup form

Add these two statements to display the splash form

Page 17: 第 6 章 多窗体工程

添加窗体 设置 StarPosition 属性 CenterScreen ControlBox False

启动界面

Page 18: 第 6 章 多窗体工程

Enabled Inteval

Timer 控件

Page 19: 第 6 章 多窗体工程

Program.cs –Main 方法 Application.Run(new Form1());

使用启动窗体

Page 20: 第 6 章 多窗体工程

Bin\Debug 文件夹中 .exe 文件 前提:安装了 .NET Framework, Visual Studio

2008 默认是 .Net 3.5 修改图标工程—属性—应用程序选项—图标和清单

( C )

在 IDE 之外运行程序

Page 21: 第 6 章 多窗体工程