-
Feb 6th, 2018, 04:47 AM
#1
Thread Starter
New Member
Function Key F10
Hi
My Code is as follows. I have set the form's Keypreview to True,
but when I press "F10" the focus shifts to the form itself.
Code:
Private Sub txtCode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCode.KeyDown
If e.KeyCode = Keys.F10 Then
txtPassword.Visible = True
txtPassword.Text = ""
txtPassword.Focus()
End If
End Sub
I cannot seem to find a solution.
All help will be appreciated.
Last edited by Shaggy Hiker; Feb 13th, 2018 at 02:57 PM.
Reason: Added CODE tags.
-
Feb 6th, 2018, 07:18 AM
#2
Re: Function Key F10
Firstly, whether KeyPreview is True or not is irrelevant because you're not handling an event of the form.
Secondly, don't call Focus. As the documentation says, call Select.
Thirdly, I just tested your code (with Focus and with Select) on a form with just two TextBoxes and nothing else and it worked fine, so there's something else going on in your app. I suggest that you strip it down to the bare minimum, check whether that works and then build it back up slowly to exactly what you want and make a note where it breaks, if it does indeed break.
-
Feb 13th, 2018, 02:53 PM
#3
Thread Starter
New Member
Re: Function Key F10
Originally Posted by jmcilhinney
Firstly, whether KeyPreview is True or not is irrelevant because you're not handling an event of the form.
Secondly, don't call Focus. As the documentation says, call Select.
Thirdly, I just tested your code (with Focus and with Select) on a form with just two TextBoxes and nothing else and it worked fine, so there's something else going on in your app. I suggest that you strip it down to the bare minimum, check whether that works and then build it back up slowly to exactly what you want and make a note where it breaks, if it does indeed break.
I have tried using a new blank form with two textboxes and nothing else. It still does not work
Last edited by Shaggy Hiker; Feb 13th, 2018 at 02:58 PM.
Reason: Fixed the missing QUOTE tag.
-
Feb 13th, 2018, 03:01 PM
#4
Re: Function Key F10
So, just to confirm: You created a new form with two textboxes named txtCode and txtPassword. You then ran that code and when you pressed the F10 key, the focus did not go to txtPassword, right?
The first thing to do is to put a breakpoint on the line where you set the focus. Is it reached? If it is, then that would be good to know. If it is not, then you're a good ways towards identifying the problem.
My usual boring signature: Nothing
-
Feb 14th, 2018, 10:43 AM
#5
Re: Function Key F10
You are typing in the Code textbox before pressing F10, correct?
-
Feb 14th, 2018, 02:36 PM
#6
Thread Starter
New Member
Re: Function Key F10
Originally Posted by passel
You are typing in the Code textbox before pressing F10, correct?
No, however if I put in a message box after pressing F10 it seems to work
-
Feb 14th, 2018, 04:57 PM
#7
Re: Function Key F10
Don't be debugging by messagebox. At best, that gives you only modest information about one point in time. Set a breakpoint and work from there. That lets you look at any variable that is in scope, examine statements, step through code, and so on.
If you aren't familiar with using breakpoints, then ask about that, or search for it.
My usual boring signature: Nothing
-
Feb 15th, 2018, 02:25 AM
#8
Re: Function Key F10
The reason I asked if you were typing in the Code textbox is because it isn't clear to me that you made the changes that jmcilhinney pointed out.
Since you had code in the Event for the Code textbox, the Code textbox is the textbox you would have to be typing in for it to trigger the event that would see the F10 key, i.e. that textbox would have to have the focus.
What jmcihinney said is that that code should be moved to the Form's keydown event handler (or have the handler you have also handle the form's keydown event, i.e. add another event to the Handles clause of the sub), if you want the F10 key to always move the focus to password textbox (assuming keypreview set true).
Also, be aware that Windows has default actions associated with many function key combinations which can cause a side effect which may affect your expectations.
In particular, F10 is useful for a couple of situations where you might want to do something you would normally need your mouse for.
If you have a menubar in an application and you hit the F10 key you should see the first item in the Menubar highlight, and you can then use your arrow keys, Enter and other keys to navigate the menubar selections.
If you press Shift F10 you should get the popup menu that would normally popup when you press the right mouse button, and again you can use other keys to navigate the popup menu in place of using the mouse.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|