5 Replies - 1176 Views - Last Post: 04 October 2012 - 11:58 PM Rate Topic: -----

#1 krakatoa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-October 12

texBox KeyPress event

Posted 04 October 2012 - 10:38 PM

Hello, i am creating a program which have few texboxes and they should be numeric only. I mean u can type number or ".". Here is the code
        
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            bool isNumber = char.IsNumber(e.KeyChar);
            bool isPeriod = (e.KeyChar.Equals('.'));
            bool isControl = (char.IsControl(e.KeyChar)); e.Handled = (!isNumber && !isControl && !isPeriod);
        }


The problem is that I have to type this in each texbox. Now there is only 3 lines of code, but if there was lot more i think this is not the best way. So the question is, how i can make some method and call it and also if there is a way to make each textbox in form numeric only.

Is This A Good Question/Topic? 0
  • +

Replies To: texBox KeyPress event

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: texBox KeyPress event

Posted 04 October 2012 - 10:49 PM

Your options are:
1. Create a helper method that takes the EventArgs.
2. In the designer, make all the Keypress event handlers of the different text boxes point to one event handler method.
3. Derive a NumbersOnlyTextBox class that overrides onkeypress() and add this instead of a regular textbox to your form at design time.
4. At runtime, iterate over all the textboxes in the form and make all the Keypress event handlers point to one event handler method.
Was This Post Helpful? 2
  • +
  • -

#3 krakatoa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-October 12

Re: texBox KeyPress event

Posted 04 October 2012 - 11:21 PM

Could you show me 2th and 4th option examples?
Was This Post Helpful? 0
  • +
  • -

#4 krakatoa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-October 12

Re: texBox KeyPress event

Posted 04 October 2012 - 11:29 PM

I figured out how to do 2nd option. But still could you show me 4th?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: texBox KeyPress event

Posted 04 October 2012 - 11:45 PM

Psuedo code for the 4th option:
foreach(var control in myForms.Controls)
{
    if control is a TextBox
        ((TextBox) control).KeyPress += myEventHandler
}



You'll have to do that recursively if any of your textboxes are embedded within panels, or custom controls.
Was This Post Helpful? 1
  • +
  • -

#6 krakatoa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-October 12

Re: texBox KeyPress event

Posted 04 October 2012 - 11:58 PM

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1