2010-10-26 74 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

相關問題