1,标准对话框

Windows内置的对话框,又叫公用对话框,它们作为组件提供的,并且存在于System.Windows.Forms命名空间中。

手工方式:

  1. private void button1_Click_1(object sender, EventArgs e)
  2. {
  3. ColorDialog dlg = new ColorDialog();
  4. dlg.Color = Color.Red;
  5. DialogResult result = dlg.ShowDialog();
  6. if (result == DialogResult.OK)
  7. {
  8. MessageBox.Show("You picked " + dlg.Color.ToString());
  9. }
  10. }

组件方式:

  1. colorDialog1.Color = Color.Red;
  2. DialogResult result = colorDialog1.ShowDialog();
  3. if (result == DialogResult.OK)
  4. {
  5. MessageBox.Show("You Picked " + this.colorDialog1.Color.ToString());
  6. }

可见同样的功能,代码减少不少啊。

常用的标准对话框:

【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中添加公共成员,给外部使用。

  1. public string AppName {
  2. get { return this.textBox1; }
  3. set { this.aStr = value; }
  4. }

调用时候:

Form2 dlg = new Form();

dlg.AppName = “My Name”;

DialogResult result = dlg.ShowDialog();

if(result==DialogResult.OK)

{

//Do Something

}

4,Dialog的返回,处理OK按钮和Cancel按钮

手工处理:

  1. Form1
  1. private void button3_Click(object sender, EventArgs e)
  2. {
  3. Form2 dlg = new Form2();
  4. dlg.Text = "Jor";
  5. DialogResult result = dlg.ShowDialog();
  6. if (result == DialogResult.OK)
  7. {
  8. MessageBox.Show(dlg.AppName);
  9. }
  10.  
  11. }
  1. Form2
  1. private void button1_Click_1(object sender, EventArgs e)
  2. {
  3. this.DialogResult = DialogResult.OK;//不用 Close()关闭了,一回事
  4. }

如果窗体需要返回Cancel是一样的:

  1. private void button1_Click_1(object sender, EventArgs e)
  2. {
  3. this.DialogResult = DialogResult.Cancel;
  4. }

手工方式的方式无法将按钮设置成默认按钮,默认按钮是按Enter键调用的那个按钮,按Esc键的时候,Cancel按钮应该被调用。实现这些很简单,只要配置窗体的AcceptButton和CancelButton就可以了。设置好之后发现Cancel按钮的DialogResult属性被自动设置成DialogResult.Cancel了,所以也没必要像手工方式一样去处理Cancel按钮的Click事件的处理程序了。OK也一样,设置好AccepuButton之后,OK按钮的默认值还是DialogResult.None,所以需要手工修改为DialogResult.OK

5,非模式窗体数据传递

与模式窗体的方式不同,需要用户自定义消息以及事件来完成数据交换的功能

在非模式窗体中自定义消息,以及事件触发的条件等。

代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12. public partial class Form2 : Form
  13. {
  14. public Form2()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. public event EventHandler Accept;//定义事件 ***自定义添加(与主窗体处理函数要对应)***
  20.  
  21. private void button1_Click(object sender, EventArgs e)
  22. {
  23. if (Accept != null)
  24. {
  25. Accept(this, EventArgs.Empty);//按下按钮时触发事件***触发自定义事件***
  26. }
  27. }
  28.  
  29. }
  30. }

在主窗体中进行在非模式窗体中定义的事件与处理函数的绑定。当非模式窗体触发该事件时,由绑定的处理函数进行处理。

代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. private void button1_Click(object sender, EventArgs e)
  20. {
  21. Form2 frm = new Form2();
  22. frm.Accept += MyAccept;
  23. frm.Show();
  24. }
  25. void MyAccept(object sender, EventArgs e)
  26. {
  27. Form2 frm2 = (Form2)sender;
  28. this.label1.Text = frm2.textBox1.Text;//这里是直接调用,需要设置一下textBox1的公开特性。也可用代理方式,推荐。
  29. }
  30. }
  31. }

注意:对于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事件得到通知。

代码:

  1. private void textBox1_Validating(object sender, CancelEventArgs e)
  2. {
  3. if (((Control)sender).Text.Trim().Length == 2)
  4. {
  5. MessageBox.Show("Please enter a name", "Error");
  6. e.Cancel = true;//是否可以切换焦点
  7. }
  8. }
  9.  
  10. private void textBox1_Validated(object sender, EventArgs e)
  11. {
  12. MessageBox.Show("My Name is ," + this.textBox1.Text, "Thanks");
  13. }

为了允许用户在没有输入有效数据的情况下点击Cancel按钮来关闭窗体,我们必须将Cancel或Close按钮的CausesValidatin属性设置为true

  1. private void InitializeComponent()
  2. {
  3. this.button1 = new System.Windows.Forms.Button();
  4. this.textBox1 = new System.Windows.Forms.TextBox();
  5. this.textBox2 = new System.Windows.Forms.TextBox();
  6. this.SuspendLayout();
  7. //
  8. // button1
  9. //
  10. this.button1.Location = new System.Drawing.Point(190, 198);
  11. this.button1.Name = "button1";
  12. this.button1.Size = new System.Drawing.Size(75, 23);
  13. this.button1.TabIndex = 0;
  14. this.button1.Text = "button1";
  15. this.button1.UseVisualStyleBackColor = true;
  16. this.button1.CausesValidation = false; //注意这里
  17. this.button1.Click += new System.EventHandler(this.button1_Click_1);
  18. //
  19. // textBox1
  20. //
  1. //...

}

其他的可以用正则表达式或掩码文本的方式来进行数据验证

数据格式的通知功能(添加ErrorProvider组件):

  1. private void textBox1_Validating(object sender, CancelEventArgs e)
  2. {
  3. string error = null;
  4. if (((Control)sender).Text.Trim().Length == 0)
  5. {
  6. error = "Please enter a name";
  7. e.Cancel = true;//是否可以切换焦点
  8. }
  9. this.errorProvider1.SetError((Control)sender, error);
  10. }

Windows Forms 对话框篇的更多相关文章

  1. Windows Forms 窗体篇

    1,显示窗体 非模式: Form form = new Form(); form.Show(); 模式: Form form = new Form(); form.Show(); 2,拥有者窗体与附属 ...

  2. Windows Forms 布局篇

    1,锚定功能(Anchor属性) 默认为“Top,Left”,不管窗体大小如果改变,保持相对于窗体左上角的位置. 如果设置为”Top,Bottom,Left,Right”这样,控件的大小将随窗体的大小 ...

  3. C#Windows Forms窗体、按钮-xdd

    1.更换窗体图标 方法:单击窗体,更改icon属性. 2.调整窗体打开时默认位置 方法:单击窗体,更改StartPotion属性. 3.修改窗体大小 方法:单击窗体,更改Size属性. 4.设置窗体的 ...

  4. Wizard Framework:一个自己开发的基于Windows Forms的向导开发框架

    最近因项目需要,我自己设计开发了一个基于Windows Forms的向导开发框架,目前我已经将其开源,并发布了一个NuGet安装包.比较囧的一件事是,当我发布了NuGet安装包以后,发现原来已经有一个 ...

  5. 用户控件的设计要点 System.Windows.Forms.UserControl

    用户控件的设计要点 最近的项目中有一个瀑布图(彩图)的功能,就是把空间和时间上的点量值以图的形式呈现出来,如下图: X坐标为空间,水平方向的一个像素代表一个空间单位(例如50米) Y坐标为时间,垂直方 ...

  6. Windows Forms (一)

    导读 1.什么是 Windows Forms 2.需要学Windows Forms 么? 3.如何手写一个简单的Windows Forms 程序 4.对上面程序的说明 5.Form 类与Control ...

  7. Windows Forms(二)

    导读 1.用VS创建一个Windows Forms程序 2.分析上面的程序 3.Mediator pattern(中介者模式) 4.卡UI怎么办——BackgroundWorker组件 用VS创建一个 ...

  8. 开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt

    首先,大家可以看下这个链接 Windows GUI自动化测试技术的比较和展望 . 这篇文章介绍了Windows中GUI自动化的三种技术:Windows API, MSAA - Microsoft Ac ...

  9. ystem.Windows.Forms.SplitContainer : ContainerControl, ISupportInitialize

    #region 程序集 System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...

随机推荐

  1. 把书《CUDA By Example an Introduction to General Purpose GPU Programming》读薄

    鉴于自己的毕设需要使用GPU CUDA这项技术,想找一本入门的教材,选择了Jason Sanders等所著的书<CUDA By Example an Introduction to Genera ...

  2. PHP的数组分为两种类型,一种是索引数组,一种是关联数组。有如下关联数组,我们如何获取它的第一个key和value呢?

    示例:$items=array('name'=>'sjm','age'=>'26','sex' => '男','location'=>'北京'); //当然用循环然后break ...

  3. Android Developer:内存分析器

    Heap Viewer,Memory Monitor和Allocation Tracker是用来可视化你的app使用内存的补充工具. 使用Memory Monitor Tool来发现是否有不好的内存回 ...

  4. C++对象模型——Inline Functions(第四章)

    4.5 Inline Functions 以下是Point class 的一个加法运算符的可能实现内容: class Point { friend Point operator+(const Poin ...

  5. python-openpyxl安装

    今天在安装openpyxl的时候,一直提示错误,后来才发现仅仅安装它还不够,还需要其他两个库的支持1.安装jdcal2.安装et_xmlfile这两个库安装的方法,都是直接在命令行下面,进入库文件se ...

  6. Linux下MySQL导入导出数据库

    linux下 一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径):1.导出数据和表结构:mysqldump -u用户名 -p密码 数据库名 > 数据库名.sql# ...

  7. git 版本管理工具说明

    $ git init                 (初始化本地仓库,会生成.git 文件夹  .git 文件夹里存储了所有的版本信息.标记等内容) $ git add .              ...

  8. ASP.NET MVC Web API 学习笔记---第一个Web API程序---近来很多大型的平台都公开了Web API

    1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过 ...

  9. Self-Taught Learning to Deep Networks

    In this section, we describe how you can fine-tune and further improve the learned features using la ...

  10. 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 ...