1

我正在创建一个虚拟字母数字键盘(屏幕截图:https ://www.dropbox.com/s/rmlmct30bnvihkx/keypad.png ),需要一些帮助来处理它背后的代码。本练习的全部目的是使用 vb.net(在 Visual Studio 2010 中)创建此应用程序,并让它像手机一样在文本框中输入文本。这个应用程序将在带有触摸屏的计算机上运行。我已经能够成功编写此键盘的代码,以按以下方式运行:

1) 用户首先选择与他们想要输入的 3 个字母之一相关的数字,如果他们需要输入 A、B 或 C,EG 用户选择 1。然后在“数字”按钮的左侧出现 3 个框,其中包含值与相应的号码相关联。

2) 然后用户选择其中一个字母并将其添加到文本框中并重复1中的过程。

1 按钮的代码示例:

    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

相应填充值的 3 个空白框的代码示例:

 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

但这被证明是一个有点乏味的方法,好吧修正一个非常乏味的文本输入方法,所以我想尝试制作一个类似于手机的键盘。

我只需要一个按钮 (ABC/1) 的代码示例,其余的我会解决。预先感谢您的协助。

(这是一个窗体应用程序)

问候,

卡维尔·马哈拉杰。

4

1 回答 1

0

嘿伙计们 :) 首先感谢所有查看我的问题的人,特别是 Idle_Mind 的建议。我能够找到一个解决方案并得到我需要的东西,它仍然非常“Buggy”,还有很多事情需要解决,但核心概念已经实现。解决方案的代码如下(请原谅代码中的注释,因为我在一切正常工作后进行清理):

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

结束类

用于测试的 GUI 截图:https ://www.dropbox.com/s/8s26po807v6kkoj/keypad-test%20Interface.png

问候,

卡维尔。

于 2013-07-03T08:30:28.330 回答