Saturday, April 7, 2018

how to make an onscreen keyboard in visual basic .net

This tutorial will show you how to make an onscreen keyboard in visual basic .net.
The most important step in this tutorial is placing the keys(Buttons) on the form. In order to follow with this tutorial, I have included a map that shows the name of each button on the form:
The Red text in the following picture indicates the name of each control. The black text indicates the text property of each button
visual basic .net onscreen keyboard
Below, the picture shows how the Onscreen Keyboard look like:
onscreen keyboard - vb.net
After preparing the form and adding the buttons on it according the same order of the first picture, let's program the buttons:
The following code will handle the click of all the typing keys. It will also check if the Shift key is pressed:

Typing Keys:

Private Sub Button28_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click, _
    Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button48.Click, Button47.Click, _
    Button46.Click, Button45.Click, Button44.Click, Button43.Click, Button42.Click, Button41.Click, _
    Button40.Click, Button4.Click, Button39.Click, Button38.Click, Button37.Click, Button36.Click, _
    Button35.Click, Button34.Click, Button33.Click, Button32.Click, Button31.Click, Button30.Click, _
    Button3.Click, Button29.Click, Button28.Click, Button26.Click, Button25.Click, Button24.Click, _
    Button23.Click, Button22.Click, Button21.Click, Button20.Click, Button2.Click, Button19.Click, _
    Button18.Click, Button17.Click, Button16.Click, Button15.Click, Button14.Click, Button13.Click, _
    Button12.Click, Button11.Click, Button10.Click, Button1.Click 
        If ShiftR.FlatStyle = FlatStyle.Flat Then
            TextBox1.Text = TextBox1.Text + sender.text
            ShiftR.PerformClick()
        Else
 TextBox1.Text = TextBox1.Text + sender.text
        End If
End Sub

Backspace Key

The following code will handle the Backspace button click event:
Private Sub Back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Back.Click

        If TextBox1.Text < " " Then

            TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1 + 1)

        Else

            TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1)

        End If

End Sub

Enter Key

The following code will handle the Enter button click event:
Private Sub Enter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Enter.Click

        TextBox1.Text = TextBox1.Text & Environment.NewLine 

End Sub

Righ and Left Shift Keys

The following code will handle both right and left Shift keys:
Private Sub ShiftR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShiftR.Click, ShiftL.Click

If ShiftR.FlatStyle = FlatStyle.Flat Then

            ShiftR.FlatStyle = FlatStyle.Standard

            ShiftL.FlatStyle = FlatStyle.Standard

            For Each ctl As Control In Me.Controls

                If (ctl.Name.StartsWith("Button")) Then

                    Dim btn As Button = DirectCast(ctl, Button)

                    btn.Text = btn.Text.ToLower

                    Button1.Text = "1"

                    Button2.Text = "2"

                    Button3.Text = "3"

                    Button4.Text = "4"

                    Button5.Text = "5"

                    Button6.Text = "6"

                    Button7.Text = "7"

                    Button8.Text = "8"

                    Button9.Text = "9"

                    Button10.Text = "0"

                    Button11.Text = "-"

                    Button12.Text = "="

                    Button13.Text = "`"

                    Button14.Text = "\"

                    Button15.Text = "]"

                    Button16.Text = "["

                    Button29.Text = "'"

                    Button30.Text = ";"

                    Button28.Text = "/"

                    Button40.Text = "."

                    Button41.Text = ","

                End If 

            Next

        ElseIf ShiftR.FlatStyle = FlatStyle.Standard Then

            ShiftL.FlatStyle = FlatStyle.Flat

            ShiftR.FlatStyle = FlatStyle.Flat

            For Each ctl As Control In Me.Controls

                If (ctl.Name.StartsWith("Button")) Then

                    Dim btn As Button = DirectCast(ctl, Button)

                    btn.Text = btn.Text.ToUpper

                    Button1.Text = "!"

                    Button2.Text = "@"

                    Button3.Text = "#"

                    Button4.Text = "$"

                    Button5.Text = "%"

                    Button6.Text = "^"

                    Button7.Text = "&"

                    Button8.Text = "*"

                    Button9.Text = "("

                    Button10.Text = ")"

                    Button11.Text = "_"

                    Button12.Text = "+"

                    Button13.Text = "~"

                    Button14.Text = "|"

                    Button15.Text = "}"

                    Button16.Text = "{"

                    Button29.Text = """"

                    Button30.Text = ":"

                    Button28.Text = "?"

                    Button40.Text = ">"

                    Button41.Text = "<"

                End If

             Next

        End If

    End Sub

Caps Lock Key:

The followng code will handle the Caps Lock button click event:
Private Sub Caps_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Caps.Click

        If Caps.FlatStyle = FlatStyle.Flat Then

            Caps.FlatStyle = FlatStyle.Standard

            Caps.BackColor = Color.FromKnownColor(KnownColor.Control)

            For Each ctl As Control In Me.Controls

                If (ctl.Name.StartsWith("Button")) Then

                    Dim btn As Button = DirectCast(ctl, Button)

                    btn.Text = btn.Text.ToLower

                    Button1.Text = "1"

                    Button2.Text = "2"

                    Button3.Text = "3"

                    Button4.Text = "4"

                    Button5.Text = "5"

                    Button6.Text = "6"

                    Button7.Text = "7"

                    Button8.Text = "8"

                    Button9.Text = "9"

                    Button10.Text = "0"

                    Button11.Text = "-"

                    Button12.Text = "="

                    Button13.Text = "`"

                    Button14.Text = "\"

                    Button15.Text = "]"

                    Button16.Text = "["

                    Button29.Text = "'"

                    Button30.Text = ";"

                    Button28.Text = "/"

                    Button40.Text = "."

                    Button41.Text = ","

                End If

 

            Next

        ElseIf Caps.FlatStyle = FlatStyle.Standard Then

            Caps.FlatStyle = FlatStyle.Flat

            Caps.BackColor = Color.LightGreen

            For Each ctl As Control In Me.Controls

                If (ctl.Name.StartsWith("Button")) Then

                    Dim btn As Button = DirectCast(ctl, Button)

                    btn.Text = btn.Text.ToUpper

                    Button1.Text = "!"

                    Button2.Text = "@"

                    Button3.Text = "#"

                    Button4.Text = "$"

                    Button5.Text = "%"

                    Button6.Text = "^"

                    Button7.Text = "&"

                    Button8.Text = "*"

                    Button9.Text = "("

                    Button10.Text = ")"

                    Button11.Text = "_"

                    Button12.Text = "+"

                    Button13.Text = "~"

                    Button14.Text = "|"

                    Button15.Text = "}"

                    Button16.Text = "{"

                    Button29.Text = """"

                    Button30.Text = ":"

                    Button28.Text = "?"

                    Button40.Text = ">"

                    Button41.Text = "<"

                End If

 

            Next

        End If

    End Sub

The Tab Key:

The following code will handle the Tab key click event:
Private Sub Tab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tab.Click

        TextBox1.Text = TextBox1.Text & "   "

    End Sub 

The Space Key

The following code will handle the Space key click event:
Private Sub Space_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Space.Click

        TextBox1.Text = TextBox1.Text & " "

    End Sub

No comments:

Computer Terms

The termination of the process due to a program or system fault      -      Abend(abnormal ending) To terminate a process before completion....