Beng
Beng

Reputation: 13

Specify which button to run on event vb.net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim rows, column, j, i

    rows = 4
    column = 13
    For j = 0 To rows - 1
        Dim r As New TableRow()
        For i = 0 To column - 1
            Dim c As New TableCell()
            If i = 0 And j = 0 Then
                c.Text = "TIME/COURT"
                r.Cells.Add(c)
            ElseIf i = 0 And j = 1 Then
                c.Controls.Add(New LiteralControl("Court A"))
                r.Cells.Add(c)
            ElseIf i = 0 And j = 2 Then
                c.Controls.Add(New LiteralControl("Court B"))
                r.Cells.Add(c)
            ElseIf i = 0 And j = 3 Then
                c.Controls.Add(New LiteralControl("Court C"))
                r.Cells.Add(c)
            ElseIf i >= 1 And j = 0 Then
                c.Controls.Add(New LiteralControl(x & "-" & x + 1))
                r.Cells.Add(c)
                x += 1
            Else
                btn = New Button
                btn.ID = "btnr" & j & "c" & i
                AddHandler btn.Click, AddressOf Change_Colour
                c.Controls.Add(btn)
                r.Cells.Add(c)
            End If
        Next
        booktable.Rows.Add(r)
    Next

End Sub

Protected Sub Change_Colour(sender As Object, e As EventArgs)
    btn.BackColor = Drawing.Color.LightGreen
End Sub

When I click on any of the button, only the last button turns light green. How can I set the button to run the event when I click it? Can I call the button by button.ID? How?

Upvotes: 1

Views: 22

Answers (1)

Dom Sinclair
Dom Sinclair

Reputation: 2528

If multiple buttons can call your Change_Colour routine them you will need to actually determine which one did call it. so you will need something like:

    Protected Sub Change_Colour(sender As Object, e As EventArgs)
      Dim btn as Button = Ctype(sender, Button)
        'here you will need to determine which button is the sender which you could do by     checking its name for example
         'eg Select case btn.name
             'case  add stuff here
             'end select
      btn.BackColor = Drawing.Color.LightGreen
    End Sub

Upvotes: 1

Related Questions