2008-12-02 99 views
13

當在checkedlistbox中點擊一個項目時,它會被突出顯示。我怎樣才能防止這種突出顯示效果?如何擺脫checkedlistbox選擇突出顯示效果?

我可以掛入SelectedIndexChanged事件並清除選擇,但突出顯示仍然發生,並且您看到一個blip。事實上,如果您按住鼠標點擊,在您點擊複選框區域後再也不會釋放它,選擇將保持高亮顯示,直到鬆開鼠標按鈕。我基本上想完全擺脫這種突出效果。

回答

11

這將做它除了你仍然得到虛線位。

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

      } 

     } 
    } 

如果一切是不是以後有什麼你..你總是可以做出自己的一個。它是一個相當簡單的控制。

+3

似乎將在MouseDown處理程序中使用checkedListBox1.IndexFromPoint(ex,ey)更有效,而不是o f遍歷GetItemRectangle結果。 – Eyal 2009-12-09 23:46:29

7

將SelectionMode設置爲None有一些奇怪的問題,比如必須實現Click事件。您可以將SelectionMode設置爲single,然後使用OnDrawItem創建一個覆蓋CheckedListBox的類。請注意,爲了關閉選定的外觀,您必須關閉「選定」狀態並將顏色設置爲所需的顏色。您可以像我這樣在控件上獲得原始顏色。這是我想出來的,應該讓你開始讓它出現,但是你想要的。

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

我喜歡這個解決方案,但沒有一個小小的調整,導致我的`Form1.cs [Design]`視圖崩潰。在整個方法塊中添加一個簡單的try/catch,下面的代碼爲我解決了這個問題,現在它運行得非常漂亮:catch(Exception){base.OnDrawItem(e); }` – Michael 2015-08-10 19:31:24

21

使用以下:

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

非常簡單。 – KoZm0kNoT 2014-01-28 17:35:50

0

哇涼加 把所有的從豈有答案代碼在

checkedListBox1_MouseMove(object sender, MouseEventArgs e) 

加入,如果到交換機

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

a第二

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

,它會檢查突出顯示的項目,你用左鍵移動鼠標按下 和正確

0

取消選中它們,我有點晚在這裏給出一個答案。無論如何,這將全部取消複選框並刪除突出效果:

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的選擇