كيفية التخلص من checkedlistbox اختيار تسليط الضوء على تأثير ؟

StackOverflow https://stackoverflow.com/questions/334626

  •  22-07-2019
  •  | 
  •  

سؤال

عندما يتم النقر على بند في checkedlistbox ، فإنه يحصل على الضوء.كيف يمكن منع هذا تسليط الضوء على تأثير ؟

لا يمكن ربط في الحدث SelectedIndexChanged واضحة التحديد ، ولكن الضوء لا يزال يحدث و ترى ومضة.في الواقع, إذا كنت اضغط باستمرار على زر الماوس ، لم إطلاقه بعد النقر على مربع الاختيار على منطقة التحديد يظل مميزا حتى تقوم بتحرير زر الماوس.أنا في الأساس يريدون التخلص من هذا تسليط الضوء على تأثير تماما.

هل كانت مفيدة؟

المحلول

وهذا سوف نفعل ذلك غيرك لا يزال الحصول على بعض الشيء خط منقط.

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

          }

        }
    }

وإذا كان كل ذلك ليس ما بعد الخاص بك .. يمكنك دائما مجرد إجراء واحد الخاص بك. في عنصر تحكم بسيطة إلى حد ما.

نصائح أخرى

استخدم ما يلي:

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

وضبط SelectionMode إلى بلا لديه بعض القضايا غريبة مثل الحاجة إلى تنفيذ الحدث. يمكنك ترك مجموعة SelectionMode إلى واحد ثم قم بإنشاء الفئة التي يتجاوز CheckedListBox مع عادل OnDrawItem. لاحظ أنه من أجل إيقاف مظهر المحدد، لديك لإيقاف الدولة المختارة وضبط الألوان إلى ما تريد. يمكنك الحصول على اللون الأصلي من السيطرة كما فعلت هنا. هذا هو ما خطرت لي ويجب ان تحصل على انك بدأته في جعله يبدو ولكن تريد.

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

وأوه باردة إضافة وضع كافة التعليمات البرمجية من الجواب من هاث في

 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;

ووسوف تحقق من العناصر المميزة أثناء تحريك الماوس مع الضغط على الزر الأيسر وغير تسليط الضوء عليها مع الحق

أنا في وقت متأخر قليلا لإعطاء إجابة هنا.على أي حال, هذا سوف قم بإلغاء تحديد كافة خانات وإزالة تسليط الضوء على تأثير:

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 اختيار
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top