Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Maciej Los 12-Jun-12 13:19pm    
What kind of validation do you need? Please, be more specific. Provide more details.
RDBurmon 13-Jun-12 9:31am    
Thanks Everyone who replied to this thread , So LakhanP, I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.

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


C#
[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);
    }
}
 
Share this answer
 
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.
 
Share this answer
 
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

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


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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900