2014-09-22 19 views
0

我在winform中有check box list從複選框列表中獲取文本

如果選中該複選框名單,然後我想要的值傳遞到一個字符串:

For i As Integer = 0 To cbxlstPancakes.Items.Count - 1 
    If cbxlstPancakes.GetItemChecked(i) Then 

     Dim currentPancake As String = cbxlstPancakes.SelectedItem.ToString 

    Else 
     'do something if they are not checked. 
    End If 
Next 
+0

'cbxlstPancakes.Items(i).ToString' – LarsTech 2014-09-22 19:03:25

+0

我試過,因爲它返回:System.Data.DataRowView – user3191666 2014-09-22 19:32:22

+0

你沒有證明列表中還有其他東西,但字符串。您必須將其轉換爲您正在使用的對象。 – LarsTech 2014-09-22 19:34:03

回答

2

現在我很困惑,如果您使用的字符串與綁定的數據源。對於數據源,請嘗試其中之一。

如果你只關心檢查的項目,這是一個更容易一些:

'=== IF you only care about the checked items (assuming you used a databound control) 
    For Each dr As DataRowView In cbxlstPancakes.CheckedItems 
     Dim currentPancake As String = dr.Item(0) '--> TODO: correct column from your datasource 
     MessageBox.Show(currentPancake) 
    Next 

如果你關心這兩個選中和未選中的項目,你應該能夠訪問它們這樣(應爲工作,要麼約束或未綁定):

'=== IF you care about both checked and unchecked items 
    For i As Integer = 0 To cbxlstPancakes.Items.Count - 1 
     If cbxlstPancakes.GetItemChecked(i) Then 
      MessageBox.Show(cbxlstPancakes.GetItemText(cbxlstPancakes.Items(i))) 
     Else 
      'Do something if they're not checked 
     End If 
    Next 

我不知道爲什麼CheckedListBox是比其他一些實施的控制稍有不同(與.Getxxxxx()方法等)。但是這似乎對我有用!

+0

您知道用戶會改變他們的想法,並且會影響我的工作模式。 感謝這就是我想要的。爲了將來的參考,如果我想分配一個ID到複選框。我將如何獲得他們的價值? – user3191666 2014-09-23 11:09:50

+0

這不適用於字符串值。但是這一次的工作如下>感謝 '對於每個博士爲字符串中CheckedListBox1.CheckedItems 昏暗currentPancake的String =博士「 - > TODO:從您的數據源 MessageBox.Show正確的列(currentPancake.ToString)' – JT4U 2016-05-23 16:44:13

相關問題