0

嘿,我需要一些帮助,让我的代码像我需要的那样正常工作。下面是我的代码,当用户单击文本框时,它会弹出一个键盘,他们可以在其中单击任何字母,并将该字母输入文本框。问题是我似乎无法让文本框的名称返回,以便它知道将信件发送到哪里。

射击顺序为:

TextBox1_MouseDown

keyboardOrPad.runKeyboardOrPad

kbOrPad.keyboardPadType

点击信

Form1.putIntoTextBox

表格1

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
    Call keyboardOrPad.runKeyboardOrPad("SHOW") 'Just shows the keyboard
    Call kbOrPad.keyboardPadType("PAD", TextBox1)
End Sub

Public Sub putIntoTextBox(ByRef what2Put As String, ByRef whatBox As TextBox)
    whatBox.Text = what2Put '<-- has error Object reference not set to an instance of an object. for the whatBox.text
End Sub

kbOrPad 类

Dim theBoxName As TextBox = Nothing

Public Sub keyboardPadType(ByRef whatType As String, ByRef boxName As TextBox)
    theBoxName = boxName '<-- shows nothing here
    Dim intX As Short = 1

    If whatType = "PAD" Then
        Do Until intX = 30
            Dim theButton() As Control = Controls.Find("Button" & intX, True)

            theButton(0).Enabled = False
            intX += 1
        Loop
    ElseIf whatType = "KEYB" Then

    End If
End Sub

Private Sub ClickLetters(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim btn As Button = CType(sender, Button)

    If btn.Text = "Backspace" Then

    Else
        Call Form1.putIntoTextBox(btn.Text, theBoxName) 'theBoxName taken from keyboardPadType
    End If
End Sub

给你一些视觉效果:

在此处输入图像描述 在此处输入图像描述

粘贴代码:http://pastebin.com/4ReEnJB0

4

2 回答 2

1

首先,您应该将 ByRef 替换为 ByVal(当您不知道是否应该使用其中一个时,请使用 ByVal)。

其次,我认为您不需要 putIntoTextBox 方法,我认为您应该可以直接执行此操作(可能是阻止它的线程问题,但我认为这不太可能基于您的描述)。您没有显示Form1设置的位置(或者即使设置),这是另一个潜在的问题。

最后,回调另一个类的更好方法是使用委托/lambada。

(我知道,没有代码,但你没有为工作响应提供足够的上下文,所以我只是给出文本)。

于 2012-06-20T07:30:08.110 回答
1

确保它theBoxName是一个模块范围的变量,然后我会像这样填充它,为您提供实现共享TextBox MouseDown处理程序的灵活性:

Private Sub TextBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
    Dim tb As TextBox = CType(sender, TextBox)

    Call keyboardPadType("PAD", tb)
End Sub

尝试这样的事情

Public Class Form1
Dim myKborPad As New kbOrPad

Private Sub TextBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
    Dim tb As TextBox = CType(sender, TextBox)

    Call myKborPad.keyboardPadType("PAD", tb)
End Sub

根据您的 PasteBin 代码进行编辑。

我注意到你已经在你的模块中声明了一个keyboardPadType 的实例,使用它而不是我之前所说的。该代码应如下所示:

删除:将 myKborPad 调暗为新 kbOrPad

并使用您在模块中创建的 theKbOrPad,如下所示:

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
    Dim tb As TextBox = CType(sender, TextBox)

    Call keyboardOrPad.runKeyboardOrPad("SHOW")
    Call theKbOrPad.keyboardPadType("PAD", tb)
    'Call kbOrPad.keyboardPadType("PAD", tb)
End Sub

另外关于您遇到的当前错误,您正在尝试使用Form1 的默认实例,它不是您正在运行的实际 Form ,您可以通过将您尝试使用的方法设置为共享来围绕此进行编码。像这样:

Public Shared Sub putIntoTextBox(ByRef what2Put As String, ByRef whatBox As TextBox)
    whatBox.Text = what2Put
End Sub

但是,我实际上更愿意像这样将它放入您的模块中

Public Sub putIntoTextBox(ByRef what2Put As String, ByRef whatBox As TextBox)
    whatBox.Text = what2Put
End Sub

并这样称呼它

Call putIntoTextBox(btn.Text, theBoxName)

进行上述更改后,您的代码有效。

于 2012-06-20T03:14:59.267 回答