vb.net 如何获取在 Groupbox 中选中的单选按钮然后将其插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36392601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Get which Radio Button is Checked in a Groupbox then insert it to a database
提问by Diether Silverious
I want to insert a record in a databasewith two groupbox. groupbox1(has Male and female radio buttons) and groupbox2( has New students and old students radio buttons)
我想在 a 中插入一条记录,其中database有两个groupbox。groupbox1(有男有女radio buttons)和groupbox2(有新同学和老同学radio buttons)
In this case there will be four possible input the needs to be check
在这种情况下,将有四种可能的输入需要检查
- Male and old students
- Male and new students
- Female and old students
- Female and New students
- 男老同学
- 男同学和新同学
- 女同学和老同学
- 女学生和新生
Normally I will check each radio button individually just like this
通常我会像这样单独检查每个单选按钮
If rbtMale.Checked And rbtOld.Checked Then
Dim OldStudent = rbtOld.Text.ToString
Dim Male = rbtMale.Text.ToString
Using cmd As New SqlClient.SqlCommand("dbo.uspInsert", cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter("@StudPic", SqlDbType.Image))
If (Not String.IsNullOrEmpty(Me.Name) AndAlso System.IO.File.Exists(a.FileName)) Then
cmd.Parameters("@StudPic").Value = System.IO.File.ReadAllBytes(a.FileName)
cmd.Parameters.Add("@SurName", SqlDbType.VarChar, 100).Value = txtStudLN.Text
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 100).Value = txtStudFN.Text
cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 100).Value = Male
cmd.Parameters.Add("@Status", SqlDbType.VarChar, 100).Value = OldStudent
cmd.ExecuteNonQuery()
MsgBox("Save Record New record Successfully")
End If
End Using
End If
But I think there is smart way to check which radio buttonbeing checked in a groupboxand insert it in a database. Below are some codes that gives me idea about this but the problem is that i do not know how to pass the value in a groupboxthen insert this in my db. Any help would be very much appreciated. Thanks
但我认为有一种聪明的方法可以radio button检查在 a 中签入的内容groupbox并将其插入到database. 下面是一些让我对此有所了解的代码,但问题是我不知道如何在 a 中传递值,groupbox然后将其插入到我的数据库中。任何帮助将不胜感激。谢谢
For Each Ctrl In GroupBox1.Controls
If Ctrl.checked Then MsgBox(Ctrl.name)
Next
回答by David Wilson
You're nearly there in the snippet at the bottom of your question. This function takes a groupbox as the parameter and returns the RadioButton that is checked
您几乎在问题底部的代码段中。该函数以一个groupbox为参数,返回选中的RadioButton
Private Function GetGroupBoxCheckedButton(grpb As GroupBox) as RadioButton
For Each ctrl As RadioButton In grpb.Controls
If ctrl.Checked Then Return ctrl
Next
End Function
You'll get a compiler warning about the function not returning a result on all code paths, but this shouldn't matter with RadioButtons - assuming that you have more than one radiobutton in the groupbox - in theory.
您将收到有关该函数未在所有代码路径上返回结果的编译器警告,但这与 RadioButtons 无关 - 假设您在 groupbox 中有多个单选按钮 - 理论上。
Alternatively you could use this instead. It wont generate a compiler warning, but is less readable:-
或者,您可以改用它。它不会生成编译器警告,但可读性较差:-
Private Function GetGroupBoxCheckedButton(grpb As GroupBox) As RadioButton
Dim rButton As RadioButton = grpb.Controls.OfType(Of RadioButton).Where(Function(r) r.Checked = True).FirstOrDefault()
Return rButton
End Function
To get the text value just use something like
要获取文本值,只需使用类似
Dim value As String = GetGroupBoxCheckedButton(GroupBox1).Text
I'm guessing that all you want to do here is to add the RadioButton text to your database and rather than write the above code multiple times, you just want to do it the once. All you'll need to is remove the Ifand End Ifstatements and ammend the first bit of your code to read ..
我猜您在这里要做的就是将 RadioButton 文本添加到您的数据库中,而不是多次编写上述代码,您只想做一次。您所需要做的就是删除IfandEnd If语句并修改代码的第一位以读取 ..
Dim Status As String= GetGroupBoxCheckedButton(GroupBox1).Text
Dim Gender As String= GetGroupBoxCheckedButton(GroupBox2).Text
Incidentally, you don't need .ToStringat the end of these lines as the .TextProperty returns a string and no conversion to a string is needed
顺便说一句,您不需要.ToString在这些行的末尾,因为.Text属性返回一个字符串并且不需要转换为字符串
And finally change variables further down your code to match.
最后在代码中进一步更改变量以匹配。
cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 100).Value = Gender
cmd.Parameters.Add("@Status", SqlDbType.VarChar, 100).Value = Status


