Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I used two textbox like name txtSerial and txtID where I want to type only
number 0 to 9. This is ok. But I want to use same code in keypress in a other private function. But there e handler doesn't exist. Now how can I solve this problem? I need to use e handler in others private function. Please update me
a solution.

C#
private void txtSerial_KeyPress(object sender, KeyPressEventArgs e)
{
      number_handler();
}
private void txtID_KeyPress(object sender, KeyPressEventArgs e)
{
      number_handler();
}

private void number_handler()
{
    char var = e.KeyChar;

    if (var == 8)
        e.Handled = false;

    else if (var < '0' || var > '9')
         e.Handled = true;
}
Posted

Wow! And where do you think e is defined in number_handler? Add a parameter KeyPressEventArgs e to this method, for example.

Wait a minute. Do you really understand what are you trying to write? Don't use var — this is a keyword. Your code won't compile because 8 is not a char, you can use '\U+0008' to make it a character, as comparison with KeyPressEventArgs.KeyChar requires. No need to assign Handled to false — this is its initial value; only assign to true when required.

—SA
 
Share this answer
 
v4
Comments
fjdiewornncalwe 9-Nov-12 12:16pm    
+5.
Sergey Alexandrovich Kryukov 9-Nov-12 12:17pm    
Thank you, Marcus.
--SA
Maciej Los 9-Nov-12 12:46pm    
Not complete answer... but good enough for 5+! ;)
Sergey Alexandrovich Kryukov 9-Nov-12 13:04pm    
Thank you very much, Maciej.
What would you consider a complete? If you say that this is writing the complete code, I can tell you this: as OP's code has bugs, we cannot say for sure what exactly it was supposed to do.
--SA
Maciej Los 9-Nov-12 14:02pm    
Sergey, "Not complete answer.." - It was a joke, because of the second part of sentence "but good enough".
Cheers!
;)
In both KeyPress handlers, "e" is a parameter, which you do not pass to the number_handler method.
Hence, it is not available, since the compiler does not check what calls what and make variables available in a chain.

If you need "e" in number_handler, then pass it through as a parameter, or better, hand it e.KeyChar and have it return true or false depending on whether it handled it or not. That way, it does not need to know it is called from an event handler, and can be a more generic method.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Nov-12 12:10pm    
There are further problems, please see my answer.
--SA
Wow! solution 3 is greatly helped me. But there e is mistake in number_handler().
C#
private void txtSerial_KeyPress(object sender, KeyPressEventArgs e)
{
      number_handler(e);
}
private void txtID_KeyPress(object sender, KeyPressEventArgs e)
{
      number_handler(e);
}

private void number_handler(KeyPressEventArgs e)
{
    char var = e.KeyChar;

    if (var == 8)
        e.Handled = false;

    else if (var < '0' || var > '9')
         e.Handled = true;
}
 
Share this answer
 
Create function like this


C#
private void txtSerial_KeyPress(object sender, KeyPressEventArgs e)
{
      number_handler(e);
}
private void txtID_KeyPress(object sender, KeyPressEventArgs e)
{
      number_handler(e);
}

private void number_handler(KeyPressEventArgs e)
{
    char var = e.KeyChar;

    if (var == 8)
        e.Handled = false;

    else if (var < '0' || var > '9')
         e.Handled = true;
}



It will work.
 
Share this answer
 
v2
Comments
Thomas Daniels 9-Nov-12 12:10pm    
But in the txtSerail_KeyPress and txtID_KeyPress methods, don't call number_handler();
but number_handler(e);
I added this to your answer.
Sergey Alexandrovich Kryukov 9-Nov-12 12:11pm    
No, it won't. I explained why (or why else) it won't in my answer. You mechanically copied OP's code and fixed just one thing. You need to check up the solution before posting.
--SA

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