2015-08-20 2 views
0

Привет, у меня проблема с этим кодом ... я беру из моей базы данных id и имя, и я добавляю каждую строку в новый RadioButton, но как я могу взять id с msgbox onclick? код:VB.net динамические radobuttons добавить событие onclick

Imports MySql.Data.MySqlClient 

Public Class Order_info 
Dim conn As New MySqlConnection 
Dim sqlcommand As New MySqlCommand 
Dim regDate As Date = Date.Now() 
Dim strDate As String = regDate.ToString("dd MMMM yyyy") 
Dim dbdate As String = regDate.ToString("yyyy-M-dd") 
Dim RButton As RadioButton 

Private Sub Order_info_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Label1.Text = strDate 
    connect() 
End Sub 

Private Sub connect() 
    If Not conn Is Nothing Then conn.Close() 
    conn.ConnectionString = String.Format("server={0};user id={1}; 
              password={2};database={3}; pooling=false", 
              My.Resources.DatabaseIP, 
              My.Resources.DatabaseUsername, 
              My.Resources.DatabasePassword, 
              My.Resources.DatabaseName) 
    Try 
     conn.Open() 

     sqlcommand.Connection = conn 
     Dim stm As String = "SELECT o.id,s.name,o.status FROM orders o INNER JOIN supplier s ON s.id = o.id_supplier where datetim = '" + dbdate + "'" 
     Dim cmd As MySqlCommand = New MySqlCommand(stm, conn) 
     Dim reader As MySqlDataReader = cmd.ExecuteReader() 
     Dim x As Integer = 25 
     Dim y, p As Integer 
     Do While reader.Read() 
      RButton = 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 
      Panel1.Controls.Add(RButton) 
     Loop 

     reader.Close() 

    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 
    conn.Close() 
End Sub 
End Class 
+0

Вы хотите обработать щелчок на 'RadioButton' или определить, какой' RadioButton' выбран на другой кнопке мыши? – shf301

ответ

2

Вы делаете для них обработчик событий - все они используют один и тот же. Вам просто нужно передать объект sender из подписи, чтобы указать, какой из них был выбран. Я бы также просто создал новый объект RadioButton для каждой итерации. A FlowLayoutPanel упростит добавление RB - вам не нужно будет устанавливать местоположение вручную.

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 

Обработчик события:

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

CheckedListBox, вероятно, стоит посмотреть (пространство и макет) ... @OP – Plutonix

+0

работает отлично !! thx человек –

Смежные вопросы