VB.net dynamic radiobuttons add event onclick

前端 未结 1 1919
伪装坚强ぢ
伪装坚强ぢ 2021-01-28 15:29

Hello i have problem with this code... i take from my database id and name and i add every row in a new RadioButton but how i can take the id with msgbox onclick? the code is th

相关标签:
1条回答
  • 2021-01-28 15:35

    You make an event handler for them - they all use the same one. You just need to cast the sender object from the signature to tell which one was selected. I would also just make a new RadioButton object for each iteration. A FlowLayoutPanel would make adding the RBs easier - you won't have to set the location manually.

      Do While reader.Read()
            Dim RButton As New RadioButton
            RButton.Name = "RadioButton" + x.ToString
            RButton.Text = reader.GetString(1)
            RButton.Top = y
            y += x
            p += 1
            If (reader.GetInt32(2) = 1) Then
                RButton.BackColor = Color.LightGreen
            End If
            Addhandler RButton.Click, AddressOf RB_Clicked 
            'or use the CheckChanged event
            Panel1.Controls.Add(RButton)
        Loop
        '... rest of your code
    

    The event handler:

    Private Sub RB_Clicked(sender As Object, e As EventArgs)
      Dim rb As RadioButton = DirectCast(sender, RadioButton)
      'now rb is the one that was clicked
    End Sub
    
    0 讨论(0)
提交回复
热议问题