在Visual Basic应用程序中从数据库中选择单选按钮

时间:2017-09-19 12:43:59

标签: vb.net

我的框架中有3个单选按钮。如果我在数据库中有一个数字,我想根据数字选择单选按钮。很抱歉,如果这是一个重复的问题,但我尝试搜索。

这似乎是随机的:

mABC = cboABC.SelectedValue
Select Case mABC
    Case 0
        optABC_ccc.Checked = True
    Case 1
        optABC_bbb.Checked = True
    Case 2
        optABC_aaa.Checked = True

2 个答案:

答案 0 :(得分:0)

这个简单的例子做你想要的。我不确定你哪里出错了。试一试,看看你的代码缺失了什么。

我有与你相同的控件,还有一个按钮来调用你的代码。

' class to be used as data source
Private Class Foo
    Public Property A As Integer
End Class

' set up data source, and set ValueMember (otherwise SelectedValue won't work)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim ds = New List(Of Foo)()
    ds.Add(New Foo() With {.A = 0})
    ds.Add(New Foo() With {.A = 1})
    ds.Add(New Foo() With {.A = 2})
    cboABC.DataSource = ds
    cboABC.ValueMember = "A"
End Sub

' used a button to initiate your code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' SelectedValue is the value of property A of the data source
    ' this code is identical to yours
    Dim mABC = cboABC.SelectedValue
    Select Case mABC
        Case 0
            optABC_ccc.Checked = True
        Case 1
            optABC_bbb.Checked = True
        Case 2
            optABC_aaa.Checked = True
    End Select
End Sub

enter image description here

答案 1 :(得分:0)

        mABC = ds.Tables("Alphabet").Rows(0)("ABC")
        'mABC = cboABC.SelectedValue
        Select Case mABC
            Case 0
                optABC_ccc.Checked = True
            Case 1
                optABC_bbb.Checked = True
            Case 2
                optABC_aaa.Checked = True

        End Select