2008-12-02 17 views
13

İşaretli liste kutusuna bir öğe tıklandığında, vurgulanır. Bu vurgulama efektini nasıl önleyebilirim?İşaretli liste seçimi vurgulama efekti nasıl kaldırılır?

SelectedIndexChanged olayına girebilir ve seçimi temizleyebilirim, ancak vurgulama hala gerçekleşir ve bir uyarı görürsünüz. Aslında, fare tıklığını basılı tutarsanız, onay kutusu alanını tıkladıktan sonra asla bırakmazsanız, fare düğmesini serbest bırakana kadar seçim vurgulanır. Bu vurgulayıcı etkiden tamamen kurtulmak istiyorum.

cevap

11

Bu, sizden hala noktalı çizgi bitini almanıza yardımcı olur.

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

bunu gibi bir şey yapmanız gerekecek yani şimdi ... onay kutularını tıklayın edemez halde:

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

      } 

     } 
    } 

değil mi hepsi eğer sonra ne sizin .. her zaman sadece kendikini yapabilir. Oldukça basit bir kontrol.

+3

olur Öyle görünüyor yerine mouseDown işleyicide checkedListBox1.IndexFromPoint (ex, ey) kullanmak için daha verimli olması GetItemRectangle sonuçları aracılığıyla döngü. – Eyal

7

SelectionMode'u Hiçbiri olarak ayarlama, Click olayını uygulamak gibi bazı garip sorunlara yol açar. SelectionMode setini bekar bırakabilir ve CheckedListBox'ı yalnızca OnDrawItem ile geçersiz kılan bir sınıf oluşturabilirsiniz. Seçilen bir görünümü kapatmak için, Seçili durumu kapatmanız ve renkleri istediğiniz gibi ayarlamanız gerektiğini unutmayın. Orijinal rengini burada yaptığım gibi kontrolden alabilirsiniz. Bu, geldiğim şey ve istediğin gibi görünmesini sağlamak için başlaman gerekiyor.

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

Bu çözümü seviyorum, ancak bir küçük ayar olmadan “Form1.cs [Tasarım]” görünümümün çökmesine neden oluyordu.Tüm yöntem bloğu etrafında basit bir deneme/yakalama ekleme, aşağıdaki kod ile benim için sorunu çözdü ve şimdi güzel çalışıyor: 'yakalamak (özel durum) {base.OnDrawItem (e); } ' – Michael

21

aşağıdaki kullanın:

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

Zekice basit. – KoZm0kNoT

+0

Ancak bu, ürünlerin kontrol edilmesini/işaretlerinin kontrol edilmesini engeller ... – horgh

+0

tanklar bu beni deli ediyor. İşaretli liste kutusunun birden fazla seçim durumu var gibi .. neden bunu varsayılan olarak istiyorsun ?! –

0

serin ooh bir

checkedListBox1_MouseMove(object sender, MouseEventArgs e) 

yılında Hathlere gelen cevaptan koymak tüm kodu eklentinin eklerseniz anahtarı

için fare düğmesi bit
case CheckState.Checked: 
    if (e.Button == MouseButtons.Right) 
    { 
     checkedListBox1.SetItemCheckState(i, CheckState.Unchecked); 
    }      
    break; 

a nd

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

ve sol düğmesi ile fareyi hareket ettirdikçe vurgulanır öğeleri kontrol edecek preslenmiş ve burada bir cevap vermek için biraz geç kaldım sağ

0

onları un-vurgulayın. Neyse, bu irade işaretini tüm işaret kutuları ve vurgulayarak etkisini kaldırmak:

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; 
  • işaretini kaldırın tüm onay kutularını
  • Devre Dışı CheckListBox seçimi
  • CheckListBox seçimi etkinleştirme
İlgili konular