none
LinkLabel Recieves LinkClicked Event When Enter Key is Pressed RRS feed

  • Question

  • I have a project where I would like to use multiple LinkLabels but I find that hitting the enter key triggers the LinkLabel's LinkClicked event. How can I prevent this from happening? Thanks.
    Sunday, June 25, 2017 6:39 AM

Answers

  •  If you have a LinkLabel control which is suppose to have focus when the Form loads,  it will not draw the focused rectangle around it.  Not sure if this is a bug or what but,  you could use the Paint event of that LinkLabel to make sure a focus rectangle is drawn around it when it has focus,  even when the form is first loaded.

     For example,  i have 3 LinkLabels on a form and LinkLabel1 gets focus when the form loads....

    Public Class Form1
    
        Private Sub LinkLabel1_Paint(sender As Object, e As PaintEventArgs) Handles LinkLabel1.Paint
            If LinkLabel1.Focused Then
                ControlPaint.DrawFocusRectangle(e.Graphics, New Rectangle(0, 0, LinkLabel1.Width, LinkLabel1.Height))
            End If
        End Sub
    
    End Class
    

     

     


    If you say it can`t be done then i`ll try it

    • Marked as answer by PeterC28 Saturday, July 1, 2017 9:23 PM
    Friday, June 30, 2017 12:08 AM

All replies

  • I have a project where I would like to use multiple LinkLabels but I find that hitting the enter key triggers the LinkLabel's LinkClicked event. How can I prevent this from happening? Thanks.
    Enter will only be processed as a mouse click on the control if the control has the focus, which is what the user would expect.  If you want the Enter key to do something else then the typical solution would be to move the focus away from the link label. Otherwise, you should retain the default functionality.
    Sunday, June 25, 2017 7:06 AM
  • As long as you don't use it nothing happens, and if you use it set simple a switch and tell not to handle it. 

    Basic programming.


    Success
    Cor

    Sunday, June 25, 2017 8:59 AM
  •  Stopping the Enter key from activating the LinkLabels goes against the grain of how a LinkLabel is designed to work and how a user would expect them to work.  If you still really want them to not respond when they have focus and the Enter key is pressed,  then try the below...

    Public Class Form1
    
        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
            If TypeOf ActiveControl Is LinkLabel AndAlso keyData = Keys.Enter Then Return True
            Return MyBase.ProcessCmdKey(msg, keyData)
        End Function
    
    End Class
    


    If you say it can`t be done then i`ll try it

    Sunday, June 25, 2017 10:14 AM
  • I have a project where I would like to use multiple LinkLabels but I find that hitting the enter key triggers the LinkLabel's LinkClicked event. How can I prevent this from happening? Thanks.

    Enter will only be processed as a mouse click on the control if the control has the focus, which is what the user would expect.  If you want the Enter key to do something else then the typical solution would be to move the focus away from the link label. Otherwise, you should retain the default functionality.

    I wouldn't mind that if the focus actually showed when the form loaded. I have to hit the tab key to show the focus and that sends the focus to the second link.
    Sunday, June 25, 2017 8:14 PM
  • I wouldn't mind that if the focus actually showed when the form loaded. I have to hit the tab key to show the focus and that sends the focus to the second link.
    In the Form.Shown event handler, set the focus how you want:
    https://msdn.microsoft.com/en-us/library/system.windows.forms.form.shown(v=vs.110).aspx
    https://msdn.microsoft.com/en-us/library/7wt11hea(v=vs.110).aspx
    Sunday, June 25, 2017 9:45 PM
  • I wouldn't mind that if the focus actually showed when the form loaded. I have to hit the tab key to show the focus and that sends the focus to the second link.

    In the Form.Shown event handler, set the focus how you want:
    https://msdn.microsoft.com/en-us/library/system.windows.forms.form.shown(v=vs.110).aspx
    https://msdn.microsoft.com/en-us/library/7wt11hea(v=vs.110).aspx

    I followed your advice as follows:

    Private Sub frmMain_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            If m_aoChoiceLinks.Length > 1 Then
                If m_aoChoiceLinks(1).CanSelect Then
                    m_aoChoiceLinks(1).Select()
                End If
                If m_aoChoiceLinks(0).CanSelect Then
                    m_aoChoiceLinks(0).Select()
                End If
            End If
        End Sub

    But I find that even selecting one LinkLabel, then another, that I still see no focus on either link. Any help would be appreciated.

    Wednesday, June 28, 2017 11:56 PM
  • But I find that even selecting one LinkLabel, then another, that I still see no focus on either link. Any help would be appreciated.

    That's probably because when you select the linklabel some code executes that causes the focus to move away from the linklabel, so it is no longer the active label.  If you tab through the labels without selecting them, then the highlight stays with the label:

    If you want the label to remain the active label after that code executes, then you need to set it as the active label at the end of that code.

    Thursday, June 29, 2017 12:43 AM
  • The current problem is as follows: When the form loads it would appear as if no items have focus. The first LinkLabel recieves a LinkClicked when the Enter key is pressed even when it does not have the focus outline. A Select statement in the Form_Shown subroutine fails to show the focus outline on either the first or second LinkLabel (both have been tested individually). Placing a Button on the form in which a second LinkLabel is Selected fails to work. Hitting the tab key sets the focus to the second link, and then the button, followed by the first link. Hitting the button after the focus have been moved via tabbing to either to the button or the first LinkLabel shows the focus on the second LinkLabel. What I am trying to accomplish at this point is either have no focus showing and no LinkClicked when the enter is hit, or to see the focus on a LinkLabel when the form shows.
    Thursday, June 29, 2017 10:35 PM
  • What I am trying to accomplish at this point is either have no focus showing and no LinkClicked when the enter is hit, or to see the focus on a LinkLabel when the form shows.

    Sounds to me like you have the tab order set incorrectly.  Set the tab order to what you want for the link labels.  If there are no other controls on the form that might receive an Enter key press then the first item in the tab order that will process an Enter key will receive it, whether selected or not.  You might be able to get around this by setting KeyPreview in the form to True. You can't solve it with a button that sets the focus because a button is a control, and it will reset the focus to itself.   But getting your tab order correct is the starting point. You can't have no focus showing - the user won't know what happens when they press a key.

    Thursday, June 29, 2017 11:39 PM
  •  If you have a LinkLabel control which is suppose to have focus when the Form loads,  it will not draw the focused rectangle around it.  Not sure if this is a bug or what but,  you could use the Paint event of that LinkLabel to make sure a focus rectangle is drawn around it when it has focus,  even when the form is first loaded.

     For example,  i have 3 LinkLabels on a form and LinkLabel1 gets focus when the form loads....

    Public Class Form1
    
        Private Sub LinkLabel1_Paint(sender As Object, e As PaintEventArgs) Handles LinkLabel1.Paint
            If LinkLabel1.Focused Then
                ControlPaint.DrawFocusRectangle(e.Graphics, New Rectangle(0, 0, LinkLabel1.Width, LinkLabel1.Height))
            End If
        End Sub
    
    End Class
    

     

     


    If you say it can`t be done then i`ll try it

    • Marked as answer by PeterC28 Saturday, July 1, 2017 9:23 PM
    Friday, June 30, 2017 12:08 AM
  •  If you have a LinkLabel control which is suppose to have focus when the Form loads,  it will not draw the focused rectangle around it.  Not sure if this is a bug or what but,  you could use the Paint event of that LinkLabel to make sure a focus rectangle is drawn around it when it has focus,  even when the form is first loaded.

     For example,  i have 3 LinkLabels on a form and LinkLabel1 gets focus when the form loads....

    Public Class Form1
    
        Private Sub LinkLabel1_Paint(sender As Object, e As PaintEventArgs) Handles LinkLabel1.Paint
            If LinkLabel1.Focused Then
                ControlPaint.DrawFocusRectangle(e.Graphics, New Rectangle(0, 0, LinkLabel1.Width, LinkLabel1.Height))
            End If
        End Sub
    
    End Class
    

     

     


    If you say it can`t be done then i`ll try it

    This helped a lot. Thanks.
    Saturday, July 1, 2017 9:23 PM