I created a ListView programmatically. I set the FullRowSelect option as true. After adding the elements to the ListView, if I start selecting the items, the row which is
currently selected is highlighted with a blue color. How can I disable the highlighting? I want to stop the highlighting of an item on its selection, but want FullRowSelect to be true.
Also, if I select an item, I want to change the color of the item so that anyone can easily identify which item is selected.
The only way to really control the display of items in a list is to use ownerdraw. This approach is not exposed in .NET, so will mean overriding the creation and WinProc of the list (at least).
Another option might be WPF, much of the styling of controls being overridable.
Related
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?"
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.
Silverlight TreeView doesnt have KeepSelected method as in Winforms.
When TreeView loses focus - the selection isnt visible however the items is still selected..
I'd like to preserve selection visual even if TreeView lost its focus.
Anyone tryied to work around this?
You can write a custom style or template for the treeview node, so that it would display the color of selected item as per your choice even when it is out of focus.
Am having a list box item template, using wrap panel in container to show as three column listbox. working well. now the issue is, i have to capture the selection change only when user clicks on the second column.
is it possible to set the selection area in listbox?
I don't think it is possible to somehow raise selection change event only when the second or third column is selected. One thing you can do is keep last selected column index in a variable and inside your selection change event judge whether the new selected item belongs to same column or not if it doesn't simply ignore the change
I agree with Haris, I don't think it it possible. You should be able to use the mouse down event and then in code figure out the selection index from that. You could possibly use LINQ on the list box's ItemSource and find the match or use the Tag property of the item.
I have a CheckedListBox in my Windows forms application, but items can be added that are to wide for the control to display. Is there any way to have the text wrap to the next line when this occurs?
I know with a standard ListBox this would not work, since it would be hard to tell the difference between the second line of one item and the next item. But with a CheckedListBox the beginning of the items in the list are defined by the CheckBox, so it should be easy to differentiate between items.
Is this possible? Will I have to create my own control (Again)?
The CheckedListBox does not support this, but you can use a DataGridView with two columns (checkbox column, text column) to achieve this with very little effort.
I have a ListView control, and I'm trying to figure out the easiest/best way to disallow changing the selected row(s), without hiding the selected row(s).
I know there's a HideSelection property, but that only works when the ListView is still enabled (but not focused). I need the selection to be viewable even when the ListView is disabled.
How can I implement this?
You could also make the ListView ownerdraw. You then have complete control over how the items look whether they are selected or not or whether the ListView itself is enabled or not. The DrawListViewItemEventArgs provides a way to ask the ListView to draw individual parts of the item so you only have to draw the bits you're interested in. For example, you can draw the background of the item but leave it up to the ListView to draw the text.
There are two options, change the selected rows disabled colors. Or change all the other rows to simulate they are disabled except for the selected one. The first option is obviously the easiest, and the second option obviously is going to need some extra protections.
I have actually done the first option before and it works quite well. You just have to remember to change the colors back to the defaults in case another row is selected later on in the process.
Implement SelectedIndexChanged and do this
private void listViewABC_SelectedIndexChanged(object sender, EventArgs e)
{
listViewABC.SelectedItems.Clear();
}