1 / 55

第三章 WinForms 基础知识

第三章 WinForms 基础知识. 技能目标. 理解 Windows 窗体 使用基本控件如标签、文本、按钮、列表框和组合框 掌握窗体的常用属性和方法 使用 WinF orms 中的高级控件 单选按钮 图片框 选项卡控件 滚动条 进度条. 控件. 简介 2-1. GUI 界面. 各种控件. 属性. 放置控件的区域. 简介 2-2. System.Windows.Forms. 简单而强大 改善了接口和基类 IntelliSense 新的管理数据提供程序 安全 灵活的控件 通晓数据 向导.

maleah
Download Presentation

第三章 WinForms 基础知识

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. 技能目标 • 理解 Windows 窗体 • 使用基本控件如标签、文本、按钮、列表框和组合框 • 掌握窗体的常用属性和方法 • 使用WinForms中的高级控件 • 单选按钮 • 图片框 • 选项卡控件 • 滚动条 • 进度条

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

  4. 各种控件 属性 放置控件的区域

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

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

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

  8. 创建 WinForms应用程序 6-3 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } } } 基础核心命名空间 提供了大量绘图工具的访问权限 ArrayList、BitArray、Hashtable、Stack、StringCollection 和 StringTable 类 大量窗体和控件 从System.Windows.Forms.Form 派生 Visual Studio .NET 生成的代码

  9. 创建 WinForms应用程序 6-4 #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.SuspendLayout(); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(333, 266); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; 项目的容器 构造函数调用InitializeComponent()方法

  10. 创建 WinForms应用程序 6-5 /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } 释放系统资源

  11. 创建 WinForms应用程序 6-6 static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } 程序的主入口点

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

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

  14. 标签

  15. 文本框

  16. 按钮

  17. 列表框

  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. 组合框

  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 = 0; } private void cboDesig_SelectedIndexChanged(object sender, System.EventArgs e) { MessageBox.Show( "选择的是第“ + (this.cboDesig.SelectedIndex+1).ToString() , "选择的信息"); MessageBox.Show( "选择的职务是“ + this.cboDesig.Text , "选择的信息"); }

  21. 消息框窗口 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

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

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

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

  28. 窗体的属性

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

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

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

  32. 应用程序示例 6-2

  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.txtEmailId.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 _email; private string _subject; private string _feedBack; ……………………………………… ……………………………………… }

  36. 应用程序示例 6-6 public frmUserDetails(string Name, string Email, string Subject, string FeedBack) { InitializeComponent(); // 在 private 变量中存储值 this._name = Name; this._email = Email; this._subject = Subject; this._feedBack = FeedBack; // 在列表框中放置值 this.lstValues.Items.Add(this._name); this.lstValues.Items.Add(this._email); this.lstValues.Items.Add(this._subject); this.lstValues.Items.Add(this._feedBack); }

  37. 单选按钮 • Windows窗体单选按钮控件以组的形式使用 • 单选按钮允许用户从多个选项中选择一个选项

  38. 图片框 • 图片框控件表示可用于显示图像的 Windows 图片框控件 图片框 是一种图形显示控件 显示位图、元文件、图标、JPEG、GIF 或 PNG 等格式的图形

  39. 选项卡控件 • 在 Windows 应用程序中,选项卡用于将相关的控件集中在一起,放在一个页面中 • 选项卡控件用于显示多个选项卡,其中每个选项卡均可包含图片和其他控件 • 选项卡相当于另一个窗体,可以容纳其他控件 选项卡控件

  40. 选项卡控件的属性

  41. 选项卡控件的事件

  42. 滚动条 • 滚动条的属性和事件如下: • 用于上下或者左右滚动整个窗口或者文档 • 在 Microsoft Word 或 Excel 中,为浏览多页面的文档提供了滚动条 滚动条 垂直滚动条 水平滚动条 带有滚动条的 MS Word 文档窗口

  43. 进度条 • 用于指示操作的进度、完成的百分比 • 外观是排列在水平条中的一定数目的矩形

  44. 进度条的属性和方法

  45. 应用程序示例 9-1 • 使用窗体接受职员的个人信息和职业信息 • 将使用单选按钮、图片框和选项卡控件 • 应用程序提供有两个选项卡页 • 第一个选项卡页显示个人信息的文本框

  46. 应用程序示例 9-2 • 第二个选项卡页显示职员信息的文本框

  47. 应用程序示例 9-3 • 新建一个 Windows 应用程序,并将其命名为 EmployeeForm • 单击“视图”“解决方案资源管理器” • 将 Form1.cs 文件更改为 frmEmployees.cs • 单击“视图”“属性” • 将窗体的 Name属性更改为 frmEmployee,并将 Text属性更改为职员申请表 • 单击“视图”“工具箱” 以调用工具箱窗口 • 在窗体中添加一个选项卡控件 • 按以下幻灯片所示更改属性

  48. 应用程序示例 9-4 tabProfessional 选项卡页: tabPersonal 选项卡页

  49. 应用程序示例 9-5 • 在“下一步”按钮的 Click 事件中添加以下代码 private void tabMain_Click(object sender, System.EventArgs e) { if (tabMain.SelectedIndex ==0) { vsbPersonal.Visible =false; tabMain.SelectedIndex = 1; } else if (tabMain.SelectedIndex ==1) { vsbPersonal.Visible = true; tabMain.SelectedIndex = 0; } } private void btnNext_Click(object sender, System.EventArgs e) { if (tabMain.SelectedIndex ==0) { vsbPersonal.Visible = false; tabMain.SelectedIndex = 1; } } • 在选项卡控件的 Click 事件中编写以下代码。 在选项卡之间导航

  50. 应用程序示例 9-6 • 在“上一步”按钮的 Click 事件中添加以下代码 private void btnBack_Click(object sender, System.EventArgs e) { if (tabMain.SelectedIndex ==1) { vsbPersonal.Visible = true; tabMain.SelectedIndex = 0; } } private void btnDone_Click(object sender, System.EventArgs e) { MessageBox.Show(“感谢您”,“信息"); Application.Exit(); } 在“完成”按钮中添加以下代码

More Related