locked
Using Buttons as an array - VB Code RRS feed

  • Question

  • User-1188570427 posted

    I know you can make an array of button. But the buttons are already put on the web form in specific spots. I have the buttons named like- button1, button2, button3, button4, button5..... Is there a way to use that in an array even though the buttons are designed from the start and not tied to an array from the start? I basically do not want to make a routine for each button and would like to use just 1 routine and add to the counter to update my access database. Thanks.


    maybe something like this.

    Dim count as integer

    buttoncount.text = "x" and so on.....



    Sunday, January 10, 2010 9:23 PM

Answers

  • User-851968219 posted

    Can you be more specific as to what you want to do?

    You can add all the buttons id in an array and use form.findcontrol(buttonid) to get the button and get the button text.

    private buttonIds() as string={"button1","button2"}

    for i as integer = 0 to buttonids.length -1 step 1

    dim buttonId as string=buttonids(i)

    dim MyButton as button=trycast(form.findcontrol(buttonid))

    if(mybutton isnot nothing)

    mybutton.text = "something"

    end if

     

    It kind of does not make sense, since you are saying buttons are created designed time. Rather than putting the ids in array, you should rather just use ids from design time. The code above would work but won't be efficient since findcontrol basically searches for all controls in your page.

    next

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 10, 2010 10:31 PM

All replies

  • User-851968219 posted

    Can you be more specific as to what you want to do?

    You can add all the buttons id in an array and use form.findcontrol(buttonid) to get the button and get the button text.

    private buttonIds() as string={"button1","button2"}

    for i as integer = 0 to buttonids.length -1 step 1

    dim buttonId as string=buttonids(i)

    dim MyButton as button=trycast(form.findcontrol(buttonid))

    if(mybutton isnot nothing)

    mybutton.text = "something"

    end if

     

    It kind of does not make sense, since you are saying buttons are created designed time. Rather than putting the ids in array, you should rather just use ids from design time. The code above would work but won't be efficient since findcontrol basically searches for all controls in your page.

    next

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 10, 2010 10:31 PM
  • User-1188570427 posted

    its kinda hard to explain in words. the buttons are already designed on the form. If i could of, I would of made them of an array and basically calculated where each one should be each time the program loads. My friend went ahead and made the form and did not make them as an array of buttons etc. I think what you suggested might work. Let me try it and Ill let you know. Thanks.

    Sunday, January 10, 2010 11:04 PM