Click here to Skip to main content
15,905,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a ListView (Windows form) with checkboxes and those checkboxes have three states: unchecked, checked and mixed.

When I click in checkbox, first appears the checked state, then the mixed and finally the unchecked. However I want jump the mixed state (first checked and then unchecked). The mixed state must be only set programmatically.

There is any way to achieve this behavior?

Kind regards,
Filipe Marques

What I have tried:

I had try to override the method ItemChecked and the MouseDown but I could not achieve the pretended behavior.
Posted
Updated 18-Feb-16 13:21pm

I figure out a solution for my problem.

C#
protected override void OnMouseDown(MouseEventArgs e) {
	ListViewItem item = this.GetItemAt(e.X, e.Y);
	if (item != null) {
		ListViewHitTestInfo loc = this.HitTest(e.Location);

		// detect if the click was in state image area
		if (loc.Location.Equals(ListViewHitTestLocations.StateImage)) {
			if (item.StateImageIndex == 1) { // indeterminate
				item.Checked = false;
				item.StateImageIndex = 2; // unchecked
				return;
			}
		}
	}

	base.OnMouseDown(e);
}
 
Share this answer
 
The Checkbox Click event handler is fired whenever the Checked value changes (either via keyboard or mouse) but is not fired when the value is changed programatically.
Within the event handler insert the following code
C#
if(this.checkBox.CheckState == CheckState.Indeterminate)
{
    this.checkBox.Checked = false;
}

The checked state go throu Checked/Indeterminate/Unchecked, therefore whenever the state is going to indeterminate the checkbox was previously checked

Kind Regards
 
Share this answer
 
Comments
Filipe Marques 18-Feb-16 19:27pm    
Hi anOther1, thanks for your help. I don't know if I was clear in my question but the checkbox that I refer was the checkboxes of the listview by setting 'CheckBoxes' property to true. Meanwhile, I figure out a solution for my problem that I posted here. Thanks any way. King regards, Filipe Marques

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900