Presentation is loading. Please wait.

Presentation is loading. Please wait.

第13章 WinForms基础知识.

Similar presentations


Presentation on theme: "第13章 WinForms基础知识."— Presentation transcript:

1 第13章 WinForms基础知识

2 目标 理解 Windows 窗体 使用基本控件如标签、文本、按钮、列表框和组合框 掌握窗体的常用属性和方法

3 简介 3-1 GUI界面 控件

4 简介 3-2 各种控件 属性 放置控件的区域

5 WinForms应用程序可能存在多个窗体,用于获取用户输入的数据和向用户显示数据
简介 3-3 System.Windows.Forms WinForms应用程序可能存在多个窗体,用于获取用户输入的数据和向用户显示数据

6 创建 WinForms应用程序 6-1 “开始”“程序”“Microsoft Visual Studio.NET 2003”“Microsoft Visual Studio.NET 2003”

7 创建 WinForms应用程序 6-2 设计窗口

8 创建 WinForms应用程序 6-3 从 System.Windows.Forms.Form 派生
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace SampleProject { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form /// 必需的设计器变量. 基础核心命名空间 提供了大量绘图工具的访问权限 ArrayList、BitArray、Hashtable、Stack、StringCollection 和 StringTable 类 大量窗体和控件 从 System.Windows.Forms.Form 派生 Visual Studio .NET 生成的代码

9 创建 WinForms应用程序 6-4 构造函数调用 InitializeComponent() 方法
private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 InitializeComponent(); // TODO:在 InitializeComponent 调用之后 添加任何构造函数代码 } 项目的容器 private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } 构造函数调用 InitializeComponent() 方法

10 创建 WinForms应用程序 6-5 /// <summary> /// 清理所有正在使用的资源。
protected override void Dispose( bool disposing ) { if( disposing ) if(components != null) components.Dispose(); } base.Dispose( disposing ); 释放系统资源

11 创建 WinForms应用程序 6-6 [STAThread] static void Main() {
程序的主入口点 [STAThread] static void Main() { Application.Run(new Form1()); }

12 System.Windows.Forms.Control
WinForms 中的常用控件 2-1 System.Windows.Forms.Control 可视化界面组件统称为控件 System.Windows.Forms Control ButtonBase Button CheckBox Label ListControl ComboBox ListBox TextBoxBase TextBox RadioButton

13 WinForms 中的常用控件 2-2 标签 文本框 组合框 列表框 按钮

14 标签 属性 说明 Text 该属性用于设置或获取与该控件关联的文本 方法 说明 Hide
隐藏控件,调用该方法时,即使 Visible 属性设置为 True,控件也不可见 Show 相当于将控件的 Visible 属性设置为 True 并显示控件 事件 Click 用户单击控件时将发生该事件

15 文本框 属性 说明 方法 说明 事件 MaxLength 可在文本框中输入的最大字符数 Multiline 表示是否可在文本框中输入多行文本
Passwordchar 机密和敏感数据,密码输入字符 ReadOnly 文本框中的文本为只读 Text 检索在控件中输入的文本 方法 说明 Clear 删除现有的所有文本 事件 KeyPress 用户按一个键结束时将发生该事件

16 按钮 属性 说明 方法 说明 事件 Enabled 确定是否可以启用或禁用该控件 PerformClick
Button 控件的 Click 事件 事件 Click 单击按钮时将触发该事件

17 列表框 属性 方法 事件 Items SelectionMode SelectedIndex SelectedItem
SelectedItems Text 方法 ClearSelected 事件 SelectedIndexChanged

18 使用列表框 private void frmUserAdd_Load(object sender, System.EventArgs e)
{ this. lstCurrDeptName.Items.Add("软件部"); this. lstCurrDeptName.Items.Add("硬件部"); this. lstCurrDeptName.Items.Add("财务部"); this. lstCurrDeptName.Items.Add("人事部"); } private void cmdOK_Click(object sender, System.EventArgs e) { //注意SelectedIndex的值,第一个应该为0 if (this. lstCurrDeptName.SelectedIndex ==0) MessageBox.Show(this. lstCurrDeptName.Text + "已经选择上...","当前选择的值"); }

19 组合框 属性 说明 方法 说明 DropDownStyle ComboBox 控件的样式 MaxDropDownItems
下拉区显示的最大项目数 方法 说明 Select 在 ComboBox 控件上选定指定范围的文本

20 使用组合框 private void frmUserAdd_Load(object sender, System.EventArgs e)
{ …… this.cboDesig.Items.Add("总裁"); this. cboDesig.Items.Add("副总裁"); this. cboDesig.Items.Add("首席执行官"); this. cboDesig.Items.Add("经理"); //默认的选择是"产品部" this. cboDesig.SelectedIndex = 1; } private void cboDesig_SelectedIndexChanged(object sender, System.EventArgs e) { MessageBox.Show( "选择的是第“ + (this.cboDesig.SelectedIndex+1).ToString() , "选择的信息"); MessageBox.Show( "选择的职务是“ + this.cboDesig.Text , "选择的信息"); }

21 Abort, Cancel, Ignore, No, None, Ok, Retry 和 Yes
消息框窗口 2-1 消息框用于显示消息 MessageBox.Show(“[消息文本]"); if (MessageBox.Show(“保存文件”,“保存", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes) { //保存文件所用的代码 //保存后的 MessageBox } Abort, Cancel, Ignore, No, None, Ok, Retry 和 Yes

22 消息框窗口 2-2 …… 重载方法 Show(string text);
Show(string text, string caption); Show(string text, string caption, MessageBoxButtons buttons); Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon); ……

23 应用程序示例 3-1 属性 窗口 解决方案资源管理器 工具箱

24 应用程序示例 3-2 private void btnAdd_Click(object sender, System.EventArgs e) { this.txtEmpName.Enabled=true; this.txtAddress.Enabled=true; this.cboDesignation.Enabled=true; this.lstCurrDeptName.Enabled=true; } private void btnCancel_Click(object sender, System.EventArgs e) { this.txtEmpName.Text=""; this.txtAddress.Text=""; this.cboDesignation.Text=“经理"; } private void btnExit_Click (object sender, System.EventArgs e) { string str=""; for(int ctr=0;ctr<=this.lstCurrDeptName.SelectedItems.Count-1; ctr++) str += "\n"+this.lstCurrDeptName.SelectedItems[ctr].ToString(); MessageBox.Show(“选定的项目为\n" +str); Application.Exit(); } private void btnAdd_Click(object sender, System.EventArgs e) { }

25 在退出应用程序之前,使用 MessageBox.Show() 显示在 str 变量中存储选定项的消息框
应用程序示例 3-3 在退出应用程序之前,使用 MessageBox.Show() 显示在 str 变量中存储选定项的消息框 private void cboDesignation_SelectedIndexChanged (object sender, System.EventArgs e) { MessageBox.Show(“您已经选定了" + this.cboDesignation.SelectedItem.ToString()); }

26 窗体容器简介 2-1 System.Windows.Forms 标题栏 系统按钮 图标 Control ScrollableControl
ContainerControl Form 标题栏 系统按钮 图标 控件

27 窗体容器简介 2-2 SDI [单文档界面] MDI [多文档界面] 模式窗口

28 窗体的属性 属性 ActiveForm CancelButton ControlBox FormBorderStyle HelpButton
KeyPreview MainMenu Modal ShowInTaskbar WindowState

29 窗体的常用方法和事件 方法 Activate LayoutMdi ShowDialog 事件 Activated Closed
Closing Load

30 [被调用的窗体类] [窗体实例] = new [被调用的窗体类]();
显示另一窗体 [被调用的窗体类] [窗体实例] = new [被调用的窗体类](); [窗体实例].Show(); private void cmdShow_Click(object sender , System.EventArgs e) { frmA A = new frmA(); A.Show(); }

31 应用程序示例 6-1 单击“发送”按钮

32 应用程序示例 6-2 控件 名称 控件 名称 Form frmFeedBack TextBox (姓名) txtName
txt Id ComboBox (主题) cboSubject TextBox (反馈) txtFeedback MultiLine 属性 True Button (发送) btnSend Button (关闭) btnClose 控件 名称 Form frmUserDetails ListBox lstvalues MultiColumn true Button btnClose

33 应用程序示例 6-3 private void frmFeedBack_Load(object sender, System.EventArgs e) { this.cboSubject.Items.Add(“一般反馈"); this.cboSubject.Items.Add(“设计反馈"); this.cboSubject.Items.Add(“颜色反馈"); this.cboSubject.Items.Add(“字体反馈"); } private void btnSend_Click(object sender, System.EventArgs e) frmUserDetails objfrmUserDetails = new frmUserDetails(this.txtName.Text, this.txt Id.Text, this.cboSubject.SelectedItem.ToString(), this.txtFeedback.Text); objfrmUserDetails.Show();

34 应用程序示例 6-4 private void btnClose_Click(object sender, System.EventArgs e) { this.Close(); } private void frmFeedBack_Closed(object sender, System.EventArgs e) MessageBox.Show(“感谢您输入的反馈!");

35 应用程序示例 6-5 public class frmUserDetails : System.Windows.Forms.Form {
……………………………………… private string _name; private string _ Id; private string _subject; private string _feedBack; }

36 应用程序示例 6-6 public frmUserDetails(string varName, string var , string varSubject, string varFeedBack) { InitializeComponent(); // 在 private 变量中存储值 this._name = varName; this._ Id = var ; this._subject = varSubject; this._feedBack = varFeedBack; // 在列表框中放置值 this.lstValues.Items.Add(this._name); this.lstValues.Items.Add(this._ Id); this.lstValues.Items.Add(this._subject); this.lstValues.Items.Add(this._feedBack); }

37 总结 WinForms可用于 Windows 窗体应用程序开发 Windows 窗体控件是从 System.Windows.Forms.Control 类派生的类 标签控件用于显示用户不能编辑的文本或图像 按钮控件提供用户与应用程序交互的最简便方法 组合框控件是列表框控件和文本框控件的组合,用户可以键入文本,也可以从所提供的列表中选择项目 窗体提供了收集、显示和传送信息的界面,是 GUI的重要元素 消息框显示消息,用于与用户交互

38 高级WinForms控件 NumericUpDown控件 ProgressBar(进度条)控件 ListView(列表视图)控件
TreeView(树形视图)控件 Splitter(分隔)控件 TabControl(标签控件)

39 菜单设计

40 工具栏和状态栏

41 对话框 模式对话框(Model) 非模式对话框(Modeless)
区别:模式对话框弹出之后,出现在应用程序所有窗口之上,用户不能访问除这个对话框及其控件之外的其他窗口。 模式对话框使用ShowDialog方法显示; 非模式对话框使用Show方法显示。

42 多文档界面应用程序 单文档界面应用程序(SDI Application) 多文档界面应用程序(MDI Application)
区别:单文档界面应用程序只有一个主窗体; 多文档界面应用程序主窗体内包括多个子窗体,每个子窗体显示一个文档。

43 多文档界面应用程序的设计工作 创建主窗体以及主窗体上的控件; 创建子窗体以及子窗体上的控件; 实现子窗体的功能; 实现主窗体的功能;
实现父子窗口交互功能。

44 多文档界面应用程序的主窗口 设置其属性IsMDIContainer为true。


Download ppt "第13章 WinForms基础知识."

Similar presentations


Ads by Google