文本框验证

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

问题描述

大家好,

这是我的问题...我的项目有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

推荐答案

[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);
    }
}



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[^]


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

08-22 23:13
查看更多