473,480 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with KeyDown Event

365 Contributor
Hello peeps,

bit of a trivial question really...

I have a form with 2 buttons, a left scroll and a right which moves the form through dates.

i want to use the keyboard arrow buttons to "press" these buttons which all works fine so far..

My problem is that i want the focus to remain on these buttons (which ever was "pressed") but every other "press" moves the focus to the next form control and then back on the next control...

I want to avoid this...

I have tried to set the focus again to the button in question and have also tried to first set the focus to the opposite button to then be set again to the button i want but it still seems to only work alternatively.... any suggestions?

cheers fellas (and fellettes)

Dan
Mar 2 '10 #1
7 9743
ADezii
8,834 Recognized Expert Expert
Let's assume that you have two Command Buttons named cmdForward and cmdBackward that you wish to also activate through the Arrow Keys (Left and Right):
  1. Set the Caption Property of the Forward Key to &Forward (Shortcut Key F).
  2. Set the Caption Property of the Backward Key to &Backward (Shortcut Key B).
  3. Set the KeyPreview Property of the Form to True/Yes.
  4. Place the following code in the KeyDown() Event of your Form:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2. Select Case KeyCode
    3.   Case vbKeyRight
    4.     SendKeys "%F"
    5.   Case vbKeyLeft
    6.     SendKeys "%B"
    7.   Case Else
    8.     'Do nothing
    9. End Select
    10. End Sub
  5. This should enable the specific Command Button to retain Focus.
Mar 2 '10 #2
Dan2kx
365 Contributor
Sendkeys works, i was calling the on click event in the same way.


Genious as ever Cheers

Dan
Mar 2 '10 #3
Dan2kx
365 Contributor
OK.. round two...

What about using the up and down keys to cycle through a combo box, no caption box?

EDIT: i dont want the focus to remain on the combo box in this instance...Just want to stop the form scrolling through controls (up/down)
Mar 2 '10 #4
ADezii
8,834 Recognized Expert Expert
I'm literally running out the door, so here goes some thrown-together code that will cycle through entries Downward in a Combo Box. You may have to modify it, and I didn't have time to complete KeyUp, but here it goes with two Assumptions:
  1. The KeyPreview Property of the Form is set to True/Yes.
  2. Combo Box Name is cboTest.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2. Dim cbo As ComboBox
    3.  
    4. Set cbo = Me![cboTest]
    5.  
    6. If cbo.ListCount = 0 Then Exit Sub      'Combo not populated
    7.  
    8. With cbo
    9.   Select Case KeyCode
    10.     Case vbKeyDown
    11.       KeyCode = 0
    12.       'A ListIndex of -1 means that no Item is selected in the Combo Box. The
    13.       'Combo Box itself may/may not be selected, so let's be sure by directing
    14.       'Focus to it
    15.       cbo.SetFocus
    16.         If .ListIndex = .ListCount - 1 Then         'Last Item in Combo
    17.           'Move back to the first? It's up to you.
    18.           .ListIndex = 0       '1st Item
    19.         Else
    20.           'Move Down one Item
    21.           .ListIndex = .ListIndex + 1
    22.         End If
    23.     Case vbKeyUp
    24.       KeyCode = 0
    25.       'for you to fill in
    26.     Case vbKeyLeft
    27.       KeyCode = 0
    28.     Case vbKeyRight
    29.       KeyCode = 0
    30.     Case Else
    31.       'Do nothing
    32.   End Select
    33. End With
    34. End Sub
  3. As far as the Arrow Key Movement through Fields, the code also Demos how to Cancel that effect by setting KeyCode = 0 for the Arrow Keys (Lines 11, 24, 27, and 29).
Mar 2 '10 #5
Dan2kx
365 Contributor
Thanks ADezii, the Keycode = 0 bit was the most helpfull in my situation.
Mar 3 '10 #6
Annie Bender
15 New Member
I know this is an old thread, but I came across it and wanted to let ADezii know that his KeyCode=0 bit solved a problem for me too. Not altogether sure I know why that works, but it does what is needed. Thanks.

Annie
Apr 26 '10 #7
ADezii
8,834 Recognized Expert Expert
@Annie Bender
Your are quite welcome, Annie. Glad it benefitted you also.
Apr 26 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
9198
by: bardo | last post by:
I have a Datagrid that is inside a panel. I want to use the keyDown event to reconize the arrow keys. But I have no luck at all. The problem is that the keydown event won't fire at all, unless I...
5
1414
by: Randel Bjorkquist | last post by:
Hers's my question. I want the ability to set a property of an object I've created, from outside of the class code (ie... inside of "Main" or an application event), with an enum. Something like...
3
3265
by: trint | last post by:
Ok, I have tried to do this with the System.Web.UI and can't find anything for the webform. It seems much easier for a Winform. Any help in trapping Webform keydown event and keyup event is...
16
3228
by: Merlin | last post by:
I have created a users control with a text box and button on it, what I want to do is override the usercontrol Keydown events with that of the Text box. What is the correct syntax to accomplish...
4
7116
by: ShaneO | last post by:
I would like to handle the KeyUp & KeyDown events in the same event handler but can't find how to determine which event was fired - Private Sub ListBox1_KeyUp(ByVal sender As Object, ByVal e As...
1
3292
by: fripper | last post by:
I have a VB 2005 windows app and I want to recognize keydown events. I have a form key down event handler but it does not get control when a key is depressed. In playing around I found that if I...
4
13498
by: velasquez.m | last post by:
i'm working with visual studio 2005 and have a solution with 16 projects in it. this specific application has a base form with other forms added to it at any area of the application that the...
0
1739
by: tony | last post by:
Hello!! I have a derived class called StringClassEditor which inherit from UITypeEditor listed below. Now to my question in method EditValue in this class I have this statement lb.KeyDown ...
3
3540
by: RobinS | last post by:
I'm trying to learn WPF and do it in VB instead of C#. (God forbid I should do *anything* the easy way. ;-) Here's something weird. On p162-3 of this book by Petzold (in C# of course) in an...
2
19272
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.