开发者

Winforms checkedlistbox check one item

开发者 https://www.devze.com 2023-01-29 05:26 出处:网络
I have a CheckedListBox control on my form, and I want the user to only be able to check one item at a time within this list (so effectively I\'d want something that would mimic a \"RadioListBox\").

I have a CheckedListBox control on my form, and I want the user to only be able to check one item at a time within this list (so effectively I'd want something that would mimic a "RadioListBox").

Is this possible to do with a CheckedListBox or开发者_JAVA百科 would I have to improvise doing it some other way?

The CheckedListBox is populated on the form load by loading items from a database, in case that matters.

Thanks

Edit

I think I should clarify, I am not looking to limit the amount a user can SELECT (ie the SelectionMode property), rather how many they can CHECK.


You could do it by adding an event check on CheckedListBox for ItemCheck and use function like this:

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

Then in the event you would have like:

  if (checkIfAllowed) { 
     ...
  } else {

  }

Additionally you could improve this by adding another function/method that will uncheck all items before allowing item to be checked. So when users click some checkbox all other checkboxes are unchecked.

To uncheck all checked items just use:

    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);
        }
    }

So in ItemCheck event you would have to run uncheckAll(yourListBox) first and then simply let the item be checked.

Edit: I've tested it with following code and it works. Without the if it throws exception.

    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);
            }
        }

    }


Try set .SelectionMode = SelectionMode.One property.


I had to do something similar to select a single user from a massive list. There isn't a RadioListBox to speak of, so I just coded it manually...

Sorry it's in VB, I just pasted it in, the logic is the same.

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


This code is could be much more simple, requires one variable. Every time you will check a new box, it will uncheck the last one, leading to the fact that there won't be two checked items or more checked at the time. Make sure the property SelectionMode = SelectionMode.One; is set as mentioned.

    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);
    }

Or alternativly you could set lastCheck to the default checked box if required, do this by:

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

and then the rest of the code that I mentioned above comes below.

Notice*: I've used lastCheck = **CheckListBox.SelectedIndex;** to support keyboard moving, just in case.

0

精彩评论

暂无评论...
验证码 换一张
取 消