2010-12-08 81 views
4

我有一個CheckedListBox控制在我的表單上,我希望用戶只能夠在這個列表中一次檢查一個項目(所以我想要的東西會模仿一個「RadioListBox 「)。Winforms checkedlistbox檢查一個項目

這可能與CheckedListBox有關嗎?還是我不得不以其他方式來做這件事?

CheckedListBox通過從數據庫加載項目填充表單加載,以防事件發生。

感謝

編輯

我想我應該澄清,我並不想限制用戶可以選擇的量(即SelectionMode屬性),而他們能有多少檢查。

+0

爲什麼使用一個選中的列表框。你可以使用一個簡單的列表框。 – 2010-12-08 11:55:52

+0

用戶明確要求能夠「檢查」不幸的。如果沒有證明是一種簡單的方法來限制一次可以檢查的項目的數量,那麼我會像你說的那樣使用正常的列表框。 – AndrewC 2010-12-08 11:58:10

+0

改爲使用組合框。 DropDownStyle =簡單如果你真的想要一個列表。 – 2010-12-08 13:01:28

回答

3

您可以通過添加對CheckedListBox事件支票ItemCheck和使用功能這樣做:

private static bool checkIfAllowed(CheckedListBox listBox) { 
     if (listBox.CheckedItems.Count > 0) { 
      return false; 
     } 
     return true; 
    } 

然後在事件中,你會有這樣的:

if (checkIfAllowed) { 
    ... 
    } else { 

    } 

另外你可以通過添加另一個函數/方法來改善這種情況,在允許檢查項目之前將取消選中所有項目。所以當用戶點擊一些複選框時,所有其他複選框都未選中。

,取消勾選所有項目只需使用:

private static void uncheckAll(CheckedListBox listBox) { 
     IEnumerator myEnumerator; 
     myEnumerator = listBox.CheckedIndices.GetEnumerator(); 
     int y; 
     while (myEnumerator.MoveNext() != false) { 
      y = (int)myEnumerator.Current; 
      listBox.SetItemChecked(y, false); 
     } 
    } 

所以在ItemCheck事件時,你必須運行uncheckAll(yourListBox)第一,然後讓這個項目進行檢查。

編輯: 我用下面的代碼測試過它,它工作。沒有如果它拋出異常。

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) { 
     if (e.NewValue == CheckState.Checked) { 
      IEnumerator myEnumerator; 
      myEnumerator = checkedListBox1.CheckedIndices.GetEnumerator(); 
      int y; 
      while (myEnumerator.MoveNext() != false) { 
       y = (int)myEnumerator.Current; 
       checkedListBox1.SetItemChecked(y, false); 
      } 
     } 

    } 
1

嘗試設置.SelectionMode = SelectionMode.One屬性。

+0

這隻允許我一次選擇一個項目(即突出顯示),它無法控制隨時可以檢查多少項目。 :) – AndrewC 2010-12-08 11:53:15

0

我不得不做一些類似的事情,從一個海量列表中選擇一個用戶。沒有一個RadioListBox可以說,所以我只是手動編碼...

對不起,它在VB中,我只是粘貼它,邏輯是相同的。

Private m_NoFire As Boolean = False 

Private Sub lstSource_ItemCheck(_ 
     ByVal sender As Object, _ 
     ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lstSource.ItemCheck 

    'When the checked state is set programatically, 
    'this event will still fire and cause the loop 
    'to run - infinatly. This line prevents that. 
    If m_NoFire Then Exit Sub 
    m_NoFire = True 

    'Ensure only one item is selected 
    For i As Integer = 0 To lstSource.Items.Count - 1 
     If Not lstSource.SelectedIndex = i Then 
      lstSource.SetItemChecked(i, False) 
     End If 
    Next 

    m_NoFire = False 

End Sub '-- lstSource_ItemCheck 
0

此代碼可能更簡單,需要一個變量。 每次你檢查一個新盒子時,它將取消最後一個盒子,導致當時不會有兩個檢查項目或更多檢查項目。 確保財產SelectionMode = SelectionMode.One;設置如上所述。

private int lastCheck = -1; 
    private void CheckListBox_IndexChanged(object sender, EventArgs e) { 
     int toUncheck = lastCheck; 
     if (toUncheck != -1) 
      CheckListBox.SetItemChecked(toUncheck, false); 
     lastCheck = CheckListBox.SelectedIndex; 
     CheckListBox.SetItemChecked(lastCheck, true); 
    } 

或者alternativly如果需要,你可以設置lastCheck爲默認選中複選框,通過這樣做:

private void FormFoo_Load(object sender, EventArgs e) { 
     CheckListBox.SelectedIndex = 0; 
     lastCheck = CheckListBox.SelectedIndex; 
    } 

,然後我上面提到的代碼的其餘部分來自下面。

注意*:我用lastCheck = **CheckListBox.SelectedIndex;**來支持鍵盤移動,以防萬一。

相關問題