How to require a CheckedListBox to have at least one selected item

With CheckListBox in VB.NET in VS2005, how would you make it mandatory for at least one item to be selected?

Is it possible to select one of the elements during development to make it the default?

What would be the best way to handle the validation part of this scenario? When should the user specify one of the fields?

+3
source share
6 answers

Any idea - to prohibit the user from marking the last marked item or to verify that at least one item is checked before continuing - is quite simple to implement.

How to prevent a user from unmarking the last marked item

1. , (, Load):

Private Sub frm_Load(ByVal sender As Object, ByVal e As EventArgs)
    clb.SetItemChecked(0, True) ' whatever index you want as your default '
End Sub

2. ItemCheck:

Private Sub clb_ItemCheck(ByVal sender As Object, ByVal e As ItemCheckEventArgs)
    If clb.CheckedItems.Count = 1 Then ' only one item is still checked... '
        If e.CurrentValue = CheckState.Checked Then ' ...and this is it... '
            e.NewValue = CheckState.Checked ' ...so keep it checked '
        End If
    End If
End Sub

,

Private Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' you could also put the below in its own method '
    If clb.CheckedItems.Count < 1 Then
        MsgBox("You must check at least one item.")
        Return
    End If

    ' do whatever you need to do '
End Sub
+5

. 100%, , . , . . . .

Public Class MyCheckedListBox
    Inherits CheckedListBox

    Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
        REM Ensure at least one item is checked
        MyBase.OnEnter(e)
        If Me.CheckedIndices.Count = 0 AndAlso Me.Items.Count > 0 Then
            Me.SetItemChecked(0, True)
        End If
    End Sub

    Protected Overrides Sub OnItemCheck(ByVal e As ItemCheckEventArgs)
        REM Prevent unchecking last item
        If Me.CheckedIndices.Count <= 1 AndAlso e.NewValue = CheckState.Unchecked Then e.NewValue = CheckState.Checked
        MyBase.OnItemCheck(e)
    End Sub
End Class

, , :

Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
    MyBase.OnHandleCreated(e)
    If Me.CheckedIndices.Count = 0 AndAlso Me.Items.Count > 0 Then
        Me.SetItemChecked(0, True)
    End If
End Sub
+2

Trap ItemCheck , :

    private void Form1_Load(object sender, EventArgs e)
    {
        checkedListBox1.SetItemChecked(0, true);
        checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
    }

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (CountChecked() == 1 &&
            e.NewValue == CheckState.Unchecked &&
            e.CurrentValue == CheckState.Checked)
        {
            checkedListBox1.SetItemChecked(0, true);
        }
    }

    private int CountChecked()
    {
        int count = 0;
        for (int i = 0; i < checkedListBox1.Items.Count; i++)
        {
            if (checkedListBox1.GetItemChecked(i) == true)
                count++;
        }
        return count;
    }

: , .

    private delegate void SetItemCallback(int index);

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (checkedListBox1.CheckedIndices.Count == 1 &&
            e.NewValue == CheckState.Unchecked &&
            e.CurrentValue == CheckState.Checked)
        {
            int index = checkedListBox1.CheckedIndices[0];
            // Initiate the asynchronous call.
            SetItemCallback d = new SetItemCallback(this.SetItem);
            d.BeginInvoke(index, null, null);
        }
    }
    private void SetItem(int index)
    {
        if (this.checkedListBox1.InvokeRequired)
        {
            SetItemCallback d = new SetItemCallback(SetItem);
            this.Invoke(d, new object[] { index });
        }
        else
        {
            checkedListBox1.SetItemChecked(index, true);
        }
    }
+1

:

  • winform "/", , , ( , , , ). MessageBox, ,

  • , , , . ItemCheck, , . , .

:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (checkedListBox1.CheckedIndices.Count == 1 &&
        e.NewValue == CheckState.Unchecked &&
        e.CurrentValue == CheckState.Checked)
    {
        //Show error label in red
    }
}
0

I would use the ItemCheck event to set Button.Enabled = falseif at least one item is not checked, and Button.Enabled = truewhen at least one item is checked (provided that the OK button or something similar).

0
source

Source: https://habr.com/ru/post/1744148/


All Articles