Windows Forms 对话框篇
1,标准对话框
Windows内置的对话框,又叫公用对话框,它们作为组件提供的,并且存在于System.Windows.Forms命名空间中。
手工方式:
- private void button1_Click_1(object sender, EventArgs e)
- {
- ColorDialog dlg = new ColorDialog();
- dlg.Color = Color.Red;
- DialogResult result = dlg.ShowDialog();
- if (result == DialogResult.OK)
- {
- MessageBox.Show("You picked " + dlg.Color.ToString());
- }
- }
组件方式:
- colorDialog1.Color = Color.Red;
- DialogResult result = colorDialog1.ShowDialog();
- if (result == DialogResult.OK)
- {
- MessageBox.Show("You Picked " + this.colorDialog1.Color.ToString());
- }
可见同样的功能,代码减少不少啊。
常用的标准对话框:
【ColorDialog】
颜色选取,返回值System.Drawing.Color类型的Color属性
【FolderBrowserDialog】
选择文件夹,返回值string类型的SelectdPath属性
【FontDialog】
选择字体,返回值System.Drawing.Font类型的Font
【OpenFileDialog】【SaveFileDialog】
打开或保存文件。返回string类型的FileName
【PageSetupDialog】【PrintDialog】【PrintPreviewDialog】
打印相关
2,窗体风格
相关属性:
ControlBox,FormBorderStyle.HelpButton,MaximizeBox,MinimizeBox,ShowIcon,ShowInTaskbar.
在运行中获取窗体时模式还是非模式:
if(this.Modal)
{
this.FormBorderStyle = FormBorderStyle.FixedDialog;
}
else
{
this.FormBorderStyle = FormBorderStyle.Sizable;
}
3,数据交换
可以直接操作:
Form2 dlg = new Form();
dlg.textBox1.Text = “My Name”;
DialogResult result = dlg.ShowDialog();
if(result==DialogResult.OK)
{
//Do Something
}
弊端如果Form2设计改动时(例如改变TextBox为LabelBox),需要改Form1的代码。
这样做比较好:在Form2中添加公共成员,给外部使用。
- public string AppName {
- get { return this.textBox1; }
- set { this.aStr = value; }
- }
调用时候:
Form2 dlg = new Form();
dlg.AppName = “My Name”;
DialogResult result = dlg.ShowDialog();
if(result==DialogResult.OK)
{
//Do Something
}
4,Dialog的返回,处理OK按钮和Cancel按钮
手工处理:
- Form1:
- private void button3_Click(object sender, EventArgs e)
- {
- Form2 dlg = new Form2();
- dlg.Text = "Jor";
- DialogResult result = dlg.ShowDialog();
- if (result == DialogResult.OK)
- {
- MessageBox.Show(dlg.AppName);
- }
- }
- Form2:
- private void button1_Click_1(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.OK;//不用 Close()关闭了,一回事
- }
如果窗体需要返回Cancel是一样的:
- private void button1_Click_1(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.Cancel;
- }
手工方式的方式无法将按钮设置成默认按钮,默认按钮是按Enter键调用的那个按钮,按Esc键的时候,Cancel按钮应该被调用。实现这些很简单,只要配置窗体的AcceptButton和CancelButton就可以了。设置好之后发现Cancel按钮的DialogResult属性被自动设置成DialogResult.Cancel了,所以也没必要像手工方式一样去处理Cancel按钮的Click事件的处理程序了。OK也一样,设置好AccepuButton之后,OK按钮的默认值还是DialogResult.None,所以需要手工修改为DialogResult.OK
5,非模式窗体数据传递
与模式窗体的方式不同,需要用户自定义消息以及事件来完成数据交换的功能
在非模式窗体中自定义消息,以及事件触发的条件等。
代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
- public event EventHandler Accept;//定义事件 ***自定义添加(与主窗体处理函数要对应)***
- private void button1_Click(object sender, EventArgs e)
- {
- if (Accept != null)
- {
- Accept(this, EventArgs.Empty);//按下按钮时触发事件***触发自定义事件***
- }
- }
- }
- }
在主窗体中进行在非模式窗体中定义的事件与处理函数的绑定。当非模式窗体触发该事件时,由绑定的处理函数进行处理。
代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Form2 frm = new Form2();
- frm.Accept += MyAccept;
- frm.Show();
- }
- void MyAccept(object sender, EventArgs e)
- {
- Form2 frm2 = (Form2)sender;
- this.label1.Text = frm2.textBox1.Text;//这里是直接调用,需要设置一下textBox1的公开特性。也可用代理方式,推荐。
- }
- }
- }
注意:对于textBox1.Text的调用,默认情况下是不可以的,因为textBox1在Form1.Designer.cs中定义为private,可以改成public然后像这个例子中的方式进行调用。当然,最好用上面做代理的方式进行调用。
public string AppName { get { return this.textBox1; } set { this.aStr = value; } }
6,数据验证
可以添加Validating事件处理,Validating事件当焦点从一个CausesValidation属性为true的控件转移到另外一个CausesValidation属性为true的控件时发生。如果没有取消Validating事件,窗体会通过Validated事件得到通知。
代码:
- private void textBox1_Validating(object sender, CancelEventArgs e)
- {
- if (((Control)sender).Text.Trim().Length == 2)
- {
- MessageBox.Show("Please enter a name", "Error");
- e.Cancel = true;//是否可以切换焦点
- }
- }
- private void textBox1_Validated(object sender, EventArgs e)
- {
- MessageBox.Show("My Name is ," + this.textBox1.Text, "Thanks");
- }
为了允许用户在没有输入有效数据的情况下点击Cancel按钮来关闭窗体,我们必须将Cancel或Close按钮的CausesValidatin属性设置为true
- private void InitializeComponent()
- {
- this.button1 = new System.Windows.Forms.Button();
- this.textBox1 = new System.Windows.Forms.TextBox();
- this.textBox2 = new System.Windows.Forms.TextBox();
- this.SuspendLayout();
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(190, 198);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(75, 23);
- this.button1.TabIndex = 0;
- this.button1.Text = "button1";
- this.button1.UseVisualStyleBackColor = true;
- this.button1.CausesValidation = false; //注意这里
- this.button1.Click += new System.EventHandler(this.button1_Click_1);
- //
- // textBox1
- //
- //...
}
其他的可以用正则表达式或掩码文本的方式来进行数据验证
数据格式的通知功能(添加ErrorProvider组件):
- private void textBox1_Validating(object sender, CancelEventArgs e)
- {
- string error = null;
- if (((Control)sender).Text.Trim().Length == 0)
- {
- error = "Please enter a name";
- e.Cancel = true;//是否可以切换焦点
- }
- this.errorProvider1.SetError((Control)sender, error);
- }
Windows Forms 对话框篇的更多相关文章
- Windows Forms 窗体篇
1,显示窗体 非模式: Form form = new Form(); form.Show(); 模式: Form form = new Form(); form.Show(); 2,拥有者窗体与附属 ...
- Windows Forms 布局篇
1,锚定功能(Anchor属性) 默认为“Top,Left”,不管窗体大小如果改变,保持相对于窗体左上角的位置. 如果设置为”Top,Bottom,Left,Right”这样,控件的大小将随窗体的大小 ...
- C#Windows Forms窗体、按钮-xdd
1.更换窗体图标 方法:单击窗体,更改icon属性. 2.调整窗体打开时默认位置 方法:单击窗体,更改StartPotion属性. 3.修改窗体大小 方法:单击窗体,更改Size属性. 4.设置窗体的 ...
- Wizard Framework:一个自己开发的基于Windows Forms的向导开发框架
最近因项目需要,我自己设计开发了一个基于Windows Forms的向导开发框架,目前我已经将其开源,并发布了一个NuGet安装包.比较囧的一件事是,当我发布了NuGet安装包以后,发现原来已经有一个 ...
- 用户控件的设计要点 System.Windows.Forms.UserControl
用户控件的设计要点 最近的项目中有一个瀑布图(彩图)的功能,就是把空间和时间上的点量值以图的形式呈现出来,如下图: X坐标为空间,水平方向的一个像素代表一个空间单位(例如50米) Y坐标为时间,垂直方 ...
- Windows Forms (一)
导读 1.什么是 Windows Forms 2.需要学Windows Forms 么? 3.如何手写一个简单的Windows Forms 程序 4.对上面程序的说明 5.Form 类与Control ...
- Windows Forms(二)
导读 1.用VS创建一个Windows Forms程序 2.分析上面的程序 3.Mediator pattern(中介者模式) 4.卡UI怎么办——BackgroundWorker组件 用VS创建一个 ...
- 开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt
首先,大家可以看下这个链接 Windows GUI自动化测试技术的比较和展望 . 这篇文章介绍了Windows中GUI自动化的三种技术:Windows API, MSAA - Microsoft Ac ...
- ystem.Windows.Forms.SplitContainer : ContainerControl, ISupportInitialize
#region 程序集 System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...
随机推荐
- 把书《CUDA By Example an Introduction to General Purpose GPU Programming》读薄
鉴于自己的毕设需要使用GPU CUDA这项技术,想找一本入门的教材,选择了Jason Sanders等所著的书<CUDA By Example an Introduction to Genera ...
- PHP的数组分为两种类型,一种是索引数组,一种是关联数组。有如下关联数组,我们如何获取它的第一个key和value呢?
示例:$items=array('name'=>'sjm','age'=>'26','sex' => '男','location'=>'北京'); //当然用循环然后break ...
- Android Developer:内存分析器
Heap Viewer,Memory Monitor和Allocation Tracker是用来可视化你的app使用内存的补充工具. 使用Memory Monitor Tool来发现是否有不好的内存回 ...
- C++对象模型——Inline Functions(第四章)
4.5 Inline Functions 以下是Point class 的一个加法运算符的可能实现内容: class Point { friend Point operator+(const Poin ...
- python-openpyxl安装
今天在安装openpyxl的时候,一直提示错误,后来才发现仅仅安装它还不够,还需要其他两个库的支持1.安装jdcal2.安装et_xmlfile这两个库安装的方法,都是直接在命令行下面,进入库文件se ...
- Linux下MySQL导入导出数据库
linux下 一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径):1.导出数据和表结构:mysqldump -u用户名 -p密码 数据库名 > 数据库名.sql# ...
- git 版本管理工具说明
$ git init (初始化本地仓库,会生成.git 文件夹 .git 文件夹里存储了所有的版本信息.标记等内容) $ git add . ...
- ASP.NET MVC Web API 学习笔记---第一个Web API程序---近来很多大型的平台都公开了Web API
1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过 ...
- Self-Taught Learning to Deep Networks
In this section, we describe how you can fine-tune and further improve the learned features using la ...
- Codefroces Educational Round 26 837 B. Flag of Berland
B. Flag of Berland time limit per test 1 second memory limit per test 256 megabytes input standard i ...