how to keep an item focused in c# listview? - c#

i'm trying to keep the last pressed item highlighted in the listview, anyone know how can i do it?

I'm assuming you want to keep the selected item highlighted when the control loses focus.
Then you should check out the HideSelection property.

You can subscribe to the SelectedIndexChanged event and change the selected index/item to the last one.

Looks like you can use ListView::DrawItem for that. Have a look here for an example.

Try looking at the code example here: List view Highlight selected.
It shows how to set the foreground/background color of the listview selected item when you change focus away from the listview control.

Related

Getting value of Combobox on Hover c#

I'm trying to make an application where I have this combobox that automatically opens and closes on mouse enter and mouse leave events respectively and then gets the value of the combobox before i leave it.
I already have the dropdown behavior as given by someone who posted it online. But now I need to get the value of my combobox item without clicking the mouse. I've tried selectedvalue or selecteditem but all seems to require mouse clicking. Is there any way of getting the value of the combobox where the mouse points to before I leave/close the combobox?
You can use labels for the combobox items.
Add 'MouseMove' event and in the event take the label text.
Here is an example how to implement very closely connected behavior: ComboBox MouseLeave MouseEnter.

silverlight listbox selection area

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.

How to get the clicked ListView cell in Winforms?

Is there a way to get the clicked ListView cell when a user clicks somewhere inside the ListView?
I can get the current ListViewItem by SelectedItems[0] but I don't know which SubItem is clicked.
SubItems don't have events on their own type.
You can use the ListViewItem.GetSubItemAt Method to determine what sub item is under the mouse (if any).
ListView.SelectedIndices may help (.net 1.1>)

WPF Listview/Gridview setting selected item when combobox is clicked

I'm having issues using a GridView as a ListView's View, what I want to do is fire an event when a user makes a selection from a combobox within the Gridview and pass the selected item within the event.
My first issue is that when the user clicks the combobox within a row, the row isnt selected (meaning the selecteditem stays null unless they click elsewhere first). Is there a clean way to do this without trying to catch mouse clicks or anything?
Secondly theres no selectionchangecommited event on a WPF combobox, is there a cleaner way to check if a user has manually selected an option other than checking if the combobox is enabled?
Thanks
I am seeing a similar behavior. My hypothesis is that one or more layers of the DataTemplate of each item in the list is swallowing the RoutedEvent that should have resulted in a new selection. Is it possible to, in a generic way, tell the items in the DataTemplate that they should never stop the event from bubbeling or tunneling further, without having to override every focus-triggering event handler in code behind?
I 'solved' my issue by using the WPF Toolkit grid (http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29117) and a series of selected items, which is probably a cleaner solution anyway.
Doesn't explain the behavior of the GridView, which is to me unusual
Original link has died, believe this to be the one https://github.com/xceedsoftware/wpftoolkit/wiki/DataGrid

In my ListView control, is there a way for me to GET the index of a clicked item?

Pretty straight forward question, but I can't find a way to do it.
When a user double clicks an item in my ListView I want to save the index number of the clicked item.
Assuming that you have a reference to the ListViewItem:
listView.Items.IndexOf(listViewItem)
Or just access listView.SelectedIndices in the Activate event handler -- if your ListView is single-select then there'll be only one index in there.
It depends on which ListView you're using. All three have a property to get the selected item's index:
For Windows Forms, use ListView.SelectedIndices
For WPF, use ListView.SelectedIndex
For Web, use ListView.SelectedIndex

Categories