如何使用错误提醒控件?
错误提醒控件,提供用于指示窗体上的控件具有关联错误的用户界面。
命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
效果如下图:
使用代码方法如下:
Code
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9namespace ErrorProviderForm
10{
11 public partial class Form1 : Form
12 {
13 public Form1()
14 {
15 InitializeComponent();
16 }
17
18 private void btnOk_Click(object sender, EventArgs e)
19 {
20 if(this.textBox1.Text.Length>12||this.textBox1.Text.Length<6)
21 {
22 this.errorProvider1.SetError(this.textBox1,"用户名输入错误"); //绑定到控件,提示信息
23 DialogResult ReturnDlg = MessageBox.Show(this,"用户名输入错误,是否从新输入?",
24 "信息提示",MessageBoxButtons.RetryCancel,MessageBoxIcon.Question);
25 switch (ReturnDlg)
26 {
27 case DialogResult.Retry:
28 this.textBox1.Text = "";
29 break;
30 case DialogResult.Cancel:
31 break;
32 }
33 }
34 if (this.textBox2.Text.Length > 12 || this.textBox2.Text.Length < 6)
35 {
36 this.errorProvider1.SetError(this.textBox2, "用户名输入错误");
37 DialogResult ReturnDlg = MessageBox.Show(this, "用户名输入错误,是否从新输入?",
38 "信息提示", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
39 switch (ReturnDlg)
40 {
41 case DialogResult.Retry:
42 this.textBox2.Text = "";
43 break;
44 case DialogResult.Cancel:
45 break;
46 }
47 }
48 if (!(this.textBox2.Text== this.textBox3.Text))
49 {
50 this.errorProvider1.SetError(this.textBox3, "用户名密码两次输入不一致");
51 DialogResult ReturnDlg = MessageBox.Show(this, "用户名密码两次输入不一致,是否从新输入?",
52 "信息提示", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
53 switch (ReturnDlg)
54 {
55 case DialogResult.Retry:
56 this.textBox3.Text = "";
57 break;
58 case DialogResult.Cancel:
59 break;
60 }
61 }
62 }
63
64 }
65}
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9namespace ErrorProviderForm
10{
11 public partial class Form1 : Form
12 {
13 public Form1()
14 {
15 InitializeComponent();
16 }
17
18 private void btnOk_Click(object sender, EventArgs e)
19 {
20 if(this.textBox1.Text.Length>12||this.textBox1.Text.Length<6)
21 {
22 this.errorProvider1.SetError(this.textBox1,"用户名输入错误"); //绑定到控件,提示信息
23 DialogResult ReturnDlg = MessageBox.Show(this,"用户名输入错误,是否从新输入?",
24 "信息提示",MessageBoxButtons.RetryCancel,MessageBoxIcon.Question);
25 switch (ReturnDlg)
26 {
27 case DialogResult.Retry:
28 this.textBox1.Text = "";
29 break;
30 case DialogResult.Cancel:
31 break;
32 }
33 }
34 if (this.textBox2.Text.Length > 12 || this.textBox2.Text.Length < 6)
35 {
36 this.errorProvider1.SetError(this.textBox2, "用户名输入错误");
37 DialogResult ReturnDlg = MessageBox.Show(this, "用户名输入错误,是否从新输入?",
38 "信息提示", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
39 switch (ReturnDlg)
40 {
41 case DialogResult.Retry:
42 this.textBox2.Text = "";
43 break;
44 case DialogResult.Cancel:
45 break;
46 }
47 }
48 if (!(this.textBox2.Text== this.textBox3.Text))
49 {
50 this.errorProvider1.SetError(this.textBox3, "用户名密码两次输入不一致");
51 DialogResult ReturnDlg = MessageBox.Show(this, "用户名密码两次输入不一致,是否从新输入?",
52 "信息提示", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
53 switch (ReturnDlg)
54 {
55 case DialogResult.Retry:
56 this.textBox3.Text = "";
57 break;
58 case DialogResult.Cancel:
59 break;
60 }
61 }
62 }
63
64 }
65}