0

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
10
  • Your question is confusing, but if I'm reading it correctly, maybe you're looking for ctr.Name? Commented Nov 20, 2014 at 14:39
  • that sounds right, Can you spurt out an example line of code? Commented Nov 20, 2014 at 14:39
  • 3
    Remind me to shoot your UX designer. Commented Nov 20, 2014 at 14:40
  • "Spurt" is an interesting word choice. I can't show you an example line of code because I don't know what you plan on doing with the name of the control once you have it. Just use ctr.Name like I suggested for whatever purpose you have. Commented Nov 20, 2014 at 14:42
  • What you don't want to do is run 84 separate queries to your database. You need to think in terms of running one query that returns data about all of your button in order, and then going over those results and applying them to each button in turn. But without knowing more about your database, we can't really even begin to say what that query might look like. Commented Nov 20, 2014 at 14:42

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

I need to run some code on a button click and some code needs to be done on the load event. Also, On that first public sub, I assume I will need to add with sub for every button?
That first public sub, what event is that?
That's a lot of questions for one post. It's better to ask one question at a time on this website. In the first sub, it's the button click event msdn.microsoft.com/en-us/library/…
I understand the button click event, but my question is, could I somehow to this on the load event instead of a button click?
You can definetly set the Tag property on the load event, that's a good idea. But you'll have to handle the Click event of your buttons if you want to show a pop up window on each click. I'll update my answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.