Hello
I have an question to the forum about the eventargs used by the "KeyPress" eventhandler:
In this there is a property example "IsNumber()", which has number in the function.
I have some textboxes where the text should only be letters and then no numbers and others only number no text.
1) The question is can you open this build in functions and create my own function to this
Because if I would use "Backspace-Key" and write number, then I should use both "ISNumber()" and "IsControl()", so I hope someone can telle that you can create your own function and use these build in function as template
(Example so I get both something from "ISNumber()" and "IsControl()" in one fucntion )
I hope someone answer the question as soon as possible.
Thanks.
Spot

About KeyPress functions in Visual C# 2003
inbissachin
Vish
Maybe...
But how do I take the content og the "IsLetter()" and put it together with something from "IsControl()", for example
It is that I should use in my program.
That you take the whole function "IsLetter()" and put something from "IsControl()" and it should be a function I have created myself
spot
Tran Trung dung
What you can do is the following:
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
string acceptable = "0123456789"+(char)8;
if (!acceptable.Contains(e.KeyChar.ToString())) e.Handled = true;
}
or a variation thereof. Now if the user types something that is not in acceptable it will simply not be entered. (char)8 is the backspace.
Another way is to use int.TryParse.
Regards,
Guido
C#Thunder
I don’t quite follow what you are looking for but think I understand... It would in many ways be nice if there was a single IsNumber() style function in C# as there is in VB.NET, unfortunately we do not have that. It is however trivially easy to create your own, something like this:
public static bool IsNumber(string expression)
{
bool hasDecimal = false;
for(int i=0;i<expression.Length;i++)
{
// Check for decimal
if (expression
== '.')
{
if (hasDecimal) // 2nd decimal
return false;
else // 1st decimal
{
// inform loop decimal found and continue
hasDecimal = true;
continue;
}
}
// check if number
if(!char.IsNumber(expression
))
return false;
}
return true;
}
This function simply iterates through each character in a string and makes sure that each character within is a numeric value and allowing no more than a single decimal point within it.
Does this answer your question
charismatic
Here straight from microsoft themselves:
// Boolean flag used to determine when a character other than a number is entered. private bool nonNumberEntered = 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. nonNumberEntered = false; // Determine whether the keystroke is a number from the top of the keyboard. if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) { // Determine whether the keystroke is a number from the keypad. if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) { // Determine whether the keystroke is a backspace. if(e.KeyCode != Keys.Back) { // A non-numerical keystroke was pressed. // Set the flag to true and evaluate in KeyPress event. nonNumberEntered = 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 (nonNumberEntered == true) { // Stop the character from being entered into the control since it is non-numerical. e.Handled = true; } }Me Myself and I
I try.
In visual C# 2003 you have some functions in "KeyPress"-Textbox method.
In my program I have this function:
private void textBox5_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
e.Handled = true;
}
}
In the function above you see two functions: 1. IsNumber() and 2. IsControl().
The function "IsNumber()" only allow writing numbers in the textbox and the function "Iscontrol()" allow to use Backspace key.
That I want is to get the content of "IsNumber()" and the function to use Backspace key from the "IsControl()" function and then put together in my own function.
How I do that because I can't see into these build in functions to see how they are, It will be very nice if you could see inside these build in function to see how there ar build and then create your own to mix something from these function.
Hope it was a litte more detailed.
spot
Brixi69
You can use the key codes and > and < symbols to get it to look for a range. I'm using 2005, but you would do something like this:
if( e.KeyChar >= Keys.0 || e.KeyChar <= 9 ) {
// its a number
}
You can't a lot of times pick up Control from a KeyPress, you need to pick it up from a KeyDown event... I spent 2 hours last night messing around with the different keyevents. Lol, KeyDownPreview, KeyDown, KeyUp, KeyPress! What I do is have an int ctrl... If in the KeyDown it's control I set ctrl=1, in the KeyUp if it's control I set ctrl=0. Then in the KeyPress Event I have whether or not control is pressed (if it's 1 then its control e.KeyChar, otherwise just e.KeyChar).