J Trahair
Well-known member
Hi. I have 45 buttons on a form, Button1, Button2, Button3 to Button45. Each one has to have a Button?.Text value from a database eg. Button1.Text = "Beef", Button2.Text = "Cabbage", etc. In old VB6 I could use the Index value of each button to reference it, but in VB.Net there's no Index capability.
Is there a more elegant way to populate these buttons other than having a Select Case statement from 1 to 45 as below?
Thank you.
Is there a more elegant way to populate these buttons other than having a Select Case statement from 1 to 45 as below?
VB.NET:
gnumButtonNumber = 1
Do Until gnumButtonNumber > 45
mstrSQL = "SELECT * FROM Buttons WHERE ButtonNumber = " & gnumButtonNumber
mstrThisTableName = Mid$(mstrSQL, InStr(mstrSQL, " FROM ") + 6) & " " 'space? see next line.
mstrThisTableName = Mid$(mstrThisTableName, 1, InStr(mstrThisTableName, " ") - 1)
objConn = New SQLiteConnection(mstrSQLiteConnectionString)
objDs = New DataSet()
objDa = New SQLiteDataAdapter(mstrSQL, mstrSQLiteConnectionString)
objDa.Fill(objDs, mstrThisTableName)
'Show data:
For Each objRow In objDs.Tables(0).Rows
Select Case gnumButtonNumber
Case 1
Button1.Text = objRow("ButtonCaption").ToString
Button1.BackColor = System.Drawing.Color.FromName(objRow("ButtonColour").ToString)
Case 2
Button2.Text = objRow("ButtonCaption").ToString
Button2.BackColor = System.Drawing.Color.FromName(objRow("ButtonColour").ToString)
Case 3
Button3.Text = objRow("ButtonCaption").ToString
Button3.BackColor = System.Drawing.Color.FromName(objRow("ButtonColour").ToString)
Case 4
Button4.Text = objRow("ButtonCaption").ToString
Button4.BackColor = System.Drawing.Color.FromName(objRow("ButtonColour").ToString)
Case 5
Button5.Text = objRow("ButtonCaption").ToString
Button5.BackColor = System.Drawing.Color.FromName(objRow("ButtonColour").ToString)
'And so on till Case 45!!
End Select
Next
gnumButtonNumber = gnumButtonNumber + 1
Loop