1
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?

1 Answer 1

1

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

Comments

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.