Tuesday, July 28, 2009

In C# .Net, How to set perticular character set for TextBox?

I have Textbox and I want to allow only Numbers(0-9), "." and "+" entering by user.


Is there any property of TextBox which allows me to do this?


What can be the other solution?


Thanks in Advance...

In C# .Net, How to set perticular character set for TextBox?
Hi there,





The way I will do that is by introducing TextBox events.





Click on the TextBox, goto the event panel, and choose "KeyDown" and "KeyPress" event. You need to control when the user presses any key in that TextBox, and while it is presssed we need to see if we will handle it...





Now you can control the contents in that TextBox from simulating those both events... ... I don't have Visual Studio installed, so try to figure out how to complete this example. I don't know if it compiles :p





I copied it here (looks better syntax):


http://pastebin.ca/699305








=================


// First lets create a var that checks if the input is valid


private bool validInput = false;





// Handle the KeyDown event to determine


// the type of character entered into the control.


private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)


{


// Initialize the flag to false.


validInput = false;





// We are checking if it is not that valid button pressed


if (


e.KeyCode %26lt; Keys.D0 || e.KeyCode %26gt; Keys.D9 ||


e.KeyCode %26lt; Keys.NumPad0 || e.KeyCode %26gt; Keys.NumPad9 ||


e.KeyCode != Keys.Oemplus || e.KeyCode != OemPeriod


){


validInput = true;


}





}





// This event occurs after the KeyDown event and can be used to prevent


// characters from entering the control.


private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)


{


// Check for the flag being set in the KeyDown event.


if (validInput == true)


{


// Stop the character from being entered into the control since it is non-numerical.


e.Handled = true;


}


}


========================








Basically, once we press a button, we have a variable that decides if that is a valid button or not, if it isn't a valid button is sets the validator variable to true which means it is invalid. Now once you take off your finger off that key on the keyboard, it will check if that variable is true, if it isn't, it will not handle that input.








This is an example to force the user to press those key strokes for that textbox.





NOTE: You can change that arround, and on keypress, you validate the whole TextBox String, and introduce a regular expression:





=========================


// Regular expression pattern that checks if it starts


// and ends with the a number/dot/plus sign


string pattern = "^([0-9]|\+|\.)*$";





// Lets match that pattern with the contents of that textbox


if(Regex.IsMatch(textBox1.Text, pattern))


{


Console.WriteLine("Pattern Found");


}


else


{


Console.WriteLine("Pattern Not Found");


}


=========================





You can introduce that regular expression to the top as well! Just register that event, check if that key is a valid key from using a regular expression or just an if/else structure, and handle that key!





That is all, if you have any more questions please refer to the docs .





Good Luck

violet

No comments:

Post a Comment