dcsimg
Results 1 to 8 of 8

Thread: Function Key F10

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    6

    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.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,073

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    6

    Re: Function Key F10

    Quote Originally Posted by jmcilhinney View Post
    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.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,508

    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

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,626

    Re: Function Key F10

    You are typing in the Code textbox before pressing F10, correct?

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    6

    Re: Function Key F10

    Quote Originally Posted by passel View Post
    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

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,508

    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

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,626

    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
  •  



Featured


Click Here to Expand Forum to Full Width