4 Replies - 134 Views - Last Post: 10 October 2018 - 04:14 PM Rate Topic: -----

#1 skip2111   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-October 18

Modify a controls properties using a string as the control name

Posted 10 October 2018 - 08:48 AM

Ive spent the last few hours looking for a simple way to access a controls properties when I create the name inside a loop. For instance

for i = 0 1 to 10
   button & "i".text="blah blah will get this info somewhere else"
next




obviously this does not work, but is there a simple way to make this work ? What I'm trying to do is more complicated but I think this will let me solve most of the other problems. Simplicity is the key! I'm pretty sure I've done this before years ago but cannot for the life of me remember how!

Thanks in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: Modify a controls properties using a string as the control name

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: Modify a controls properties using a string as the control name

Posted 10 October 2018 - 08:52 AM

Per my previous post. You need to get a handle to the control. You can use the 'me.controls' to find a reference to the control by the control's name (a string). You would need to cast the found control as what ever type it is so you have access to intellisense about it.

        Dim baseName As String = "button"
        For index = 1 To 5
            Console.WriteLine(baseName + index.ToString) ' basic string appending.
        Next


button1
button2
button3
button4
button5

Was This Post Helpful? 0
  • +
  • -

#3 skip2111   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 08-October 18

Re: Modify a controls properties using a string as the control name

Posted 10 October 2018 - 09:00 AM

I understand how to generate the name of the control, that's no problem - although I appreciate your example of basic string appending. Where I am falling down is casting the control. I don't really know anything about this. I've got this so far:

      Dim baseName As String = "Textbox"
        Dim fullname As String

        For index = 1 To 5

            fullname = baseName + index.ToString
            CType(fullname, TextBox)
        Next


How come this isn't right?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: Modify a controls properties using a string as the control name

Posted 10 October 2018 - 09:03 AM

The docs are a great help. Search terms "msdn vb.net cast"

https://docs.microso...to-another-type

https://docs.microso...ctcast-operator
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1515
  • View blog
  • Posts: 3,832
  • Joined: 01-February 13

Re: Modify a controls properties using a string as the control name

Posted 10 October 2018 - 04:14 PM

Quote

How come this isn't right?

That would be because you can not directly cast a String data type to a TextBox control type. That is what you are trying to do in the line of code below...
CType(fullname, TextBox)


As modi123_1 has mentioned to you, you can use the Controls collection property of the Form to get a reference to all the controls on the Form. In addition to that, you can supply an Index or a Name of the control you want a reference to. It will return the control as a Control class so, you would need to cast that Control type to a TextBox control type.

For example, this line would get you a reference to TextBox1 (as a Control type), and cast the Control to a TextBox...
Dim tb as TextBox = CType(Me.Controls("TextBox1"), TextBox)

From there you can access all the properties and methods of TextBox1...
Dim tb as TextBox = CType(Me.Controls("TextBox1"), TextBox) 'get "TextBox1" from the Form's Controls and cast it to a TextBox type control
tb.Text = "This is TextBox1."


If you study that small example, you should be able to see how you can use your 'fullname' String variable to get a reference to the controls you want.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1