错误提醒控件,提供用于指示窗体上的控件具有关联错误的用户界面。
命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
效果如下图:
![](/WebResource.axd?d=pLXXeGbWF7eXU8SMs2-GFZvUWY2JNH05dFx5YzJhGUYAYJAFEaTEq36NAhTPy7_KekvzDFwt8wvQWdByvJIGWdEq6x2KpKD80&t=633043282340000000)
![01.jpg](/images/cnblogs_com/huaiyu2006/01.jpg)
使用代码方法如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8![](/Images/OutliningIndicators/None.gif)
9
namespace ErrorProviderForm
10![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
11
public partial class Form1 : Form
12![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
public Form1()
14![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
InitializeComponent();
16
}
17![](/Images/OutliningIndicators/InBlock.gif)
18
private void btnOk_Click(object sender, EventArgs e)
19![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
if(this.textBox1.Text.Length>12||this.textBox1.Text.Length<6)
21![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
this.errorProvider1.SetError(this.textBox1,"用户名输入错误"); //绑定到控件,提示信息
23
DialogResult ReturnDlg = MessageBox.Show(this,"用户名输入错误,是否从新输入?",
24
"信息提示",MessageBoxButtons.RetryCancel,MessageBoxIcon.Question);
25
switch (ReturnDlg)
26![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
this.errorProvider1.SetError(this.textBox2, "用户名输入错误");
37
DialogResult ReturnDlg = MessageBox.Show(this, "用户名输入错误,是否从新输入?",
38
"信息提示", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
39
switch (ReturnDlg)
40![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
this.errorProvider1.SetError(this.textBox3, "用户名密码两次输入不一致");
51
DialogResult ReturnDlg = MessageBox.Show(this, "用户名密码两次输入不一致,是否从新输入?",
52
"信息提示", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
53
switch (ReturnDlg)
54![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
case DialogResult.Retry:
56
this.textBox3.Text = "";
57
break;
58
case DialogResult.Cancel:
59
break;
60
}
61
}
62
}
63![](/Images/OutliningIndicators/InBlock.gif)
64
}
65
}