文本框验证 [英] Text Box Validation

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

问题描述

大家好,

这是我的问题...我的项目有15个需要验证的表格(仅包括字母和数字)我想为所有相同的文本框编写通用代码...有没有办法实现这样的功能代码...请帮助我..im在C#.net中有点新功能

任何帮助将不胜感激


感谢广告高级

lakhanp22

hello Every ,

Here Is my Question ... My project have 15 Form At Many Forms I need to validate (like alphabets only and number only )i want to write a common code for all the same text box ...is there any way to impalement such code ...please help me ..i m bit new in C#.net

any help will be appreciated


thanks in Ad advanced

lakhanp22

推荐答案

这是数字文本框的实现,您可以直接使用它,也可以根据需要进行自定义.


This is an implementation of an numeric text box you can use it as is or customize by your needs.


[ToolboxBitmap(typeof(System.Windows.Forms.TextBox))]
public class FilteredTextBox : TextBox
{
    private TextBoxInputFilter tbif = TextBoxInputFilter.Standard;
    private bool nonNumberEntered = false;

    public enum TextBoxInputFilter  //input filter enumeration
    {
        Standard = 1,
        Numeric = 2,
    }

    public TextBoxInputFilter InputFilter //this property holds input filter state
    {
        get { return tbif; }
        set { tbif = value; }
    }
    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (this.InputFilter == TextBoxInputFilter.Numeric)
        {
            nonNumberEntered = false;
            //Check if pressed key is a number from a top line of keyboard
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                //Check if pressed key is a number from a numeric keyboard
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    //determine if it is a BACSPACE key
                    if (e.KeyCode != Keys.Back)
                    {
                        nonNumberEntered = true;
                    }
                }
            }
        }
        base.OnKeyDown(e);
    }
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        if (this.InputFilter == TextBoxInputFilter.Numeric)
        {
            if (nonNumberEntered == true)
            {
                //prevent entering a symbol because it is not a number
                SystemSounds.Beep.Play();
                e.Handled = true;
            }
        }
        base.OnKeyPress(e);
    }
}


嗨!

您可以创建一个派生自TextBox的自定义控件,使其仅接受经过验证的输入.为此,您必须使用C#.Net中的class(.cs文件).这是 NumericTextBox类的示例示例其中显示了如何隐式并将其放置在表单上.

您可以在类中实现验证.

问候.
Hi!

You can create a custom control derived from TextBox so that it accepts only validated input.For this you have to use class(.cs file) in C#.Net. Here is sample example of the NumericTextBox class which shows how to impliment and place it on the form.

You can implement your validation in class.

Regards.


您没有提供环境,但我认为它是WinForms.在所有事件中,都有一个发件人.您可以使用发送方确定哪个控件发送了事件,并且可以将其强制转换为正确的类型(在本例中为var tb = (TextBox)sender.

您可以进行验证

You do not give an environment, but I assume it is WinForms. On all events there is a sender. You can determine which control sent the event by using the sender, and you can cast it to the right type (in this case var tb = (TextBox)sender.

You can do the Validating

void TextBox_Validating(object sender, CancelEventArgs e) {
  if( TextBox.Text.Length == 0 )
  {
    MessageBox.Show("Please enter a name", "Error");
    e.Cancel = true;
  }
}



您需要做的就是将所有文本框事件定位到同一处理程序.

快速概述位于 http://www.sellsbrothers.com/writing/winformsDataValidation.htm [ ^ ]



All you need to do is target all the text box events to the same handler.

A quick overview is at http://www.sellsbrothers.com/writing/winformsDataValidation.htm[^]


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

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