【问题标题】:How to get rid of checkedlistbox selection highlighting effect?如何摆脱checkedlistbox选择高亮效果?
【发布时间】:2008-12-02 16:42:01
【问题描述】:

当在选中列表框中单击一个项目时,它会突出显示。如何防止这种突出显示效果?

我可以挂钩 SelectedIndexChanged 事件并清除选择,但突出显示仍然发生并且您会看到一个光点。事实上,如果您按住鼠标单击,在单击复选框区域后从不释放它,则选择会保持突出显示,直到您释放鼠标按钮。我基本上想完全摆脱这种突出效果。

【问题讨论】:

    标签: c# winforms checkedlistbox


    【解决方案1】:

    使用以下内容:

    private void checkedListBox1__SelectedIndexChanged(object sender, EventArgs e)
            {
                checkedListBox1.ClearSelected();
            }
    

    【讨论】:

    • 坦克这让我发疯。就像选中的列表框有多个选择状态.. 为什么要将此作为默认值?!
    • 赢家,赢家。鸡肉晚餐。
    • 只是为了澄清,@horgh 说这会阻止它检查或取消选中项目是不正确的。单击的动作设置选择并设置复选框状态。清除选择不会清除复选框状态。
    • 为了记录,这个方法仍然会导致文本短暂地切换到突出显示的颜色。但对于今天来说已经足够了。
    • 我对高亮颜色的短暂闪烁不满意。我在创建表单时选择了 ClearSelected(),在 CheckBoxList 失去焦点时选择了 ClearSelected()。对我来说,这是最好的妥协。
    【解决方案2】:

    除了你仍然得到虚线位之外,这将做到这一点。

    this.checkedListBox1.SelectionMode = System.Windows.Forms.SelectionMode.None;
    

    虽然现在你不能点击复选框...所以你必须这样做:

      private void checkedListBox1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
    
    
              if (checkedListBox1.GetItemRectangle(i).Contains(checkedListBox1.PointToClient(MousePosition)))
              {
                  switch (checkedListBox1.GetItemCheckState(i))
                  {
                      case CheckState.Checked:
                          checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
                          break;
                      case CheckState.Indeterminate:
                      case CheckState.Unchecked:
                          checkedListBox1.SetItemCheckState(i, CheckState.Checked);
                           break;
                  } 
    
              }
    
            }
        }
    

    如果这一切都不是你所追求的......你总是可以自己做一个。它是一个相当简单的控件。

    【讨论】:

    • 似乎在 MouseDown 处理程序中使用 checkedListBox1.IndexFromPoint(e.x, e.y) 会更有效,而不是遍历 GetItemRectangle 结果。
    【解决方案3】:

    将 SelectionMode 设置为 None 有一些奇怪的问题,例如必须实现 Click 事件。您可以将 SelectionMode 设置为 single,然后创建一个仅使用 OnDrawItem 覆盖 CheckedListBox 的类。请注意,为了关闭选定的外观,您必须关闭 Selected 状态并将颜色设置为您想要的颜色。您可以像我在这里所做的那样从控件中获取原始颜色。这是我想出的,应该可以让你开始让它看起来像你想要的那样。

    public partial class EnhancedCheckedListBox : CheckedListBox
    {
        /// <summary>Overrides the OnDrawItem for the CheckedListBox so that we can customize how the items are drawn.</summary>
        /// <param name="e">The System.Windows.Forms.DrawItemEventArgs object with the details</param>
        /// <remarks>A CheckedListBox can only have one item selected at a time and it's always the item in focus.
        /// So, don't draw an item as selected since the selection colors are hideous.  
        /// Just use the focus rect to indicate the selected item.</remarks>
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Color foreColor = this.ForeColor;
            Color backColor = this.BackColor;
    
            DrawItemState s2 = e.State;
    
            //If the item is in focus, then it should always have the focus rect.
            //Sometimes it comes in with Focus and NoFocusRect.
            //This is annoying and the user can't tell where their focus is, so give it the rect.
            if ((s2 & DrawItemState.Focus) == DrawItemState.Focus)
            {
                s2 &= ~DrawItemState.NoFocusRect;
            }
    
            //Turn off the selected state.  Note that the color has to be overridden as well, but I just did that for all drawing since I want them to match.
            if ((s2 & DrawItemState.Selected) == DrawItemState.Selected)
            {
                s2 &= ~DrawItemState.Selected;
    
            }
    
            //Console.WriteLine("Draw " + e.Bounds + e.State + " --> " + s2);
    
            //Compile the new drawing args and let the base draw the item.
            DrawItemEventArgs e2 = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, s2, foreColor, backColor);
            base.OnDrawItem(e2);
        }
    }
    

    【讨论】:

    • 我喜欢这个解决方案,但如果没有一点小调整,它就会导致我的Form1.cs [Design] 视图崩溃。在整个方法块周围添加一个简单的 try/catch,下面的代码为我解决了这个问题,它现在运行良好:catch (Exception) { base.OnDrawItem(e); }
    【解决方案4】:

    哦酷添加 将来自Hath的答案中的所有代码放在一个

     checkedListBox1_MouseMove(object sender, MouseEventArgs e)
    

    如果鼠标按钮进入开关,则添加

    case CheckState.Checked:
       if (e.Button == MouseButtons.Right)
       {
           checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
       }                     
       break;
    

    case CheckState.Unchecked:
       if (e.Button == MouseButtons.Left)
       {
           checkedListBox1.SetItemCheckState(i, CheckState.Checked);
       }
       break;
    

    它会在您按下左键移动鼠标时检查突出显示的项目 并用右侧取消突出显示它们

    【讨论】:

      【解决方案5】:

      我在这里给出答案有点晚了。无论如何,这将取消选中所有复选框并删除突出显示效果:

      foreach (int i in clb.CheckedIndices)  //clb is your checkListBox
          clb.SetItemCheckState(i, CheckState.Unchecked);
      clb.SelectionMode = System.Windows.Forms.SelectionMode.None;
      clb.SelectionMode = System.Windows.Forms.SelectionMode.One;
      
      • 取消选中所有复选框
      • 禁用 checkListBox 的选择
      • 启用 checkListBox 的选择

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-04
        • 2010-11-24
        • 2019-11-13
        • 1970-01-01
        • 2017-04-23
        • 1970-01-01
        • 1970-01-01
        • 2019-07-03
        相关资源
        最近更新 更多