1

I am trying to set the Text property of a dynamically created text box using a variable name, but when I use the Me.Controls(variable name).Text, I'm getting an error saying I need to set it up as "New". The name property of the text box, using a variable, was set when it was created but I don't seem to be able to retrieve using the same name.

    Private Sub Example(panposition As Integer)

    Dim tbfile = New TextBox()
    Dim lineExample As Integer = 2
    ' creating a text box with a variable name
    Controls.Add(tbfile)                    ' create the new textbox to hold the file name
    tbfile.Name = "tbfile" + panposition.ToString
    tbfile.Location = New Point(85, tvposition)
    tbfile.Size = New Size(155, 20)
    tbfile.Text = "file name"
    tbfile.TextAlign = HorizontalAlignment.Left
    tbfile.HideSelection = False
    tbfile.TabStop = False
    tbfile.AllowDrop = False
    tbfile.Visible = True

    ' trying to update the text in the text box using file name and text retrieved from an array
    Me.Controls.(arrTextVals(1, lineExample)).Text = arrTextVals(2, lineExample)


End Sub
8
  • Where are you getting the error? I cant see where the name of the New TextBOx is added to arrTextVals. A list or perhaps a Dictionary (depending on what the other info in it is), might work better Commented Sep 8, 2015 at 18:09
  • Try using DirectCast to get to your control Commented Sep 8, 2015 at 18:21
  • Try adding the control after you set the properties. Commented Sep 8, 2015 at 19:21
  • The values of arrTextVals are set outside the sample code that I put in the query. I have used MessageBox to determine that the value is correct for the text box that I'm trying to update. I have move the Controls.Add statement to after the parameters are set and I seem to be getting farther. Now I'm getting a BC30203 error saying an identifier is expected. I'm pretty new to this - can anyone help? Someone suggested that I use Direct Cast but I'm not familiar with that yet. Commented Sep 8, 2015 at 19:33
  • BTW, the array where I'm retrieving the text box name is a string array. Do I need to convert this to an object type before I use it in the statement to update the text? How would I do that? Commented Sep 8, 2015 at 19:49

1 Answer 1

1

I think the problem is in line:

Me.Controls.(arrTextVals(1, lineExample)).Text = arrTextVals(2, lineExample)

The correct way to address a control in this way is to make a reference like this

Me.Controls(i).Text = arrTextVals(2, lineExample)

where i is an integer or using the name of the desired control which in your case could be

Me.Controls(arrTextVals(1, lineExample)).Text = arrTextVals(2, lineExample)

Of course i suppose as you mentioned before that arrTextVals is a string array

Edit:

You have a dot after Me.Controls.( <- never put a . before a bracket.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.