2010-10-26 79 views
0

是否有可能有一个单选按钮列表,就像我们有一个选中的列表框? 其实我想从数据库加载到列表的所有选项,但不希望用户允许检查多个项目。检查列表框样式单选按钮列表

另外如何阅读它(说清单中的项目4)我想将它的值存储在变量中。

感谢和问候。 Furqan

+0

它是一个ASP.Net Web应用程序吗? – 2010-10-26 08:58:57

+0

不,它在vb.net – 2010-10-26 09:03:22

+0

VB.Net只是语言而不是技术(如ASP.Net或Windows Forms)。好的,然后看我更新的答案。 – 2010-10-26 09:10:48

回答

1

如果你指的是ASP.Net RadioButtonList的,控制尝试这个例子:

ASPX(你可以在设计配置数据源(显示智能标签):

<asp:RadioButtonList ID="RadioButtonList1" runat="server" DataSourceID="SqlDataSource1" 
    DataTextField="ClaimStatusName" DataValueField="idClaimStatus"> 
</asp:RadioButtonList> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RM2ConnectionString %>" 
      SelectCommand="SELECT [idClaimStatus], [ClaimStatusName] FROM [dimClaimStatus]"> 
</asp:SqlDataSource> 

一个单选按钮列表允许用户在默认情况下只能选择一个项目 所选择的项目被存储在RadioButtonList1.SelectedItem

编辑:正如你已经clairified既然这是一个Winform问题,你需要一个GroupBox来允许用户选择一个。

从数据源动态创建单选按钮并将它们添加到组框,看看我的samplecode:

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     Dim allStatus As DataSet2.StatusDataTable = New DataSet2TableAdapters.StatusTableAdapter().GetData() 
     For i As Int32 = 0 To allStatus.Rows.Count - 1 
      Dim status As DataSet2.StatusRow = allStatus(i) 
      Dim rb As New RadioButton() 
      rb.Text = status.ClaimStatusName 
      rb.Tag = status.idClaimStatus 
      rb.Location = New Point(Me.GroupBox1.Location.X + 5, Me.GroupBox1.Location.Y + i * rb.Height) 
      AddHandler rb.CheckedChanged, AddressOf RBCheckedChanged 
      Me.GroupBox1.Controls.Add(rb) 
     Next 
     Me.GroupBox1.Visible = allStatus.Rows.Count > 0 
     If allStatus.Rows.Count > 0 Then 
      Dim width, height As Int32 
      Dim lastRB As Control = Me.GroupBox1.Controls(GroupBox1.Controls.Count - 1) 
      width = lastRB.Width + 20 
      height = lastRB.Height 
      Me.GroupBox1.Size = New Size(width, allStatus.Rows.Count * height + 20) 
     End If 
    End Sub 

    Private Sub RBCheckedChanged(ByVal sender As Object, ByVal e As EventArgs) 
     Dim source As RadioButton = DirectCast(sender, RadioButton) 
     Dim checkedRB As RadioButton = getCheckedRadioButton(Me.GroupBox1) 
     'source and checkedRB are the same objetcs because we are in CheckedChanged-Event' 
     'but getCheckedRadioButton-function works from everywhere' 
    End Sub 

    Private Function getCheckedRadioButton(ByVal group As GroupBox) As RadioButton 
     For Each ctrl As Control In group.Controls 
      If TypeOf ctrl Is RadioButton Then 
       If DirectCast(ctrl, RadioButton).Checked Then Return DirectCast(ctrl, RadioButton) 
      End If 
     Next 
     Return Nothing 
    End Function 

请记住,你必须用你代替我的数据对象。

+0

对不起,我不需要它在asp中。我需要winforms – 2010-10-26 09:55:16

+0

查看我更新的答案。 – 2010-10-26 10:06:27

相关问题