`
369540808
  • 浏览: 197872 次
文章分类
社区版块
存档分类
最新评论

C#错误显示控件使用(以登陆为例)

 
阅读更多

在应用程序设计时候,我们经常需要判断用户输入的数据是否合法,不合法给出提示,一般提示都是用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("登陆成功");
                        }
                    }
                }
            }
        }
    }
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics