VOGONS

Common searches


First post, by ncmark

User metadata
Rank Oldbie
Rank
Oldbie

Does anyone here have any experience with detecting a keypress in visual basic?
Be forewarned.... I am using an older version (version 4)

I had a form set up like a mini-excel spreadsheet,,,, textboxes to enter data and a scroll bar. What I *wanted* was for the user to enter numerical data and press return. But windows insists on playing a "ding" every time the return key is pressed. That method also had the disadvantage of not disallowing character input.

So then I decided to do the whole thing myself - use label boxes instead of textboxes and handle it all from the form keypress event. But then I ran across a new problem - it would seem that the form keypress event is not read if there any objects on the form capable of detecting a keypress

I think I have a workaround.... as soon as a user clicks on a label box, then then all scroll bars and command buttons are disabled, at which point the FORMs keypress code becomes active. They are then re-enabled after a return keypress,

Seems like a convoluted way to do it. Does anyone have any suggestions? Thanks!

Reply 1 of 3, by collector

User metadata
Rank l33t
Rank
l33t

Can't help you with VB, but if you were using VB.NET it would be easy enough to convert this from C#. To get when enter is pressed on your control's KeyDown event:

if (e.KeyCode == Keys.Enter)
{
//add you code here
}

Of course you could always download VS Express to get VB.NET and C#.

The Sierra Help Pages -- New Sierra Game Installers -- Sierra Game Patches -- New Non-Sierra Game Installers

Reply 2 of 3, by Gemini000

User metadata
Rank l33t
Rank
l33t

It's been a lonnnnnnnnng time since I last coded in an early version of VisualBASIC. I recall that there weren't a lot of options for doing the kind of stuff you want to do.

If memory serves, SOME kind of event should fire when you hit the enter key from a text box, so you should just try and capture that event from the text box itself.

--- Kris Asick (Gemini)
--- Pixelmusement Website: www.pixelships.com
--- Ancient DOS Games Webshow: www.pixelships.com/adg

Reply 3 of 3, by SquallStrife

User metadata
Rank l33t
Rank
l33t

On VB forms you'll have one CommandButton control with the "Default" property set to True, and that control's OnClick event will be triggered when you hit Enter anywhere on the form (unless another CommandButton has explicitly been given focus, e.g. by Tabbing through the controls)

Put your text fields into a control array, and the task is simple.

Option Explicit
Dim intCurrentTextBox

Private Sub Command1_Click()

intCurrentTextBox = intCurrentTextBox + 1 ' Pick next array element
If intCurrentTextBox = Text1.UBound + 1 Then intCurrentTextBox = 0 ' Loop back to 0 if upper bound violated
Text1(intCurrentTextBox).SetFocus ' Give focus to next textbox

End Sub

Private Sub Text1_GotFocus(Index As Integer)

intCurrentTextBox = Index ' Record currently selected textbox index
Text1(Index).SelStart = 0 ' Move cursor to home

End Sub

On the form is a CommandButton named Command1 with the property Default set to True, and an arbitrary number of text boxes in a control array named Text1, with sequential Index property values.

To create a control array, just place two textboxes and name the second one the same as the first. With all this in place, pressing Enter should cycle through the text boxes.

If you don't want the "Next field" button to be visible, you'll need to move it off the edge of the form somewhere, it won't work if you change the Visible property to False.

Tested on VB6, but should apply to VB4 and even VB3.

VogonsDrivers.com | Link | News Thread