dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] Is it possible to reference a text control using a variable?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    135

    Resolved [RESOLVED] Is it possible to reference a text control using a variable?

    Hi all,

    I have many textboxes that I need to reference dymanically. The controls themselves aren't dynamic, just the way I need to reference them.

    I've got several text boxes say:
    txtMe1
    txtMe2
    txtMe3
    ...
    txtMe100

    I will need to randomly change one of the properties of the edit boxes. So lets say it picks a random number between 1-100 which is 56. So, I would want to change the background of my 56th edit box to:
    txtMe56.Backcolour = Color.Gray

    The only way I could think of doing this would be to have one giant select case statement to evaluate the random number. But, that is really dirty and not the best peformance wise (this could potentially get called hundreds of times) so was wondering what my options are?

    I tried to concatenate the random number to my generic control name 'txtMe' but it throws a wobbler.

    Can anyone tell me what my options are? I essentially would want the following to worK:

    Code:
    dim iNum as integer = 56
    txtMe & iNum & .Backcolour = Color.Gray
    many thanks!

    J

  2. #2
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: Is it possible to reference a text control using a variable?

    Here is the simplest form (there are other ways):

    Code:
            Dim TextBox As TextBox
            Dim I As Integer = 2
            Dim name As String = "TextBox" & I.ToString
            TextBox = Me.Controls.Item(name)
            TextBox.Text = "Something special"
    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    135

    Re: Is it possible to reference a text control using a variable?

    Hi mate,

    That worked a treat! Thanks for your help with that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width