Reputation: 530
I am currently working in vb.net 2013 express backed with an sql database on the back end. I have 84 buttons that are being used in an inventory management style program. These buttons are numbered M01, M02,...M12, T01, T02, ....T24, H01, H02,....H24, and P01, P02,.....P24. I need to loop through all these buttons to change background color or make some sort of visual signal to show if something is in that cell. Each button is a cell on the front end display. I will also being using a pop up window as a dialog result with sql commands on each of these buttons, so instead of typing the code into every single button I would like to use a loop.
I would want the code to have the for statement in it and then just loop through all the buttons, I am guessing I will use the name of the button as a loop description. However,I don't know how to take the name of the button and use it to run an sql query to locate the cell in the sql database. For example, I want the loop to take a button, pull the name of the button, and use it too run an sql query to return if that cell is taken and if so what is in it? I just dont know how to grab the name and import it into the hard code.
Try
Using conn1 As New SqlConnection(connstring)
conn1.Open()
For Each ctr In PanelButtons
If TypeOf ctr Is Button Then
Using comm1 As New SqlCommand("SELECT Shear FROM Production.dbo.tblFabWipLog WHERE LocID = @Cell", conn1)
comm1.Parameters.AddWithValue("@Cell", ctr.Name)
End Using
End If
Next
Using comm1 As New SqlCommand("SELECT ")
End Using
End Using
Catch ex As Exception
MsgBox("Error loading button clear or taken, please contact manufacturing engineering.")
MsgBox(ex.ToString)
End Try
Upvotes: 0
Views: 541
Reputation: 12748
The button has a Tag property. Set the ID you your item in each button tags property.
Then you just need to have one event that handles all button
Public Sub Form_Load(byVal o As Object, ByVal e As EventArgs) Handles Me.Load
' Load the tag information (this is a hardcoded example)
M01.Tag = "M01"
M02.Tag = "M02"
M03.Tag = "M03"
' ...
End Sub
Public Sub Button_Click(byVal o As Object, ByVal e As EventArgs) Handles M01.Click, M02.Click, ...
ProcessButton(CType(o, Button).Tag)
End Sub
Public Sub ProcessButton(ByVal itemId As String)
' ... Do what you need here
MessageBox.Show(itemId)
End Sub
Upvotes: 1