单击时保存文本框名称以输入文本

时间:2012-06-20 03:02:50

标签: vb.net textbox controls

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

射击命令是:

TextBox1_MouseDown

keyboardOrPad.runKeyboardOrPad

kbOrPad.keyboardPadType

ClickLetters

Form1.putIntoTextBox

Form1中

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

一些视觉效果:

enter image description here enter image description here

Pastebin代码:http://pastebin.com/4ReEnJB0

2 个答案:

答案 0 :(得分:1)

确保theBoxNameModule scoped variable,然后我会像这样填充它,让您灵活地实施共享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实例,而不是我之前说过的那个。该代码应如下所示:

删除:     Dim myKborPad As New kbOrPad

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

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

关于你当前的错误,你正在尝试使用default instance of your Form1,它不是你正在运行的实际表单,你可以通过制作你试图使用的方法来解决这个问题。共享。像这样:

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)

在进行上述更改后,您的代码才能正常运行。

答案 1 :(得分:1)

首先,你应该用ByVal替换ByRef(任何时候你不知道是否应该使用其中一个,使用ByVal)。

其次,我相信你不需要方法,putIntoTextBox,我认为你应该能够直接做到这一点(可能是线程问题,阻止它,但我不认为这可能是基于你的描述)。您没有显示设置Form1的位置(或者即使它是),这是另一个潜在的问题。

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

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