Dynamically changing button label from a variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paulers

    Dynamically changing button label from a variable

    Hello,

    I have an arraylist that looks like this: 1,4,6

    I have buttons called button1, button4, button6.

    I would like to change the text on the buttons that corispond to the
    numbers in the array. Is there a dynamic way to do this? or do I have
    to loop through the arraylist and use a switch to update them?

    like is ther any way to do something like this

    Dim button as String
    For i = 0 To al.Count - 1
    button = "button" & al.Item(i)
    button.Text = "text changed"
    Next

    thanks!

  • Kerry Moorman

    #2
    RE: Dynamically changing button label from a variable

    Paulers,

    In VB2005 you could do something like this:

    For Each i As Integer In al
    Me.Controls("Bu tton" & i.ToString).Tex t = "Text changed"
    Next

    Kerry Moorman


    "Paulers" wrote:
    Hello,
    >
    I have an arraylist that looks like this: 1,4,6
    >
    I have buttons called button1, button4, button6.
    >
    I would like to change the text on the buttons that corispond to the
    numbers in the array. Is there a dynamic way to do this? or do I have
    to loop through the arraylist and use a switch to update them?
    >
    like is ther any way to do something like this
    >
    Dim button as String
    For i = 0 To al.Count - 1
    button = "button" & al.Item(i)
    button.Text = "text changed"
    Next
    >
    thanks!
    >
    >

    Comment

    • Tom Leylan

      #3
      Re: Dynamically changing button label from a variable

      If you control over how it is all set up (i.e. you've created the array and
      the buttons) perhaps it would be better to create an array of buttons
      references. It is quite limiting to enforce a rule whereby buttons must be
      named "button1", etc. in order for a routine to work. If instead you create
      an array of button references "any" button can be included and (in your
      example) the button text will change.

      Consider not hard coding the "text" into your loop as well. You can pass
      the text into the method along with the array and FOR EACH the text into
      every button in the array and then any number of buttons can be changed to
      any arbitrary chunk of text.

      Tom


      "Paulers" <SuperGh0d@gmai l.comwrote in message
      news:1162669573 .208754.124480@ b28g2000cwb.goo glegroups.com.. .
      Hello,
      >
      I have an arraylist that looks like this: 1,4,6
      >
      I have buttons called button1, button4, button6.
      >
      I would like to change the text on the buttons that corispond to the
      numbers in the array. Is there a dynamic way to do this? or do I have
      to loop through the arraylist and use a switch to update them?
      >
      like is ther any way to do something like this
      >
      Dim button as String
      For i = 0 To al.Count - 1
      button = "button" & al.Item(i)
      button.Text = "text changed"
      Next
      >
      thanks!
      >

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Dynamically changing button label from a variable

        Paulers,

        In addition to the others, the Tag property can be very handy in this to
        seperate some buttons from others.

        Otherwise of course the array of existing buttons

        dim myButtonArray as button() = {button1, button4, button6, buttonwhatever}

        Cor

        "Paulers" <SuperGh0d@gmai l.comschreef in bericht
        news:1162669573 .208754.124480@ b28g2000cwb.goo glegroups.com.. .
        Hello,
        >
        I have an arraylist that looks like this: 1,4,6
        >
        I have buttons called button1, button4, button6.
        >
        I would like to change the text on the buttons that corispond to the
        numbers in the array. Is there a dynamic way to do this? or do I have
        to loop through the arraylist and use a switch to update them?
        >
        like is ther any way to do something like this
        >
        Dim button as String
        For i = 0 To al.Count - 1
        button = "button" & al.Item(i)
        button.Text = "text changed"
        Next
        >
        thanks!
        >

        Comment

        Working...