1 / 42

第七章

第七章. WinForms 基础知识. 回顾. 属性通过使用访问器读写类中的字段,对字段进行保护。 属性分为以下三种不同的类型: 读 / 写属性 只读属性 只写属性 可以在类中定义索引器 ,允许使用下标对该类对象中的数据进行访问 索引器必须总是命名为 this ,因为对它们的访问是通过其所属的对象进行的 委托包含对方法而不是方法名称的引用 C# 中的事件允许一个对象将发生的事件或修改通知其他对象. 目标. 理解 Windows 窗体 使用基本控件如标签、文本、按钮、列表框和组合框 掌握窗体的常用属性和方法. 控件. 简介 3-1. GUI 界面.

kathie
Download Presentation

第七章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第七章 WinForms基础知识

  2. 回顾 • 属性通过使用访问器读写类中的字段,对字段进行保护。 • 属性分为以下三种不同的类型: • 读/写属性 • 只读属性 • 只写属性 • 可以在类中定义索引器,允许使用下标对该类对象中的数据进行访问 • 索引器必须总是命名为 this,因为对它们的访问是通过其所属的对象进行的 • 委托包含对方法而不是方法名称的引用 • C# 中的事件允许一个对象将发生的事件或修改通知其他对象

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

  4. 控件 简介 3-1 GUI界面

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

  6. 简介 3-3 System.Windows.Forms • 简单而强大 • 改善了接口和基类 IntelliSense • 新的管理数据提供程序 • 安全 • 灵活的控件 • 通晓数据 • 向导 WinForms应用程序可能存在多个窗体,用于获取用户输入的数据和向用户显示数据

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

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

  9. 创建 WinForms应用程序 6-3 基础核心命名空间 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 { /// <summary> /// 必需的设计器变量. /// </summary> 提供了大量绘图工具的访问权限 ArrayList、BitArray、Hashtable、Stack、StringCollection和 StringTable 类 大量窗体和控件 从 System.Windows.Forms.Form派生 Visual Studio .NET生成的代码

  10. 创建 WinForms应用程序 6-4 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() 方法

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

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

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

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

  15. 标签

  16. 文本框

  17. 按钮

  18. 列表框

  19. 使用列表框 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 + "已经选择上...","当前选择的值"); } }

  20. 组合框

  21. 使用组合框 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 , "选择的信息"); }

  22. 消息框窗口 2-1 消息框用于显示消息 MessageBox.Show(“[消息文本]");

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

  24. 消息框窗口 2-2

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

  26. 应用程序示例 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 btnAdd_Click(object sender, System.EventArgs e) { }

  27. 应用程序示例 3-2 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(); }

  28. 应用程序示例 3-2 在退出应用程序之前,使用 MessageBox.Show()显示在 str变量中存储选定项的消息框

  29. 应用程序示例 3-3 private void cboDesignation_SelectedIndexChanged (object sender, System.EventArgs e) { MessageBox.Show(“您已经选定了" + this.cboDesignation.SelectedItem.ToString()); }

  30. 应用程序示例 3-3

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

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

  33. 窗体的属性

  34. 窗体的常用方法和事件

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

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

  37. 应用程序示例6-2

  38. 应用程序示例 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.txtEmailId.Text, this.cboSubject.SelectedItem.ToString(), this.txtFeedback.Text); objfrmUserDetails.Show(); }

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

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

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

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

More Related