What is the equivalent of the VB6 SendKeys in VB.NET?

esposito

Well-known member
Joined
Jul 11, 2003
Messages
103
Location
Perugia - Italy
Hello, everybody. I have been studying the syntax of VB.NET not for a long time and Im trying to convert into this new language the most important functions I used to implement in VB6.

Can anybody tell me the equivalent of the VB6 "SendKeys" statement under VB.NET? In other words, do you know how to simulate the pressure of the keyboard keys?

An example in VB6 could be

If KeyAscii = 13 Then SendKeys "{Tab}"

Do you know how to "translate" the statement above into VB.NET?

Thanks in advance for your help.
 
PROBLEM SOLVED, at least partially. I still need to know the equivalent of

KeyAscii = 0

that, in VB6, allows you to cancel the pressure of a keyboard key.

Any suggestions?

Originally posted by esposito
Hello, everybody. I have been studying the syntax of VB.NET not for a long time and Im trying to convert into this new language the most important functions I used to implement in VB6.

Can anybody tell me the equivalent of the VB6 "SendKeys" statement under VB.NET? In other words, do you know how to simulate the pressure of the keyboard keys?

An example in VB6 could be

If KeyAscii = 13 Then SendKeys "{Tab}"

Do you know how to "translate" the statement above into VB.NET?

Thanks in advance for your help.
 
to achieve the same result in a textbox use...
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
	e.Handled = True
End Sub
 
Thanks, PlausiblyDamp, you have solved my problem. I just wanted to prevent the users from pressing any keys different from numbers, giving them the possibility of using Backspace to make corrections and the Tab or Enter key to move the focus to another object.
This is the code I have written:

[VB]

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

The user can press Enter.
If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
e.Handled = False
MsgBox("You pressed Enter.")
Exit Sub
End If

The user can press Backspace to make corrections.
If e.KeyChar = Microsoft.VisualBasic.ChrW(8) Then
e.Handled = False
Exit Sub
End If

The user can press Tab.
If e.KeyChar = Microsoft.VisualBasic.ChrW(9) Then
e.Handled = False
Exit Sub
End If

The user is not allowed to type in letters.
If e.KeyChar <> "1" And e.KeyChar <> "2" And e.KeyChar <> "3" And e.KeyChar <> "4" And e.KeyChar <> "5" And e.KeyChar <> "6" And e.KeyChar <> "7" And e.KeyChar <> "8" And e.KeyChar <> "9" And e.KeyChar <> "0" Then
e.Handled = True
MessageBox.Show("Please use only numbers.", "Invalid data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If

End Sub

[/VB]

My only doubt is this: does the statement Microsoft.VisualBasic.ChrW(8) etc. refer to a DLL that could not be supported by implementations of the .NET platform for different operating systems? (Im thinking about the Mono Project)

Is there any other statement I could use instead of Microsoft.VisualBasic.ChrW(8) to refer to the Enter key, Backspace and Tab?

TIA
 
I use Select Case in KeyPresses myself, looks better and easier to understand. Btw, Chr() is the function I use for getting Chars, heres what I got:

Code:
Private Sub txtNumbersOnly_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSoloNumeri.KeyPress

    Select Case e.KeyChar

        The user can press Enter.
        Case Chr(13)
            e.Handled = False
            MsgBox("You pressed Enter.")
            Exit Sub

        The user can press Backspace to make corrections.
       Case Chr(8)
            e.Handled = False
            Exit Sub
        End If

        The user can press Tab.
        Case Chr(9)
            e.Handled = False
            Exit Sub
    End Select

        The user is not allowed to type in letters.
        If e.KeyChar <> "1" And e.KeyChar <> "2" And e.KeyChar <> "3" And e.KeyChar <> "4" And e.KeyChar <> "5" And e.KeyChar <> "6" And e.KeyChar <> "7" And e.KeyChar <> "8" And e.KeyChar <> "9" And e.KeyChar <> "0" Then
            e.Handled = True
            MessageBox.Show("Please use only numbers.", "Invalid data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

    End Sub
 
Thanks, Darc. Youre right, it looks better and easier than the code I used.

Originally posted by Darc
I use Select Case in KeyPresses myself, looks better and easier to understand. Btw, Chr() is the function I use for getting Chars, heres what I got:

Code:
Private Sub txtNumbersOnly_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSoloNumeri.KeyPress

    Select Case e.KeyChar

        The user can press Enter.
        Case Chr(13)
            e.Handled = False
            MsgBox("You pressed Enter.")
            Exit Sub

        The user can press Backspace to make corrections.
       Case Chr(8)
            e.Handled = False
            Exit Sub
        End If

        The user can press Tab.
        Case Chr(9)
            e.Handled = False
            Exit Sub
    End Select

        The user is not allowed to type in letters.
        If e.KeyChar <> "1" And e.KeyChar <> "2" And e.KeyChar <> "3" And e.KeyChar <> "4" And e.KeyChar <> "5" And e.KeyChar <> "6" And e.KeyChar <> "7" And e.KeyChar <> "8" And e.KeyChar <> "9" And e.KeyChar <> "0" Then
            e.Handled = True
            MessageBox.Show("Please use only numbers.", "Invalid data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

    End Sub
 
Back
Top