Sending objects to an event handler

  • Thread starter Thread starter John Sutor
  • Start date Start date
J

John Sutor

I currently have an event handler setup to handle keypress events. the
handler only takes the same arguments as the keypress event (object sender,
System.Windows.Forms.KeyPressEventArgs e).
I need to somehow pass the control to the event to evaluate how many
characters have been types into the text box and limit them to a max of 4
characters if a decimal has been entered in the third spot or 3 charaters if
just numbers have been entered.
Any hints
 
John,

If its just the issue of limiting number of characters...there is a simple
way

//set text box maxlength
textBox1.MaxLength = 5;
//select even casing
textBox1.CharacterCasing = CharacterCasing.Upper;

Is is what you are looking for?

Shak
 
Create a class that inherits from KeyPressEventArgs and pass that, then cast
it to your type when it gets there. The *EventArgs classes are designed for
this.

Just add a new member to the inherited class that contains your control
reference.
 
Hi John,
That's what the sender parameter of the events is for. It has to be the
control (or object) sending the event. In very rear cases it is not so
obvious. For example in toolbars the toolbar is the one that sends the event
that's why the button is passed thru the event args. But in the case with
text boxes everything is simpler. The textbox is the sender of all the
events.
 
Back
Top