hello every1,
i am new to vb.net and was wondering how can i trigger a label_click event on the press of a key.
thanx in advance!

Dani AI

Generated

Short answer: centralize the label's work into a single Sub and call that Sub from both the label Click handler and from a keyboard handler. This follows what suggested and keeps behaviour predictable; the form should be allowed to see keys first as noted (Form.KeyPreview controls that). (Form.KeyPreview docs)

A compact, practical pattern (attach the key handler at run time to avoid Handles/Me oddities):

Private Sub PerformLabelAction()
    ' Put the logic that used to be in the label's Click here.
    MessageBox.Show("Label action")
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    KeyPreview = True
    AddHandler Me.KeyDown, AddressOf Form_KeyDown
End Sub

Private Sub Form_KeyDown(sender As Object, e As KeyEventArgs)
    If e.KeyCode = Keys.L Then
        PerformLabelAction()
        e.SuppressKeyPress = True
    End If
End Sub

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    PerformLabelAction()
End Sub

Using AddHandler + a shared Sub avoids calling a control's event routine directly and also sidesteps the "keyword is not a valid identifier" Handle/Me problem that saw; AddHandler and AddressOf are the runtime alternative to Handles. (AddHandler docs)

Notes on event choice and troubleshooting: KeyPress is for character input (letter keys), KeyDown/KeyUp are for non-character keys and modifiers — pick KeyDown for shortcuts or for detecting function/navigation keys. (KeyDown docs) (KeyPress notes). If a Handles clause is used, the target must be a WithEvents object or a base-class member; that constraint is what commonly produces the BC31412/"keyword is not a valid identifier" errors — using MyBase in designer-generated stubs or using AddHandler are the usual fixes. (Handles clause details)

Checklist: set KeyPreview = True, move shared logic into one Sub, choose KeyDown vs KeyPress per need, attach the handler with AddHandler if the Handles clause is problematic.

Recommended Answers

All 13 Replies

you need to call label_click event in the Key press event.

hello mr das,
actually what exactly i want is, when i click L on my keyboard then all the code written in label1_click even is executed.....i have tried using what you have posted ba its not working....
newaz THANX.

Member Avatar for Member #857553

I did exactly what debasisdas posted and it does exactly what you want.

You do need to set your form's KeyPreview property to true. On the designer click on your form and in the properties window find KeyPreview and set it to true. Or in the load event you could add Me.KeyPreview = True .

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.KeyPreview = True
    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = "l" Then
            Label1_Click(Label1, EventArgs.Empty)
        End If
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        MsgBox("L")
    End Sub

End Class

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

its giving an error on me.keypress and hence not running.

cant we use a keydown event?
earlier in my project i have used keydown event to be invoked on the button shortcuts.

Private Sub Form3_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.l Then
Me.KeyPreview = False
Button1.PerformClick()
End If
End Sub

What error ur getting?

hello every1,
i jst changed the handles me.keypress to handles mybase.keypress
and now its working......
thanx every1 for your help.
and
IT WAS GIVING ERROR on 'me' as "keyword is not a valid identifier".

The above code by Unhnd_Exception works fine...

Then plz could you mark the thread as solved?

oops sry ...new to this site.........and m finding this site very useful:)

how to do that?btw

thank you pgmer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.