Click here to Skip to main content
13,146,742 members (93,972 online)
Rate this:
 
Please Sign up or sign in to vote.
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.

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 9-Nov-12 12:16pm
   
+5.
   
Thank you, Marcus.
--SA
Maciej Los 9-Nov-12 12:46pm
   
Not complete answer... but good enough for 5+! ;)
   
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!
;)
   
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
   
There are further problems, please see my answer.
--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  
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 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.
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100