在应用程序设计时候,我们经常需要判断用户输入的数据是否合法,不合法给出提示,一般提示都是用MessageBox,但是怎样达到类似下面的效果呢?
右边有个小红叹号,鼠标放上还有信息提示。。。。
这个就需要使用C#提供的错误显示控件了。
在工具栏找到errorPrivoder控件拖进来,发现跟timer控件差不多。。呵呵。。。。
创建应用界面如下:
我们用到的是errorProvider1.SetError这个函数有两个参数,第一个是要显示错误的对于空间ID,第二个是鼠标放在上面自动显示的信息。
代码如下:(这个代码完全实现了用户登陆的细节,需要的话可以直接放到自己代码中)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 使用错误提示控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSure_Click(object sender, EventArgs e)
{
if (txtName.Text != "Admin")
{
if (txtName.Text != "")
{
errorProvider1.SetError(txtName, "用户名输入错误");
DialogResult d = MessageBox.Show("用户名输入错误", "错误", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
if (d == DialogResult.Retry)
{
txtName.Text = "";
txtPsw.Text = "";
txtRePsw.Text = "";
}
else
{
this.Close();
}
}
else
{
errorProvider1.SetError(txtName, "用户名不能为空");
DialogResult d = MessageBox.Show("用户名不能为空", "错误", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
if (d == DialogResult.Retry)
{
txtName.Text = "";
txtPsw.Text = "";
txtRePsw.Text = "";
}
else
{
this.Close();
}
}
}
else //判断密码
{
if (txtPsw.Text == "")
{
errorProvider1.SetError(txtPsw, "密码不能为空");
DialogResult d = MessageBox.Show("密码不能为空", "错误", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
if (d == DialogResult.Retry)
{
txtPsw.Text = "";
txtRePsw.Text = "";
}
this.Close();
}
else
{
if (txtRePsw.Text != txtPsw.Text)
{
errorProvider1.SetError(txtRePsw, "密码不一致");
DialogResult d = MessageBox.Show("密码不一致", "错误", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
if (d == DialogResult.Retry)
{
txtRePsw.Text = "";
txtPsw.Text = "";
}
else
{
this.Close();
}
}
else
{
if (txtPsw.Text != "Admin")
{
errorProvider1.SetError(txtPsw, "密码错误");
DialogResult d = MessageBox.Show("密码错误", "错误", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question);
if (d == DialogResult.Retry)
{
txtPsw.Text = "";
txtRePsw.Text = "";
}
else
{
this.Close();
}
}
else
{
MessageBox.Show("登陆成功");
}
}
}
}
}
}
}
分享到:
相关推荐
c#显示时间控件
C#上位机开发教程,5.C#波形显示控件源码.rar,有需要的拿走
C#编写的十六进制数据显示控件 就像WinHex那样的
用C#编的一个led数字显示控件,能够液晶显示数字,大小,颜色均可调
C#如何使用图像控件显示图片
C#常用工业控件及使用方法,各种表,各种按钮都有,好东西,一起分享!!
超好用的C# Log日志控件,直接引用DLL即可,代码已加注释,可以根据自己的需求做二次开发,很不错的学习案例!
多个控件时,调用SetMeTop(object obj)方法,可使控件处于最上面。别忘了添加System.Collections命名空间。
c#实现的日程显示控件,可以用于展示日程安排,并附带使用的简单示例
c#在线程中使用控件时控件出现红叉解决方法.cs
C# DataGridView控件使用全攻略
C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮控件C# 自定义按钮...
C#_DataGridView控件使用全攻略
C#控件学习C#控件学习C#控件学习C#控件学习C#控件学习C#控件学习C#控件学习C#控件学习C#控件学习C#控件学习C#控件学习
本人开发 采用vs2008 c#,开发的电池容量控件 根据输入参数容量百分比实时显示。
C#的所有控件列表以及使用方法的详细说明
c#常用标准控件c#常用标准控件c#常用标准控件c#常用标准控件
C#编写的3D显示控件(可将文字转换为3D) 本人收藏了3年的资源 现放出 都是总结了很多系统 软件项目实施过程中的经验的 慢慢积累的