Click here to Skip to main content
15,900,524 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a numericUpDown1_ValueChanged event and i want to call this event from keypress's event of form1

how can do this???
tnx
Posted

You should not call event handlers. Events should be raised and handled by user action. If you want to reuse the code in the ValueChanged in your KeyPress event handler, do something like this:

KeyPressEvent Start

// Some other processing
CommonMethod();
// Some other processing

End

ValueChangedEvent Start

// Some other processing
CommonMethod();
// Some other processing

End

CommonMethod Start
// Some processing
End


If you meant something else, update your question and/or leave comment to the reply.
 
Share this answer
 
v2
Why?
That would annoy the heck out of a user, unless the only control on the form was the up/down control - which would make it pointless since the control would get the keydown, not the form...

Why not handle the NumericUpDown.KeyPress event, instead?
 
Share this answer
 
Comments
jiji2663 7-Jul-11 2:29am    
how can i do this??
i want when user press for example "a" or "A" the ValueChanged event of numericUpDown1 is runnig
tnx
There is no such event numericUpDown1_ValueChanged. The event is ValueChanged. You cannot call event instance at all. Event handler can be called. Also, firing event is only allowed from the same class where the event is declared, this is one of the main fool-proof limitation of events over regular delegate instances.

So, here is the resolution: create a separate (named, not anonymous) method to server as and event handler for ValueChanged, add this method to event's invocation list. You probably already done it; this is numericUpDown1_ValueChanged (rename it to semantic name; auto-generated names with "_" violate good Microsoft naming conventions). Now, this is a regular method. Call it from your handler of key press event. For sender, better pass your instance numericUpDown1, not "this" (it may be irrelevant, depending what numericUpDown1_ValueChanged does. Anyway, what parameters mean depends on what this method does. You can even pass null to indicate "faked" call and process it in your numericUpDown1_ValueChanged accordingly.

—SA
 
Share this answer
 

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