Home > Blockchain >  How can I force a listview to draw its checkboxes as radio buttons in Windows Forms?
How can I force a listview to draw its checkboxes as radio buttons in Windows Forms?

Time:12-03

I have a ListView and I have it behaving exactly as I want, I'm displaying check boxes and I'm only able to select a single item at a time. Now the problem is this is bad UX- it shouldn't use check boxes when only 1 selection at a time is allowed, it should use radio buttons. So I need to replace the checkboxes with radio buttons. I think the easiest way would be to preempt the drawing of the checkbox and replace it with a radio button glyph, but I'm not really sure how to get started there. Also, it needs to work on a ListView, not a ListBox, and it needs to work in WinForms, not ASP or WPF. I would really appreciate information on how you know this as well- if you can link to documentation/code that shows how drawing works for the ListView in particular that would be wonderful. I have found the docs for .NET 4.8 here and am perusing that now, but details would still be appreciated.

So my question in brief is, "How can I subclass a ListView (Not ListBox) to use radio buttons rather than checkboxes?"

CodePudding user response:

If the ListView.CheckBoxes property is true, you could use the ListView.StateImageList property as a source for images of checked/unchecked items.

Remarks section says:

The StateImageList property allows you to specify an ImageList that contains images to use to represent an application-specific state of an item in a ListView control. State images are displayed to the left of an icon for the item. You can use state images, such as checked and unchecked check boxes, to indicate application-defined item states. State images are visible in all views of the ListView control.

If an ImageList is specified in the StateImageList property and the CheckBoxes property is set to true, the images at index positions 0 and 1 in the ImageList are displayed instead of the check box. The image at index position 0 is displayed instead of the unchecked check box, and the image at index position 1 is displayed instead of the checked check box.

  • Related