开发者

Any way to limit a ListBox (or alternative) to selecting consecutive items?

Is there any nice way to configure a ListBox (WinForms) so that it is possible to select multiple items by click+drag, or by shift+click, but that only a consecutive range of items can be selected? Essentially, I'd like to make the selection behavior of the ListBox be (from a user perspective) rather like that of a TextBox, but with the restriction that only entire lines may be selected.

Right now I'm using a UserDrawVariable ListBox as an event log, but the scrolling behavior of the UserDrawVariable Listbox is somewhat wonky. I'd like to change it to a UserDrawFixed ListBox, using multiple items for things which are supposed to be taller (have an "ItemExtension" type for later items which would hold a reference to the first item, which would expose its draw routine to them). The only way I can think of to have the selection behavior work reasonably, though, would be to track the beginning and end of the select开发者_JS百科ion range, and draw as highlighted any items which are at least partially selected. Using individual item selection flags would seem wonky if a user clicks on the top half of a two-part "item" and then control-clicks the bottom half; clicking either half should highlight the item, and the control-click should unhighlight it, but since the clicks would be registered on separate items I don't think that would happen.

Perhaps ListBox really isn't the right control. I'd be open to suggestions for some better open-source control with an unrestricted license (I can't opensource my application).


Suggestion 1) Interept the Ctrl+Click on the control, and allow it through only if there is a) no selection or b) the clicked item is adjacent to the current selection.

Suggestion 2) Keep the last selection in a field in your listbox subclass, and in the selection changed event, verify that the new selection is valid (consecutive) and if not, set the last selection to give the impression of no change. If the selection is valid, promote it to the last selection field.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜