CheckedListBox Control - Only checking the checkbox when the actual checkbox is clicked

前端 未结 6 741
故里飘歌
故里飘歌 2021-01-02 01:14

I\'m using a CheckedListBox control in a small application I\'m working on. It\'s a nice control, but one thing bothers me; I can\'t set a property so that it only checks th

6条回答
  •  萌比男神i
    2021-01-02 01:23

    Try this. Declare iLastIndexClicked as a form-level int variable.

    private void chklst_MouseClick(object sender, MouseEventArgs e)
    {
      Point p = chklst.PointToClient(MousePosition);
      int i = chklst.IndexFromPoint(p);
      if (p.X > 15) { return; } // Body click. 
      if (chklst.CheckedIndices.Contains(i)){ return; } // If already has focus click anywhere works right.
     if (iLastIndexClicked == i) { return; } // native code will check/uncheck
      chklst.SetItemChecked(i, true);  
      iLastIndexClicked = i;
    }
    

    Just checking to see if the user clicked in the leftmost 15 pixels of the checked list (the check box area) works at all times except re-checking a currently selected item. Storing the last index and exiting without changing lets the native code handle that properly, trying to set it to checked in that case turns it on and it just turns back off when the "ItemCheck" code runs.

提交回复
热议问题