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);
}
posted @ 2016-03-16 14:13  C#winform软件设计  阅读(1116)  评论(0编辑  收藏  举报