perfect in c# but facing error in vb.net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I am running a code in which on a button press event i programmatically
dropdown combobox by passing it F4 key the same code is running perfect in c#
but in vb.net i m getting exception "NotSupportedException"

---vb.net code is---

<DllImport("coredll.dll")> _
Public Function keybd_event( _
ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Integer, _
ByVal dwExtraInfo As Integer)
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
ComboBox1.Focus()
keybd_event(CByte(Keys.F4), 0, 0, 0)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

---c# code is---

[DllImport("coredll.dll")]
public static extern void keybd_event(byte bVk, byte
bScan, int dwFlags, int dwExtraInfo);

private void button1_Click(object sender, System.EventArgs e)
{
ComboBox1.Focus();
keybd_event((byte)Keys.F4, 0, 0, 0);
}

any idea?
 
Your cast from a Keys enumeration to a byte is probably the issue since the
Keys enumeration is an Int32 (Integer) type. Try using CType(Keys.F4,
GetType(Byte)) and if that doesn't work try a two stage approach convert to
an Integer first then to a Byte.

Peter
 
Try changing the declare to a Sub (not Function).

Also PLEASE turn option strict on at the project level and deal with the
warnings.

Cheers
Daniel
 
Back
Top