示例#1
0
 /// <summary>
 /// Left mouse click: check/uncheck the item.
 /// Middle mouse click: remove item.
 /// Right mouse click: send item from chList1 to chList2.
 /// </summary>
 /// <param name="chList1"> The handled checkedListBox. </param>
 /// <param name="chList2"> The dual checkedListBox. </param>
 /// <param name="e"> The mouse click event. </param>
 public static void DualChListMouseEvent(CheckedListBox chList1, CheckedListBox chList2, MouseEventArgs e, Point mousePosition, bool useDual = true)
 {
     if (e.Button == System.Windows.Forms.MouseButtons.Right)
     {
         for (int i = 0; i < chList1.Items.Count; i++)
         {
             if (i != 0 || useDual)
             {
                 if (chList1.GetItemRectangle(i).Contains(chList1.PointToClient(mousePosition)))
                 {
                     chList2.AddOrderedFromBottom(chList1.GetText(i), chList1.GetItemChecked(i));
                     chList1.Items.RemoveAt(i);
                 }
             }
         }
     }
     else if (e.Button == System.Windows.Forms.MouseButtons.Middle)
     {
         for (int i = 0; i < chList1.Items.Count; i++)
         {
             if (i != 0 || useDual)
             {
                 if (chList1.GetItemRectangle(i).Contains(chList1.PointToClient(mousePosition)))
                 {
                     chList1.Items.RemoveAt(i);
                 }
             }
         }
     }
     else if (e.Button == System.Windows.Forms.MouseButtons.Left)
     {
         for (int i = 0; i < chList1.Items.Count; i++)
         {
             if (chList1.GetItemRectangle(i).Contains(chList1.PointToClient(mousePosition)))
             {
                 switch (chList1.GetItemCheckState(i))
                 {
                     case CheckState.Checked:
                         chList1.SetItemCheckState(i, CheckState.Unchecked);
                         break;
                     case CheckState.Indeterminate:
                     case CheckState.Unchecked:
                         chList1.SetItemCheckState(i, CheckState.Checked);
                         break;
                 }
             }
         }
     }
 }