Results 1 to 15 of 15

Thread: [RESOLVED] Stopping a label from receiving focus

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    23

    Resolved [RESOLVED] Stopping a label from receiving focus

    Hi,

    I am working on a data entry type form and I want the enter key to move focus to the next text box. I've got that working great except that the focus is moving to the label that is, in sequence, between the text boxes.

    In VB6 I didn't have to worry about this because the label couldn't receive focus, in VB.Net it does and I'm not sure how to stop it from receiving focus.

    Any ideas?

  2. #2

    Re: Stopping a label from receiving focus

    Why not use Tab and change the Tab Orders of each Control? Other than that, I have no clue.

  3. #3
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Stopping a label from receiving focus

    No, the label doesn't recieve focus. VB.NET is better than VB6 in every way!!!!
    Are you finding the next control and setting its focus? Can you post your code please?

  4. #4
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Stopping a label from receiving focus

    No, the label doesn't recieve focus. VB.NET is better than VB6 in every way!!!!
    Says the guy who won't upgrade from VS 2005 and XP to 2008 and Win7
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    23

    Re: Stopping a label from receiving focus

    Sure, here is my code:

    If e.KeyCode = Windows.Forms.Keys.Enter Then
    Dim NextControl As Windows.Forms.Control = GetNextControl(ActiveControl, True)
    If Not NextControl Is Nothing Then
    NextControl.Focus()
    End If
    e.SuppressKeyPress = True
    e.Handled = True
    End If

    If I breakpoint the code, after I press enter in the first text box, a label control has focus rather than the next textbox.

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Stopping a label from receiving focus

    Well, one option may to to check if the 'next' control is a Label, if so move on to the next.

  7. #7
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Stopping a label from receiving focus

    Why can't you just change the TapStop property of the Label to False? or am I missing the point?
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: Stopping a label from receiving focus

    Just curious, what is your code for moving from one control to another with the enter key.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    23

    Re: Stopping a label from receiving focus

    weirddemon, I don't believe that a label has a tabstop property, that is part of the issue.

    I ended up changing my code and removing it from the form and creating the following routine

    Private Sub HandleEnterKey(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUserID.KeyDown, txtName.KeyDown, txtEmail.KeyDown, txtPassword.KeyDown, txtConfirmPassword.KeyDown

    If e.KeyCode = Windows.Forms.Keys.Enter Then
    Dim CurrentControl As System.Windows.Forms.Control = CType(sender, System.Windows.Forms.Control)
    Me.SelectNextControl(CurrentControl, True, True, True, True)

    'Dim NextControl As Windows.Forms.Control = GetNextControl(ActiveControl, True)
    'If Not NextControl Is Nothing Then
    ' NextControl.Focus()
    'End If
    e.SuppressKeyPress = True
    e.Handled = True
    End If

    End Sub

    it works great.

  10. #10
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: [RESOLVED] Stopping a label from receiving focus

    weirddemon, I don't believe that a label has a tabstop property, that is part of the issue.
    Did you check in code? it's not in the designer, but I was able to set it in code.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  11. #11
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: [RESOLVED] Stopping a label from receiving focus

    I was trying to use this to duplicate the problem but couldn't get it to go to a label. I didn't know you could set the focus to a label?
    Code:
        'set forms keypreview to true first
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = 13 Then
                SendKeys.Send("{TAB}")
            End If
        End Sub
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  12. #12
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: [RESOLVED] Stopping a label from receiving focus

    Quote Originally Posted by weirddemon View Post
    Did you check in code? it's not in the designer, but I was able to set it in code.
    I was able to set it in code as well but still it wouldn't get the focus when running the program?
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  13. #13
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: [RESOLVED] Stopping a label from receiving focus

    I was able to set it in code as well but still it wouldn't get the focus when running the program?
    Yeah. Same here. I couldn't get the Label to receive the focus. But, I saw the property was available, so I thought to mention it to the OP.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  14. #14
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: [RESOLVED] Stopping a label from receiving focus

    Interesting. If you run this with a form that has a label it tries to set the focus to the label. I couldn't tell if the label actually had the focus or not since it appeared none of the controls had focus?

    Code:
        Dim NextControl As Windows.Forms.Control = GetNextControl(ActiveControl, True)
            If Not NextControl Is Nothing Then
                NextControl.Focus()
                MsgBox(NextControl.Name)
            End If
            e.SuppressKeyPress = True
            e.Handled = True
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  15. #15
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Stopping a label from receiving focus

    Quote Originally Posted by weirddemon View Post
    Says the guy who won't upgrade from VS 2005 and XP to 2008 and Win7
    Yup, says that guy.

    Is this actually resolved now? I can't tell.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width