Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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.
 
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 9-Nov-12 5:52am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v4
Comments
Marcus Kramer at 9-Nov-12 12:16pm
   
+5.
Sergey Alexandrovich Kryukov at 9-Nov-12 12:17pm
   
Thank you, Marcus.
--SA
Maciej Los at 9-Nov-12 12:46pm
   
Not complete answer... but good enough for 5+! ;)
Sergey Alexandrovich Kryukov at 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 at 9-Nov-12 14:02pm
   
Sergey, "Not complete answer.." - It was a joke, because of the second part of sentence "but good enough".
Cheers!
;)
Sergey Alexandrovich Kryukov at 9-Nov-12 14:20pm
   
Whatever... thank you again.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 9-Nov-12 12:10pm
   
There are further problems, please see my answer.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Create function like this
 

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.
  Permalink  
v2
Comments
ProgramFOX at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Wow! solution 3 is greatly helped me. But there e is mistake in number_handler().
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;
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 405
1 Sergey Alexandrovich Kryukov 220
2 Maciej Los 195
3 Peter Leow 190
4 DamithSL 174


Advertise | Privacy | Mobile
Web02 | 2.8.140705.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid