0

I need some assistance in generate buttons/tiles based on a database vales for example if have 5 people in the database it will create 5 buttons on the form, so on load it will connect to the data base and name the 5 buttons based on the first name of the user.

i know i am missing something but not too sure what here is what i have so far any help would be great

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports System.Data.SqlClient.SqlDataReader


Public Class Main

    Dim cn As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Nate\documents\visual studio 2012\Projects\WindowsApplication9\WindowsApplication9\Database1.mdf;Integrated Security=True")
    Dim cmd As New SqlCommand
    Dim dr As SqlDataReader
    Private dataAdapter As New SqlDataAdapter()

    Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Database1DataSet11.StudentTB' table. You can move, or remove it, as needed.
        Me.StudentTBTableAdapter.Fill(Me.Database1DataSet11.StudentTB)

        cmd.Connection = cn
        Dim Ps As Integer
        Dim nm As String
        Ps = StudentTBBindingSource.Count
        nm = "select * from StudentTB"

        Dim I As Integer

        For I = 1 To Ps

            Dim srt As New Button
            srt.Width = 50
            srt.Height = 50
            srt.Text = nm
            srt.Visible = True
            srt.Location = New Point(I * 30, 0)
            Panel1.Controls.Add(srt)

        Next

    End Sub

i added this coded it seem to try to work but then i came up saying their is no value in position 1

srt.Text = Database1DataSet11.Tables(0).Rows(I).Item("FirstName")

1 Answer 1

1

Array index start at zero and ends at Count - 1

For I = 0 To Ps - 1

should fix your problem on the row access, but then you need

srt.Location = New Point((I+1) * 30, 0)
3
  • how do i get the buttons to go on to the next line dynamically ?
    – OneNathan
    May 30, 2013 at 11:42
  • At which point? After the first 3, 2 or each on a separate line?
    – Steve
    May 30, 2013 at 11:43
  • after it fills the window horizontally it just seems to draw the button on the same line, what i want it to do is when i get to the edge of the window size go to the next line. hope that makes sense
    – OneNathan
    May 30, 2013 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.