Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Raise keydown event

Status
Not open for further replies.

hansaplast

Programmer
Dec 17, 2001
61
NL
I want to raise a keydown event from within a function. How do I do that?

Regards,

Hansa

Whatever you do will be insignificant, but it is very important that you do it. (Mahatma Gandhi)
 
Without really knowing what you are trying to do, you could call the KeyDown method from within another method or function and pass the key as KeyEventArgs.

Example Code. The button just sends the Enter key to the keydown method.

Code:
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            If Not TextBox1.Text.Trim = "" Then
                MsgBox(TextBox1.Text)
            End If
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1_KeyDown(Nothing, New KeyEventArgs(Keys.Enter))
    End Sub
 
He Thanks. This works within the same class.

However, I want to raise TextBox1_KeyDown event from a different class. I can make the event public but since it's an event I don't think its the proper way to do it.

Any suggestions here?

Whatever you do will be insignificant, but it is very important that you do it. (Mahatma Gandhi)
 
[Class 1]

Code:
    Public Event TextBox_KeyDown(ByVal sender as Object, ByVal e as System.Windows.Forms.KeyEventArgs)

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        raiseevent TextBox_KeyDown(sender, e)
    End Sub

[Class 2]
Code:
Private WithEvents myClass as Class1

Private Sub myClass.TextBox_KeyDown(ByVal sender as Object, ByVal e as System.Windows.Forms.KeyEventArgs) Handles myClass.TextBox_KeyDown
   Msgbox e.KeyCode.ToString()
End Sub
 
Err should be Private Sub myClass_TextBox_KeyDown

not

Private Sub myClass.TextBox_KeyDown

I wrote it from memory so no IDE to error check. :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top