VB.net动态单选按钮添加事件onclick [英] VB.net dynamic radiobuttons add event onclick
本文介绍了VB.net动态单选按钮添加事件onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
导入MySql.Data.MySqlClient
公共类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(发送方作为对象,作为EventArgs)处理MyBase.Load
Label1.Text = strDate
connect()
End Sub
私有子连接()
如果不是conn是Nothing然后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.D atabasePassword,
My.Resources.DatabaseName)
尝试
conn.Open()
sqlcommand.Connection = conn
Dim stm As String =SELECT o.id,s.name,o.status FROM orders o INNER JOIN供应商s ON s.id = o.id_supplier其中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
尽管reader.Read()
RButton = New RadioButton
RButton.Name =RadioButton+ x.ToString
RButton.Text = reader.GetString(1)
RButton.Top = y
y + = x
p + = 1
如果(reader.GetInt32(2)= 1)则
RButton.BackColor = Color.LightGreen
End If
Panel1.Controls.Add(RButton)
循环
reader.Close()
捕获ex作为异常
MsgBox(ex.Message)
结束尝试
conn。关闭()
End Sub
结束类
解决方案
你为他们做一个事件处理程序 - 他们都使用相同的。您只需要从签名中转发发件人
对象,以指定哪一个被选中。我也会为每次迭代创建一个新的 RadioButton
对象。一个 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
如果(reader.GetInt32(2)= 1)则
RButton.BackColor =颜色.LightGreen
End If
Addhandler RButton.Click,AddressOf RB_Clicked
'或使用CheckChanged事件
Panel1.Controls.Add(RButton)
循环
'...其余的代码
事件处理程序:
私有子RB_Clicked(发件人作为对象,e作为EventArgs)
Dim rb As RadioButton = DirectCast(sender,RadioButton)
'now rb is被点击的
End Sub
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 this:
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
解决方案
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
这篇关于VB.net动态单选按钮添加事件onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文