How to fill buttons texts with for each statement

Hello, I would be grately thanfull if someone could help me out with this problem. I have a set of 75 buttons named button1...button75, and I want to fill the text of the buttons with data I have inputed in a dataset. I am using a for each statement to get the information from the data rows, but I cant put the button name as a variable so that it changes to the next button to put the next text. Right now all the information is put only in one button, so it ends up with the last data from the dataset table. Please help

Thank you, Alfredo



Answer this question

How to fill buttons texts with for each statement

  • udaykiran potti

    Thanks a lot to all, its working perfectly.

    Alfredo


  • Utkarsh Shigihalli

     Alfredomad wrote:

    Thanks, this is exactly what I need, I only have one problem, when I run it, it gives me a NullReferenceException error and asks me to define the instance as new.  The buttons are supposed to be already defined, any suggestions

    Thanks a lot, Alfredo

    Hi Alfredomad,

    The NullReferenceException should be resolved by declaring intCtr as New Int32. Then on a seperate line you would assign intCtr a value. You may also run into an error about the form referencing itself. You would then change the code from Form1.Controls("button" & intCtr.ToString()).Text = "Put Your Text Here". The following will show you the new coding; if you are placing this code into the same form that is renaming the button text property (if you are renaming the button text property from another form then just change the "me" to the Form's Name; I.E. Form1):

    Dim intCtr As New Int32

    intCtr = 1

    Me.Controls("button" & intCtr.ToString()).Text = "Put Your Text here"

    I hope this helps.

    Thank you,

    James



  • Davids Learning

    Thanks, this is exactly what I need, I only have one problem, when I run it, it gives me a NullReferenceException error and asks me to define the instance as new. The buttons are supposed to be already defined, any suggestions

    Thanks a lot, Alfredo


  • Cathie 64

    you can get the button reference as fallows

    Dim intCtr as int32 = 1

    Form1.controls("button" & intCtr.ToString()).Text = "Put Your Text here"



  • PabloWablo

    you would have to change "Form1" to "Me" in his code....however the following should help

    For each c as control in me.controls

    If TypeOf c is Button then

    DIm TheButton as Button = DirectCast(C,Button)

    TheButton.Text ="Whatever"

    end if

    Next



  • How to fill buttons texts with for each statement