Search code examples
vb.netnumericalphanumericvirtual-keyboardkeypad

Creating an Alphanumeric keypad using vb.net


I'm creating a virtual alphanumeric keypad (Screenshot: https://www.dropbox.com/s/rmlmct30bnvihkx/keypad.png ) and need some assistance with the code behind it. The whole aim of this exercise is to create this application using vb.net (in visual studio 2010) and have it enter text into the text box like a cellphone would. This app is going to be run on a computer with a touch screen. I have been able to successfully write the code for this keypad to function in the following manner:

1) User first selects the number associated with one of the 3 alphabets they want typed, EG user selects 1 if they need to type A, B, or C. 3 Boxes then appear to the left of the "Num" button with the values associated with the respective number.

2) The user then selects one of the alphabets and it is added to the text box and the process in 1 is repeated.

Code sample for the 1 button:

    Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    Dim cursorPos As Integer = _SourceControl.SelectionStart
    If numlock = False Then
        btnAlpha1.Visible = True
        btnAlpha1.Text = "A"
        btnAlpha2.Visible = True
        btnAlpha2.Text = "B"
        btnAlpha3.Visible = True
        btnAlpha3.Text = "C"
    ElseIf numlock = True Then
        _sourceForm.ActiveControl = _SourceControl
        _SourceControl.SelectedText += "1"
        _SourceControl.Select(cursorPos + 1, 0)
    End If

End Sub

Code sample for the 3 blank boxes that populate the values accordingly:

 Private Sub btnAlpha3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAlpha3.Click
    Dim cursorPos As Integer = _SourceControl.SelectionStart
    _sourceForm.ActiveControl = _SourceControl

    _SourceControl.SelectedText += btnAlpha3.Text
    _SourceControl.Select(cursorPos + 1, 0)
End Sub

Private Sub btnAlpha2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAlpha2.Click
    Dim cursorPos As Integer = _SourceControl.SelectionStart
    _sourceForm.ActiveControl = _SourceControl

    _SourceControl.SelectedText += btnAlpha2.Text
    _SourceControl.Select(cursorPos + 1, 0)
End Sub

Private Sub btnAlpha1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAlpha1.Click
    Dim cursorPos As Integer = _SourceControl.SelectionStart
    _sourceForm.ActiveControl = _SourceControl

    _SourceControl.SelectedText += btnAlpha1.Text
    _SourceControl.Select(cursorPos + 1, 0)
End Sub

But this proves to be a bit of a tedious method of , ok correction a very tedious method of text input so I want to try and make a keypad similar to a cellphones.

All I need is a sample of code for just one button (ABC/1) and I will work out the rest. Thank you in advance for your assistance.

(This is a windows forms Application)

Regards,

Kavir Maharaj.


Solution

  • Hey Guys :) firstly thanks to all that viewed my question and especially to Idle_Mind for the suggestion. I was able to find a solution and get what I need, Its still very "Buggy" and there are alot of things to sort out but the core concept was achieved. The code to the solution is as follows (Please excuse the comments in code as I do cleanup after getting everything working as it should):

    Public Class Form1
    Dim WithEvents intTimer As New System.Timers.Timer
    Dim hitCounter As Integer = 1
    Dim value As String = ""
    
    Public Sub startTimer()
        intTimer.Interval = 1500
        intTimer.Start()
    End Sub
    
    Public Sub setText_1()
        If Me.txtInput.InvokeRequired Then
            Me.txtInput.Invoke(New MethodInvoker(AddressOf setText_1))
        Else
            Dim cursorPos As Integer = txtInput.SelectionStart
            txtInput.Select(cursorPos + 1, 0)
            Me.ActiveControl = txtInput
            hitCounter = 1
            value = ""
        End If
    End Sub
    
    Public Sub timer_Elapsed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles intTimer.Elapsed
        setText_1()
        intTimer.Stop()
    End Sub
    
    Public Sub BtnLoseFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Leave, btn2.Leave, btn3.Leave, btn4.Leave, btn5.Leave, btn6.Leave, btn7.Leave, btn8.Leave, btn9.Leave
        txtInput.SelectedText += value
        hitCounter = 1
    End Sub
    
    Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
        If btn1.Focused Then
            startTimer()
            If hitCounter <= 3 Then
                Select Case hitCounter
                    Case 1
                        'txtInput.Text += "A"
                        value = "A"
                    Case 2
                        'txtInput.Text = "B"
                        value = "B"
                    Case 3
                        'txtInput.Text += "C"
                        value = "C"
                    Case Else
                        'txtInput.SelectedText += "1"
                        value = "1"
                End Select
                hitCounter += 1
            End If
        Else
            hitCounter = 1
        End If
    End Sub
    
    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
        'hitCounter = 1
        If btn2.Focused Then
            startTimer()
            If hitCounter <= 3 Then
                Select Case hitCounter
                    Case 1
                        'txtInput.Text += "A"
                        value = "D"
                    Case 2
                        'txtInput.Text = "B"
                        value = "E"
                    Case 3
                        'txtInput.Text += "C"
                        value = "F"
                    Case Else
                        'txtInput.SelectedText += "1"
                        value = "2"
                End Select
                hitCounter += 1
            End If
        End If
    End Sub
    
    Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
        'hitCounter = 1
        startTimer()
        If hitCounter <= 3 Then
            Select Case hitCounter
                Case 1
                    'txtInput.Text += "A"
                    value = "G"
                Case 2
                    'txtInput.Text = "B"
                    value = "H"
                Case 3
                    'txtInput.Text += "C"
                    value = "I"
                Case Else
                    'txtInput.SelectedText += "1"
                    value = "3"
            End Select
            hitCounter += 1
        End If
    End Sub
    
    Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
        'hitCounter = 1
        startTimer()
        If hitCounter <= 3 Then
            Select Case hitCounter
                Case 1
                    'txtInput.Text += "A"
                    value = "J"
                Case 2
                    'txtInput.Text = "B"
                    value = "K"
                Case 3
                    'txtInput.Text += "C"
                    value = "L"
                Case Else
                    'txtInput.SelectedText += "1"
                    value = "4"
            End Select
            hitCounter += 1
        End If
    End Sub
    
    Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
        'hitCounter = 1
        startTimer()
        If hitCounter <= 3 Then
            Select Case hitCounter
                Case 1
                    'txtInput.Text += "A"
                    value = "M"
                Case 2
                    'txtInput.Text = "B"
                    value = "N"
                Case 3
                    'txtInput.Text += "C"
                    value = "O"
                Case Else
                    'txtInput.SelectedText += "1"
                    value = "5"
            End Select
            hitCounter += 1
        End If
    End Sub
    
    Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
        'hitCounter = 1
        startTimer()
        If hitCounter <= 3 Then
            Select Case hitCounter
                Case 1
                    'txtInput.Text += "A"
                    value = "P"
                Case 2
                    'txtInput.Text = "B"
                    value = "Q"
                Case 3
                    'txtInput.Text += "C"
                    value = "R"
                Case Else
                    'txtInput.SelectedText += "1"
                    value = "6"
            End Select
            hitCounter += 1
        End If
    End Sub
    
    Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
        'hitCounter = 1
        startTimer()
        If hitCounter <= 3 Then
            Select Case hitCounter
                Case 1
                    'txtInput.Text += "A"
                    value = "S"
                Case 2
                    'txtInput.Text = "B"
                    value = "T"
                Case 3
                    'txtInput.Text += "C"
                    value = "U"
                Case Else
                    'txtInput.SelectedText += "1"
                    value = "7"
            End Select
            hitCounter += 1
        End If
    End Sub
    
    Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
        'hitCounter = 1
        startTimer()
        If hitCounter <= 3 Then
            Select Case hitCounter
                Case 1
                    'txtInput.Text += "A"
                    value = "V"
                Case 2
                    'txtInput.Text = "B"
                    value = "W"
                Case 3
                    'txtInput.Text += "C"
                    value = "X"
                Case Else
                    'txtInput.SelectedText += "1"
                    value = "8"
            End Select
            hitCounter += 1
        End If
    End Sub
    
    Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
        'hitCounter = 1
        startTimer()
        If hitCounter <= 2 Then
            Select Case hitCounter
                Case 1
                    'txtInput.Text += "A"
                    value = "Y"
                Case 2
                    'txtInput.Text = "B"
                    value = "Z"
                Case Else
                    'txtInput.SelectedText += "1"
                    value = "9"
            End Select
            hitCounter += 1
        End If
    End Sub
    
    Private Sub btnBackSpace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackSpace.Click
        Dim cursorPos As Integer = txtInput.SelectionStart
        If txtInput.Text.Length > 0 Then
            Me.ActiveControl = txtInput
            txtInput.Text = txtInput.Text.Remove(cursorPos - 1, 1)
            txtInput.Select(cursorPos - 1, 0)
        Else
            'Do nothing
            Me.ActiveControl = txtInput
        End If
    End Sub
    
    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
        'spacebar
        Dim cursorPos As Integer = txtInput.SelectionStart
        Me.ActiveControl = txtInput
        txtInput.SelectedText += " "
        txtInput.Select(cursorPos + 1, 0)
    End Sub
    

    End Class

    A screenshot of the GUI used for testing: https://www.dropbox.com/s/8s26po807v6kkoj/keypad-test%20Interface.png

    Regards,

    Kavir.