2 Replies - 1651 Views - Last Post: 03 October 2011 - 08:42 PM Rate Topic: -----

#1 AN1554  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 130
  • Joined: 03-April 11

One KeyPress event for many boxes

Posted 29 September 2011 - 11:59 PM

Private Sub txtName_KeyPress(KeyAscii As Integer)
    Call CheckForEnter(KeyAscii)
End Sub

Private Sub MaskNumber_KeyPress(KeyAscii As Integer)
    Call CheckForEnter(KeyAscii)
End Sub

Private Sub txtAddress_KeyPress(KeyAscii As Integer)
    Call CheckForEnter(KeyAscii)
End Sub


After typing in name in a textbox, I press Enter to go to masked edit box to type in number. I then press Enter again to go to next textbox to type in address. With many boxes, I have to call the procedure in each one's KeyPress event.

I would like to know if there is a way to remove the need to call the procedure in each box's KeyPress event. Instead of having one KeyPress event for each box, there will be a single KeyPress event which will call the procedure for any box. I consider a collection of controls but I don't know how to create and implement it.

This post has been edited by AN1554: 30 September 2011 - 12:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: One KeyPress event for many boxes

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,770
  • Joined: 26-March 09

Re: One KeyPress event for many boxes

Posted 30 September 2011 - 05:50 AM

You could set the KeyPreview property to true on the form and capture it at form level.
Was This Post Helpful? 0
  • +
  • -

#3 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: One KeyPress event for many boxes

Posted 03 October 2011 - 08:42 PM

You could do that. If you want to go that way, investigate the ActiveControl property. You should also investigate control arrays. I see that you have a masked edit control in there, and control arrays have to be the same type of control, so you might not gain too much. But you'd narrow it down to two KeyPress events at least. If you decide to use control arrays, investigate the Tag property as a means of keeping track of your controls with human-readable strings. (You can of course use the Index property as well.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1