2
votes

Self taught(in progress) Visual Basic guy here.

I've searched for a clear answer on this, but so far have come up empty handed.

The problem...

  1. I have two comboboxes. The first combobox has 10 options, second combobox has 2 options

  2. I have 10 textboxes, with a name that includes one of the 10 options.

    ex 1st textbox name - "txb_Option1Type"

    2nd textbox name - "txb_Option2Type" and so on.

  3. I have 2 tabs, with the first 5 text boxes on the 1st tab and last 5 on the 2nd tab.

  4. I thought the following bit of code, upon a button click, would transfer the text of the chosen option in the 2nd combobox to the corresponding textbox...

` Public Sub TransferTruckToDoorText()

    Dim str_ErrorButton As String = cbx_DoorNumber.Text

    Dim str_ReplaceSpacesButton As String = str_ErrorButton.Replace(" ", "")

    Dim str_Button As String = str_ReplaceSpacesButton

' Null reference error on below line of code

    Me.Controls("txb_" & str_Button & "Type").Text = cbx_TruckType.Text


End Sub

`

As noted in the above code, I'm getting a null reference and for the life of me cannot figure out why. I've stepped through the code, and I'm not able to find a NULL or Nothing value that could be making this catch.

Any and all help would be appreciated.

edited for clarity

2
Use the debugger to see what the value of str_Button is. Make sure these controls are not in a panel, etc. - LarsTech
The value of the str_Button is the selected option from the first combobox, minus the spaces. Not sure what you mean by "not in a panel". - ManyLeafClover
Don't keep str_Button a secret. What did the debugger tell you the value was? You are only looking for these controls in "Me". If these controls are in a Panel or a GroupBox, etc, they won't be there. They would be in the Panel's Controls collection, etc. - LarsTech
Apologies. Didn't mean to be cryptic. When option 1 is chosen, the value is "Option1". I have the text boxes separated between two tabs(should of mentioned that in retrospect). Does that affect it? - ManyLeafClover
Yep. TabControls have TabPages. Try using Me.Controls.Find(controlname, True). - LarsTech

2 Answers

0
votes

The Me.Controls collection does not automatically search the child panels.

Try using the Controls.Find method for that, which includes a parameter to search the child control's control collection, too. It returns an array:

Dim c As Control() = Me.Controls.Find("txb_" & str_Button & "Type", True)
If c.Length = 1 Then
  c(0).Text = cbx_TruckType.Text
End If
0
votes
Me.Controls.Item("txb_" & str_Button & "Type")