Author Topic: Call KeyDown event from button  (Read 1151 times)

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Call KeyDown event from button
« on: October 11, 2018, 10:39:29 AM »
I have a basic vb question that I cannot get right.  I want to call the KeyDown event of a textbox with a button click.  This code throws an "Invalid Cast exception was unhandled" when I press the button.  I assume I do not have the correct arguments but not sure what to use.  Any help is appreciated, Thanks.

Code: [Select]
Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click   'give the user the option to click a "connect" button
        tbxIPaddress_KeyDown(sender, New System.EventArgs())

    End Sub

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Call KeyDown event from button
« Reply #1 on: October 11, 2018, 10:52:18 AM »
Try this:
Code: [Select]
tbxIPaddress_KeyDown(sender, New System.EventArgs.Empty)

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Call KeyDown event from button
« Reply #2 on: October 11, 2018, 11:48:52 AM »
that doesn't seem to compile.  I get the blue squiggly under .Empty from intellisense.

Here is the details of the exception from the original code

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Call KeyDown event from button
« Reply #3 on: October 11, 2018, 12:04:11 PM »
I should have left off the keyword "new"
Code: [Select]
tbxIPaddress_KeyDown(sender, System.EventArgs.Empty)

or maybe even this:
Code: [Select]
New System.Windows.Forms.KeyEventArgs(Keys.A)
« Last Edit: October 11, 2018, 01:22:15 PM by Archie »

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Call KeyDown event from button
« Reply #4 on: October 11, 2018, 01:21:37 PM »
the later worked by substituting key.A for key.Enter.  Thank you

Code: [Select]
New System.Windows.Forms.KeyEventArgs(Keys.Enter)