【问题标题】:How to require CheckedListBox to have at least one item selected如何要求 CheckedListBox 至少选择一项
【发布时间】:2010-05-05 12:35:44
【问题描述】:

在 VS2005 中使用 VB.NET 中的 CheckListBox,如何强制要求至少选择一项?

您可以在设计时选择其中一项以使其成为默认项吗?

处理这种情况的验证部分的最佳方法是什么?何时应要求用户勾选其中一个框?

【问题讨论】:

  • 戴上我的“用户”帽子,如果应用在需要之前不强制执行此要求,我会非常高兴。如果我想将项目 Q 更改为唯一选中的项目 J 更改为唯一选中的项目 J,我希望能够使用 uncheck-Q-then-check-J 以及 check-J 来做到这一点-然后-取消选中-Q。实施您编辑的要求会阻止我这样做 - 是否有令人信服的理由阻止我以第一种方式这样做?
  • 你是对的,那会更好,但你什么时候检查它们是否被勾选了?
  • 您可以在 2 个级别上进行验证 - 首先是在未选中最后一项时发出警告/提醒您需要进行选择,其次是在未选择任何项目时在提交时出现错误。

标签: vb.net winforms visual-studio-2005


【解决方案1】:

无论是防止用户取消选中最后一个选中的项目,还是在继续之前验证至少一个项目已被选中,这两种想法都相当容易实现。

如何防止用户取消选中最后一个选中项

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

【讨论】:

    【解决方案2】:

    可以,但用户可以随时取消选中它。

    我这样做的方式是在提交时,遍历复选框项目并确保至少选中其中一个(在满足您的要求后中断循环,无需处理列表的其余部分。 )

    如果检查失败,则使自定义验证器可见。必填字段验证器不适用于检查列表框,但您可以在此处查看它们的实现方式:

    http://www.codeproject.com/KB/webforms/Validator_CheckBoxList.aspx

    【讨论】:

      【解决方案3】:

      这个解决方案应该很接近。不是 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
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          验证场景建议:

          • 如果您的 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
              }
          }
          

          【讨论】:

            【解决方案6】:

            我会使用ItemCheck 事件来设置Button.Enabled = false,当没有检查至少一项时,Button.Enabled = true,当至少检查一项时(假设一个确定按钮或类似的东西)。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-11-01
              • 1970-01-01
              • 2020-12-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-05-11
              • 1970-01-01
              相关资源
              最近更新 更多