Windows 窗体

学习目标:

对窗体的理解

Windows窗体应用程序

Windows窗体的属性、方法和事件

各种控件的使用

窗体

窗体可以是标准窗体,多文档界面(MDI)窗体,对话框或图形化的显示界面。窗体是对象,这些对象公开定义其外观的属性、其行为的方法、用于用户交互的事件。通过设置窗体的属性以及编写响应其事件的代码,可自定义该对象以满足应用程序的要求。Windows窗体其实也是控件,因为它是从Control类中继承的。

Control类为定义窗体及控件的基类。Form为窗体类,用来构造窗体,其他标准Windows控件类均派生于Control类

Windows窗体应用程序:

 

使用Windows窗体,首先要创建Windows窗体应用程序。在创建项目的时候选择Windows应用程序,这样就能创建Windows窗体应用程序。

通过项目创建的Windows应用程序:

通过编程创建一个Windows窗体应用程序:

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Windows.Forms;//引用Windows窗体类
5 namespace WindowsForms//命名空间名
6 {
7 //声明一个从Form类继承的名为myForm的类
8 class myForm : System.Windows.Forms.Form
9 {
10 //为myForm类创建默认的构造函数
11 public myForm()
12 {
13 }
14 static void Main()
15 {
16 //Application.Run()负责启动标准的应用程序的消息循环
17 Application.Run(new myForm());
18 }
19 }
20 }

运行结果:得到是一个没有标题的空白窗体,没有实际的功能,只是一个Windows窗体。

在此程序中Application.Run()方法负责启动标准的应用程序的消息循环,Application类提供一些静态属性和方法用于控制应用程序的启动和停止过程,访问应用程序处理Windows 消息。Run方法有三种重载方法,在此是第三种重载方法,就是把窗体对象作为参数,这表示窗体关闭,应用程序结束。

控件的创建以及在Windows窗体中添加控件:

View Code
 1 using System;
2 using System.ComponentModel;
3 using System.Drawing;
4 using System.Windows.Forms;
5
6 namespace WindowsForms
7 {
8 public class Form1 : Form
9 {
10 public Button button1;//声明Button按钮事件
11 //在构造函数中设置Size、Location和Text属性
12 public Form1()
13 {
14 button1 = new Button();
15 button1.Size = new Size(70, 30);
16 button1.Location = new Point(30, 30);
17 button1.Text = "Clik me";
18 //将按钮添加到窗体中
19 this.Controls.Add(button1);
20 button1.Click += new EventHandler(button1_Click);
21 }
22 //按钮事件
23 void button1_Click(object send, EventArgs e)
24 {
25 MessageBox.Show("Hello World");
26 }
27 static void Main()
28 {
29 Application.Run(new Form1());
30 }
31 }
32 }

运行程序后,在Windows窗体中出现一个Button控件,单击Button,弹出一个对话框,显示“Hello World”,如图:

控件

标准控件和组件

控件是包含在窗体对象内的对象。每种控件都具有自己的属性集、方法和事件,以使该控件适合于特定用途。

.NET中有多种控件包括:

文本控件:Lable控件(是最常用的Windows窗体控件)、TextBox控件(显示和输入多行文本)、RichTextBox控件(提供具有打开和保存文件的功能的方法)、MashedTextBox控件(增强型的TextBox控件,支持用于接收或拒绝用户输入的声明性语法)

举例说明RichTextBox控件的使用

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.IO;
9 namespace WindowsApplication1
10 {
11 public partial class Form1 : Form
12 {
13 public Form1()
14 {
15 InitializeComponent();
16 }
17 private void button1_Click(object sender, EventArgs e)
18 {
19 SaveFileDialog sa = new SaveFileDialog();
20 sa.Filter = "文本文件(.txt)|*.txt|C#文件(.cs)|*.cs|所有文件(*.*)|*.*";//文件另存类型
21 sa.FilterIndex = 2;
22 sa.RestoreDirectory = true;
23 if (sa.ShowDialog() == DialogResult.OK)
24 {
25 string s = sa.FileName;
26 StreamWriter sw = File.AppendText(s);
27 sw.Write(this.richTextBox1.Text);
28 sw.Write("Hello World");
29 sw.Flush();
30 sw.Close();
31 }
32 }
33 private void button2_Click(object sender, EventArgs e)
34 {
35 this.richTextBox1.Text = "";//重置richTextBox1的内容
36 }
37 }
38 }

运行后如下:在RichTextBox中输入文本信息,单击保存按钮文件以.cs格式保存,单击重置按钮将会清空RichTextBox中的文本信息。

按钮控件:Button控件(表示简单的命令按钮,派生于ButtonBase类)、RadioButton控件(是一个单选按钮,一般用作一个组,有时称为选项按钮)、CheckBox控件(用来显示一个复选框,可以用来表示某个选项是否被选中,可多选)

列表控件(派生于ListContol类):ComboBox控件(单选框列表)、ListBox控件多选(多选框列表)、CheckedListBox控件(复选框列表,可以说是ListBox空间的派生控件,因此继承了其很多方法和属性)

举例说明按钮控件常用的属性和事件

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 namespace WindowsApplication2
9 {
10 public partial class Form1 : Form
11 {
12 public Form1()
13 {
14 InitializeComponent();
15 }
16 //定义两个字符串变量
17 private String strGender = "";
18 private String strLike = "";
19 //单击Button事件
20 private void button1_Click(object sender, EventArgs e)
21 {
22 Label1.Text = "您的姓名是:" + textBox1.Text + "," + "爱好是:" + strLike + "," + "性别是:" + strGender;
23 }
24 private void radioButton1_CheckedChanged(object sender, EventArgs e)
25 {
26 strGender = "";
27 }
28 private void radioButton2_CheckedChanged(object sender, EventArgs e)
29 {
30 strGender = "";
31 }
32 //上网
33 private void chk1_CheckedChanged(object sender, EventArgs e)
34 {
35 if (chk1.Checked)
36 {
37 strLike = strLike + chk1.Text;
38 }
39 else
40 {
41 strLike.Replace(chk1.Text + "</br>", "");
42 strLike.Trim();
43 }
44 }
45 //阅读
46 private void chk2_CheckedChanged(object sender, EventArgs e)
47 {
48 if (chk2.Checked)
49 {
50 strLike = strLike + chk2.Text;
51 }
52 else
53 {
54 strLike.Replace(chk2.Text + "</br>", "");
55 strLike.Trim();
56 }
57 }
58 //爬山
59 private void chk3_CheckedChanged(object sender, EventArgs e)
60 {
61 if (chk3.Checked)
62 {
63 strLike = strLike + chk3.Text;
64 }
65 else
66 {
67 strLike.Replace(chk3.Text + "</br>", "");
68 strLike.Trim();
69 }
70 }
71 }
72 }

ListView控件:通过此控件,可将项目组成或不带有列表头的列,并显示伴随图标和文本

容器控件(是指内部可以包含控件或其他内容的控件)Windows窗体中的主要包括容器控件:Panel(派生于ScrollableControl)、TabControl(允许把相关的空间组合到一系列的tab页面上)、SplitContainer(可以看作是一个复合体,它是由一个可移动的拆分条分割的两个面板)、FlowLayoutPanel(控件在水平或垂直方向排列其内容)和TabLayoutPanel(在网格中排列其内容)控件

 

ImageList组件(图像列表)和PictureBox控件(用于显示图像)

举例:

新建Windows窗体添加相应控件并设置相应的属性

事件:

View Code
1 private void Form1_Load(object sender, EventArgs e)
2 {
3 label2.Image = imageList1.Images[0];
4 label3.Image = imageList1.Images[1];
5 label4.Image = imageList1.Images[2];
6 label5.Image = imageList1.Images[3];
7 }

运行后如图:

DataTimePicker控件(表示日期)ProgressBar控件(通过在水平条中显示适当数目的矩形来表示进程的进度) 

其他控件:

ToolStrip控件(用于将用户界面元素组合到工具栏、状态栏和菜单中)

MenuStrip控件(可以轻松创建Microsoft Office中那样的菜单)

ContextMenuStrip控件(快捷菜单,也称为上下文)

ToolStripContainer控件(在ToolStripContainer控件的左侧、右侧、顶部和底部都有用来放置和漂浮ToolStrip、MenuStrip和ContextMenuStrip控件的面板)

ErrorProvider组件(可以对窗体或控件上的用户输入进行验证)和HelpProvider组件(允许挂起控件、显示帮助主题)









posted @ 2012-02-01 01:02  翼灵绝  阅读(666)  评论(0编辑  收藏  举报