VB.net动态单选按钮单击添加事件
你好,我有这个代码的问题。。。我从我的数据库id和名称中获取,并将每一行添加到一个新的单选按钮中,但如何使用msgbox onclick获取id?代码如下:VB.net动态单选按钮单击添加事件,vb.net,events,dynamic,Vb.net,Events,Dynamic,你好,我有这个代码的问题。。。我从我的数据库id和名称中获取,并将每一行添加到一个新的单选按钮中,但如何使用msgbox onclick获取id?代码如下: 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
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
您为它们创建一个事件处理程序-它们都使用相同的事件处理程序。您只需从签名中强制转换
发送者
对象,即可知道选择了哪一个。我还将为每次迭代创建一个新的RadioButton
对象。FlowLayoutPanel
将使添加RBs变得更容易-您不必手动设置位置
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
您想处理对
单选按钮的点击,还是确定在不同的按钮点击中选择了哪个单选按钮
?一个选中的列表框可能值得一看(按空间和布局)@操作