If I put a listview component into a windows form and add bellow code to it's SelectedIndexChanged event:
MessageBox.Show("Fired!");
foreach (int selectedIndex in listView1.SelectedIndices)
{
listView1.Items[selectedIndex].Selected = false;
listView1.Items[selectedIndex].Focused = false;
}
the message box will be shown 4 times! why is that?
Note: I use the loop to clear the selected items in listview
You should not change the selection in a SelectedIndexChanged event. More generally, you should not change a property inside of a notification that the property has been changed.
If you need to change a property in response to a notification, look to handle the corresponding *Changing event. Rather than being a notification that something has changed (which comes after the fact), it is a notification that something is about to change (which comes before the fact). In the SelectedIndexChanging event, you have a couple of different options to alter the course of events:
You can set the e.Cancel property to true, which will do just as it says. It will cancel the event and prevent the selected index from changing.
You can use the e.NewSelectedIndex property to alter the selection. Just set this property to the index of the item that you want to be selected.
And if you want to clear the selected items in a ListView in response to some other event (e.g., a click on a "Clear Selection" button that is not part of the ListView, or a similar context menu item), you don't need a loop at all. Just clear the control's SelectedItems collection: myListView.SelectedItems.Clear(). Again, you can't do this in response the SelectedIndexChanged event, or you'll have the same problem of triggering a bunch of notifications.
Honestly, though, the code you've written here makes no sense. Why would you want to clear all selected items when the user tries to select an item? If you don't want to allow selection, disable the control by setting its Enabled property to false.
Related
I have a Win Forms listbox whose contents are refreshed every few seconds. The listbox displays messages stored in a database to which people can respond - it's a customized instant messenger app. When the refresh happens the selectedindex automatically changes to 0 which is a problem if I have the 6th message selected and I am trying to responding to it. How can I determine if the index change is a result of the refresh or a mouse click on the listbox and then stop the selection change if it's not a mouse click?
I believe I have found a solution to my question. I wasn't sure how to do event handlers so I researched that. I added an event handler to the MessageListBox.click event. This event handler sets a property called selectedIndex and is the only place that sets this value. In the refresh method I set the MessageListBox.SelectedIndex to this value after the Update/Refresh call.
I have a listview for displaying a table inside of a button. I have a click event assigned to the button, but when the user clicks on the list view, a row is selected on the list view and the mouse click is never bubbled up to the button.
I'm stuck at this point, and need a way to solve this. All the examples I've seen online are for placing a button inside the listview. How do I make this work?
Set the IsHitTestVisible property to false on your ListView. Since it is a control that normally processes click events, doing this will have it ignore them.
If you don't want the items to be selectable in the first place, you could make the List View unable to receive Focus. Just set the Focusable property to false.
With the List View unable to receive focus, then I would expect the mouse event to bubble up to the button.
I would like to know if it is possible to make a difference in the event selection change of a combobox.
I want to make a difference between a user that manually click the combobox and changes its value and a change in the selection that i do from code.
ie :
if i click my combobox and change its value by hand, the event is fired
but if i do myCombobox.selectedItem=1 [edit] the events is not fired
Is there an event that has this behaviour in a wpfcombobox ?
If not, do you have any idea on how to do that ?
Thanks
[edit] or if it is the binding of the combobox that changes its value
You are dealing with a couple of different scenarios, both of which are solvable.
1) Don't process SelectedItem requests during databinding. You have at least two options here:
a) Don't add the event handlers to the control until after databinding is complete or the form is loaded (depending on whether or not databinding is automatic or manual).
b) Set a form level property indicating when it is ok to process the SelectedItem event. You will probably want to set this to true after the form is loaded or after the databinding is complete. In your SelectedItem code, don't perform any actions unless this property is true.
2) Process the SelectedItem logic if the SelectedItem is changed programatically. Again, two options:
a) Extract your logic from the SelectedItem event into a method and then call this method when you perform the logic to set the selected item.
b) Create a custom combobox that inherits from the base and add a SetSelectedItem method (for example) to this inherited combo. This method would then raise the SelectedItem method. This would be reusable and you wouldn't have to remember to do 2 pieces of work whenever you set the SelectedItem manually.
Before you change the selecteditem in code, remove the event handler with -=, then add it back afterwards.
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
I have a DataGridView control in a TabPage, and I listen for the SelectionChanged event. When the TabPage is selected, the DataGridView selects the first row and fires the SelectionChanged event. How can I stop it from automatically selecting a row?
I can think of two ways of working around this, If you know the event always fires, Have a bool flag that is set on the first selection, then only perform the rest of your SelectionChanged code.
The other way is to have a hidden control that is the first TabStop for the TabPage and hence is the first control to get focus (I say this without having tested any this of course, just my two cents).
Override the function "OnEnter" of the TabPage.
Call the function "ClearSelecton" of the DataGridView within this function.