null--------------基于C#语言--------------基于C#语言主讲教师:夏浩波
本课学时:40课时Windows程序设计1.1 Windows和窗体 1.1 Windows和窗体 本章学习目标:理解 Windows 窗体
使用基本控件如标签、文本、按钮、列
框和组合框
掌握窗体的常用属性和方法1.1 Windows和窗体 GUI界面1.1 Windows和窗体 1.1 Windows和窗体 各种控件属性放置控件的区域1.1 Windows和窗体 1.1 Windows和窗体 WinForms应用程序可能存在多个窗体,用于获取用户输入的数据和向用户显示数据System.Windows.Forms1.1 Windows和窗体 1.1.2 创建 WinForms应用程序 1.1.2 创建 WinForms应用程序 “开始”“程序”“Microsoft Visual Studio.NET 2005”“Microsoft Visual Studio.NET 2005”创建 WinForms应用程序 6-2创建 WinForms应用程序 6-2设计窗口 1.1.2 创建 WinForms应用程序1.1.2 创建 WinForms应用程序using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace SampleProject
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
///
/// 必需的设计器变量.
/// 提供了大量绘图工具的访问权限基础核心命名空间ArrayList、BitArray、Hashtable、Stack、StringCollection 和 StringTable 类 大量窗体和控件从 System.Windows.Forms.Form 派生Visual Studio .NET 生成的代码1.1.2 创建 WinForms应用程序1.1.2 创建 WinForms应用程序private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO:在 InitializeComponent 调用之后
添加任何构造函数代码
//
}
构造函数调用 InitializeComponent() 方法//下面代码见:Form1.Designer.cs文件
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}项目的容器创建 WinForms应用程序创建 WinForms应用程序///
/// 清理所有正在使用的资源。【下面代码: Form1.Designer.cs 】
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}释放系统资源1.1.2 创建 WinForms应用程序1.1.2 创建 WinForms应用程序//下面代码见:program.cs文件
[STAThread]
static void Main()
{
Application.Run(new Form1());
}程序的主入口点 1.1.3 WinForms 中的常用控件1.1.3 WinForms 中的常用控件可视化界面组件统称为控件System.Windows.Forms.Control1.1.3 WinForms 中的常用控件 1.1.3 WinForms 中的常用控件 标签按钮组合框列表框文本框标签标签1.1.3 WinForms 中的常用控件 标签控件按钮控件文本框控件列表控件组合框控件nullprivate void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
linkLabel1.LinkVisited = true;
Form2 newform = new Form2();
newform.Show();
this.Hide();
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
//label2.Visible = true;
label2.Show();
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
label2.Visible = false;
label2.Hide();
} 案例:窗口的打开和关闭文本框文本框1.1.3 WinForms 中的常用控件 按钮按钮1.1.3 WinForms 中的常用控件 null 案例:用户登录设计private void button2_Click(object sender, EventArgs e)
{
clear();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty || textBox2.Text == string.Empty)
{
MessageBox.Show("信息禁止为空!","登录提示");
clear();
return;
}
if (!textBox1.Text.Equals("admin") || !textBox2.Text.Equals("admin"))
{
MessageBox.Show("用户名称或密码为空!", "登录提示");
clear();
return;
}
else
{
MessageBox.Show("欢迎您登录本系统!","消息提示");
clear();
}
}
public void clear()
{
textBox1.Clear();
textBox2.Clear();
textBox2.Focus();
}列表框列表框1.1.3 WinForms 中的常用控件 使用列表框(1)使用列表框(1)private void Form1_Load(object sender, EventArgs e)
{
this.listBox1.Items.Add("软件部");
this.listBox1.Items.Add("硬件部");
this.listBox1.Items.Add("财务部");
this.listBox1.Items.Add("人事部");
}private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("您选择的部门是:"+listBox1.SelectedItem.ToString()+",位列第"+listBox1.SelectedIndex.ToString(),"信息提示");
}使用列表框(2)使用列表框(2)private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox1.Items.Add("软件部");
listBox1.Items.Add("硬件部");
listBox1.Items.Add("财务部");
listBox1.Items.Add("人事部");
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Insert(2,"插入值");
label1.Text = "已经添加" + listBox1.Items.Count.ToString() + "条记录";
}组合框组合框1.1.3 WinForms 中的常用控件 使用组合框使用组合框private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.Items.Add("财务部");
this.comboBox1.Items.Add("产品部");
this.comboBox1.Items.Add("销售部");
this.comboBox1.Items.Add("生产部");
//默认的选择是"产品部"
this.comboBox1.SelectedIndex = 1;
this.comboBox2.Items.Add("财务部");
this.comboBox2.Items.Add("产品部");
this.comboBox2.Items.Add("销售部");
this.comboBox2.Items.Add("生产部");
//默认的选择是"产品部"
this.comboBox2.SelectedIndex = 1;
this.comboBox3.Items.Add("财务部");
this.comboBox3.Items.Add("产品部");
this.comboBox3.Items.Add("销售部");
this.comboBox3.Items.Add("生产部");
//默认的选择是"产品部"
this.comboBox3.SelectedIndex = 1;
}消息框窗口消息框窗口MessageBox.Show(“[消息文本]");消息框用于显示消息Abort, Cancel, Ignore, No, None, Ok, Retry 和 Yes if (MessageBox.Show(“保存文件”,“保存",
MessageBoxButtons.YesNo, MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
//保存文件所用的代码
//保存后的 MessageBox
}1.1.3 WinForms 中的常用控件 消息框窗口消息框窗口nullprivate void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("嘿,这是简单提示!","信息提示");
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("嘿,这是问询提示!","问询提示",MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
label1.Text = "您选择了YES";
}
else
{
label1.Text = "您选择了NO";
}
}private void button3_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("嘿,这是带有图标的问询提示!", "问询提示", MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button3,MessageBoxOptions.RightAlign);
if (result == DialogResult.Yes)
{
label1.Text = "您选择了图标YES";
}
else if(result==DialogResult.Cancel)
{
label1.Text = "您选择了图标取消";
}
else if (result == DialogResult.No)
{
label1.Text = "您选择了图标NO";
}
} 应用程序示例 应用程序示例 解决
资源管理器属性
窗口工具箱应用程序示例应用程序示例private void btnAdd_Click(object sender, System.EventArgs e)
{
}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 cboDesignation_SelectedIndexChanged
(object sender, System.EventArgs e)
{
MessageBox.Show(“您已经选定了" +
this.cboDesignation.SelectedItem.ToString());
}在退出应用程序之前,使用 MessageBox.Show() 显示在 str 变量中存储选定项的消息框null图标系统按钮标题栏控件1.1.4 窗体容器简介 1.1.4 窗体容器简介 1.1.4 窗体容器简介 SDI [单文档界面]
MDI [多文档界面]
1.1.5 窗体的属性 1.1.5 窗体的属性 1.1.5 窗体的常用方法和事件 1.1.5 窗体的常用方法和事件 示例:显示另一窗体示例:显示另一窗体示例:显示另一窗体示例:显示另一窗体[被调用的窗体类] [窗体实例] = new [被调用的窗体类]();[窗体实例].Show();下面,在菜单的单击事件中写下如下的事件。private void menuItem3_Click(object sender, EventArgs e)
{
Form2 Mdichild = new Form2();
Mdichild.MdiParent = this;
Mdichild.Show();
}当然,需要再建立两个窗体对象,form2和form3窗体示例1:在form2窗体中进行如下操作 示例1:在form2窗体中进行如下操作 单击“发送”按钮应用程序示例应用程序示例首先,将form2的comboBox1下拉框填充完毕后,增加load事件
private void Form2_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
textBox3.Text = "";
textBox1.Focus();
}
其次,添加form2的发送信息事件
public void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || comboBox1.Text == "")
{
MessageBox.Show("姓名,或者邮件,或者提交,信息禁止为空!", "信息提示");
}
else
{
this.Hide();
Form3 childform3 = new Form3(this.textBox1.Text,this.textBox2.Text,this.comboBox1.SelectedItem.ToString(),this.textBox3.Text);
childform3.Show();
}
}
最后关闭窗体事件:
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}应用程序示例应用程序示例对于form3窗体而言,在系统初始事件填写如下代码:
public partial class Form3 : Form
{
private string _name;
private string _emailId;
private string _subject;
private string _feedBack;
public Form3(string varName, string varEmail, string varSubject, string varFeedBack)
{
InitializeComponent();
// 在 private 变量中存储值
this._name = varName;
this._emailId = varEmail;
this._subject = varSubject;
this._feedBack = varFeedBack;
// 在列表框中放置值
listBox1.Items.Add("姓名:" + this._name);
listBox1.Items.Add("邮件地址:" + this._emailId);
listBox1.Items.Add("信息主题:" + this._subject);
listBox1.Items.Add("反馈意见:" + this._feedBack);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("感谢您输入的反馈!");
this.Close();
}
}示例2:在MDI父窗口中,子窗口如何彼此之间传递信息? 示例2:在MDI父窗口中,子窗口如何彼此之间传递信息? 代码见下示例3:如何防止重复打开子窗体啊? 示例3:如何防止重复打开子窗体啊? 方法: 直接检测是否已经打开此MDI窗体 // 是否已经打开了?(用循环来判断) foreach (Form childrenForm in this.MdiChildren) { //检测是不是当前子窗体名称 if (childrenForm.Name == "子窗体名称") { //是的话就是把他显示 childrenForm.Visible = true; //并激活该窗体 childrenForm.Activate(); return; } } //下面是打开子窗体 Form1 childrenForm = new Form1(); childrenForm.MdiParent = this; childrenForm.Show(); childrenForm.WindowState = FormWindowState.Maximized; 示例4:另一种窗体之间的传值技巧(一)
——传单个值 示例4:另一种窗体之间的传值技巧(一)
——传单个值 1、先在Form2中定义一个成员变量和一个属性如下: private string form2zhi = null; public string Form2ChuanZhi { get { return form2zhi; } } 2、再在Form3中定义一个成员变量和一个属性如下: private string form3zhi = null; public string Form3ChuanZhi { set { form3zhi = value; } get { return form3zhi; } } 3、双击btn_ChuanZhi在这个事件中写入以下代码(主要是显示Form3窗体和将Form2中的值传过去): Form3 form3 = new Form3(); form3.Form3ChuanZhi = form2zhi;//将值传过去 form3.Show(); 代码见下示例5:另一种窗体之间的传值技巧(二)
——类保存任意值 示例5:另一种窗体之间的传值技巧(二)
——类保存任意值 代码见下总结总结WinForms可用于 Windows 窗体应用程序开发
Windows 窗体控件是从 System.Windows.Forms.Control 类派生的类
标签控件用于显示用户不能编辑的文本或图像
按钮控件提供用户与应用程序交互的最简便方法
组合框控件是列表框控件和文本框控件的组合,用户可以键入文本,也可以从所提供的列表中选择项目
窗体提供了收集、显示和传送信息的界面,是 GUI的重要元素
消息框显示消息,用于与用户交互