C# - 文本框验证 [英] C# - TextBox Validation

查看:182
本文介绍了C# - 文本框验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code,检查并确保当用户进入该领域,从1的整数 - 10已被输入

虽然如果用户开出场的对焦,坏的数据(如fdgfdg)仍留在现场。所以可以一些演示如何丢失焦点时的字段,如果该数据是无效的,一个默认值将被输入,而不是如5

 私人无效textBox4_TextChanged(对象发件人,EventArgs的)
        {
            尝试
            {
                INT numberEntered = int.Parse(textBox4.Text);
                如果(numberEntered&小于1 || numberEntered→10)
                {
                    的MessageBox.show(你必须输入1到10之间的数字);
                }
            }
            赶上(出现FormatException)
            {

                的MessageBox.show(你需要输入一个整数);
            }
        }
 

解决方案

有几个事件,您可以在这里使用,离开引发LostFocus 确证有上的MSDN这里

在某些情况下的离开引发LostFocus 将不会触发所以最好在你的情况下使用是确证事件:

  textBox1.Validating + =新CancelEventHandler(textBox1_Validating);


    无效textBox1_Validating(对象发件人,CancelEventArgs E)
    {
        INT numberEntered;

        如果(int.TryParse(textBox1.Text,出numberEntered))
        {
            如果(numberEntered&小于1 || numberEntered→10)
            {
                的MessageBox.show(你必须输入1到10之间的数字);
                textBox1.Text = 5.ToString();
            }
        }
        其他
        {
            的MessageBox.show(你需要输入一个整数);
            textBox1.Text = 5.ToString();
        }
    }
 

I have some code that checks and makes sure that when the users enters in the field an integer from 1 - 10 has to be input.

Although if the users takes focus of the field, the "bad" data (such as "fdgfdg") is still left in the field. So could some demonstrate how when focus is lost on the field, if the data is not valid, a default value will be entered instead e.g. 5

private void textBox4_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int numberEntered = int.Parse(textBox4.Text);
                if (numberEntered < 1 || numberEntered > 10)
                {
                    MessageBox.Show("You must enter a number between 1 and 10");
                }
            }
            catch (FormatException)
            {

                MessageBox.Show("You need to enter an integer");
            }
        }

解决方案

There are several events that you can use here, Leave, LostFocus and Validating there is more discussion of these various events on MSDN here.

Under certain scenarios the Leave and the LostFocus will not fire so the best to use in your case is the Validating event:

    textBox1.Validating += new CancelEventHandler(textBox1_Validating);


    void textBox1_Validating(object sender, CancelEventArgs e)
    {
        int numberEntered;

        if (int.TryParse(textBox1.Text, out numberEntered))
        {
            if  (numberEntered < 1 || numberEntered > 10) 
            { 
                MessageBox.Show("You have to enter a number between 1 and 10");
                textBox1.Text = 5.ToString();
            }
        }
        else
        {
            MessageBox.Show("You need to enter an integer");
            textBox1.Text = 5.ToString();
        }
    }

这篇关于C# - 文本框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆