第 6 章 多窗体工程

Post on 03-Jan-2016

53 views 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

第 6 章 多窗体工程

项目—添加 Windows 窗体

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

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

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

项目—添加现有项

添加现有项

About 对话框

创建新实例

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();

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

使用窗体的方法和事件

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

Hide ()

FormName.Load 和 FormName.Actiated

响应窗体事件

窗体被加载到内存

发生于 Load之后,每次显

示窗体Actiated 事件

再次发生

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

非隐藏窗体。

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

窗体事件顺序 P211

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

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

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

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; }}

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; }}

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

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

启动界面

Enabled Inteval

Timer 控件

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

使用启动窗体

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

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

( C )

在 IDE 之外运行程序