Saving textbox name when clicked in order to input text

Hey all i am in need of some help getting my code working correctly like i am needing it to. Below is my code that when the user click on the textbox, it pops up a keyboard where they can click on any letter and it will type that letter into the textbox. Problem being is i can not seem to get the name of the text box to return so that it knows where to send the letters to.

Order in firing is:

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 class

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

Some visuals for you:

Pastebin code: http://pastebin.com/4ReEnJB0

Answers


make sure that theBoxName is a Module scoped variable, then I would populate it like this giving you the flexibility of implementing a shared TextBox MouseDown Handler:

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

Try something like this

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

Edit Based on your PasteBin code.

I noticed you already have an instance of your keyboardPadType declared in your Module, use that instead of what I said earlier. That code should look like:

remove: Dim myKborPad As New kbOrPad

and use the theKbOrPad that you created in your module like this:

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

Also about the current error your are getting, you are trying to use the default instance of your Form1 , it isn't the actual Form that you are running, you can code around this by making the method you are trying to use as shared. Like this:

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

But however I would actually prefer to put it into your Module like this

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

and call it like this

Call putIntoTextBox(btn.Text, theBoxName)

after making above changes your code worked.


First, you should replace the ByRef with ByVal (anytime you don't know whether you should use one or the other, use ByVal).

Secondly, I believe you don't need the method, putIntoTextBox, I think you should be able to do that directly (might be threading problems that prevent it, but I don't think that's likely based on your description). You don't show where Form1 is set (or even if it is), and that's another potential problem.

Finally, the better way to call back into the other class is to use a delegate/lambada.

(I know, no code, but you don't provide enough context for a working response, so I'm just giving text).


Need Your Help

Creating a pandas DataFrame from columns of other DataFrames with similar indexes

python pandas dataframe

I have 2 DataFrames df1 and df2 with the same column names ['a','b','c'] and indexed by dates.