i need to load all buttons(200) text in my vb.net windows form at run time from my database table column.

for single button its like

button1.text = database value

but i want

For i = 1 to 200

button(i).text = database value

Next i

i tried this but its showing invalid

Please help


Recommended Answers

All 8 Replies

I think this will give you an idea how to do it...

Private Sub buttonloop_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
		Dim dummy() As String = New String() {"Value 1", "Value 2", "Value 3"}

		For i As Integer = 1 To 3 'we start with button1
			CType(Me.Controls.Find("Button" & i, True)(0), Button).Text = dummy(i - 1)
	End Sub

Two hundred buttons on one form seems a tad excessive but if you must, I suggest you create the buttons at runtime. You can lay them out precisely and keep references to them in an array for easy access. Another advantage is that it is easy to define just one handler that can manage the click event for all buttons. Here is an example of creating buttons at runtime. Create a blank form and add one button named btnAdd. Each time you click on btnAdd, one new button is added to the form.

Imports System.Windows.Forms

Public Class Form1

    Private xoffset As Integer      'x coordinate of new button 
    Private yoffset As Integer      'y coordinate of new button 
    Private btnID As Integer        'button number              

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click

        'Create a new button and set size, location and text

        Dim button As New Button
        button.Size = New Size(75, 23)
        button.Location = New Point(xoffset, yoffset)
        button.Text = "Button " & btnID

        'add the button to the form controls (or you won't be able to see/click it)


        'tell it what code to run when the button is clicked

        AddHandler button.Click, AddressOf RTButton_Click

        'adjust the location and button number for the next button

        yoffset += 30
        btnID += 1

    End Sub

    Private Sub RTButton_Click(sender As System.Object, e As System.EventArgs)
    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        xoffset = 75
        yoffset = 50
        btnID = 1

    End Sub

End Class

To keep track of the buttons you can

Private buttons(20, 20) As Button             '2D array of button references

Assign each new button to an element in the array.

hey jim thanx 4 ur post..

I want all my buttons to be lay on my vb form at design time and while at run time i want to add text(caption) to my buttons from my database and i don't want my user to click button every time while running exe.

Please help

thanx vb.netfreak

I don't understand the necessity of placing the buttons at design time. You can create them in the form load event and the effect is the same - the buttons are there when the form is displayed. Here's an example of that using a 2D array of buttons. If you need to add text from a database then get the text with a SELECT and modify the button labels at that time.

Imports System.Windows.Forms

'  Name:                                                                            
'    RunTimeButtons                                                                 
'  Description:                                                                     
'    This program shows how to create an array of buttons at runtime. Each button   
'    has a label that identifies its row and column. All buttons use a common       
'    handler that displays the button text in a msgbox when clicked.                
'  Audit:                                                                           
'    2011-12-13  original code                                                      

Public Class Form1

    Private buttonSize As New Size(30, 20)      'default new button size            
    Private buttonBase As New Point(15, 15)     'position of button(0,0)            

    Private buttons(9, 9) As Button             'array of new button references     

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        'Create an array of buttons

        Dim x As Integer        'new button X coordinate
        Dim y As Integer        'new button Y coordinate
        Dim b As Button         'new button reference   

        For row As Integer = 0 To 9
            For col As Integer = 0 To 9

                'calculate location of new button

                x = buttonBase.X + col * (buttonSize.Width + 2)
                y = buttonBase.Y + row * (buttonSize.Height + 2)

                'create button and set properties

                b = New Button
                b.Size = buttonSize
                b.Location = New Point(x, y)
                b.Text = row & "-" & col

                'add button handller and add to form controls

                AddHandler b.Click, AddressOf Button_Click

                'save reference to new button

                buttons(row, col) = b


    End Sub

    Private Sub Button_Click(sender As System.Object, e As System.EventArgs)
    End Sub

End Class

done !! thanx geekbychoice u rock !!

thanx jim 4 ur concern i got what i want !!

hey guys i want to open a same form for all those buttons on single click events of each button.
please help?

In the common handler routine add




Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.