Winforms' ListBox
appears to have a strange behavior. When I set the SelectionMode
to one, I expect that I can click an item, and it becomes selected. This holds true, but if I click an item, drag up and down the list, the selection changes.
Now, that wouldn't be too much of a big deal except that I need to perform drag and drop between some controls. So, when they select an item, and drag it down the list, a newly-selected item is actually the one it registers as dragging, and the wrong item gets sent over.
So, I then further bandage it by saving a reference to the selected item on mousedown, but it winds up as a bad user experience. My users drag an item to the other listbox, which does work, but the original listbox doesn't have the "correct" item selected anymore, and they become confused as to which item was actually dropped on the second control.
So, is there any way to change this sort of behavior? I want an item to be selected at MouseDown, ignoring the MouseUp part of things. Simply consuming the event doesn't seem to be enough, and I'd rather not have to override ListBox (we have to write documents for any new classes being created).
I guess if you call
DoDragDrop
this behavior would disappear. Windows doesn't dispatchMouseOver
messages while in drag&drop mode.Example of propper drag&drop:
...and
SelectedIndexChanged
still works!