Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have listbox filled with many items. I want to multi select rows by clicking one row and dragging it. As we can do in excel or word. I want to achieve that functionality in listbox but without using Ctrl key.

Any help would be appriciated.

Thanks in advance
Posted
Updated 4-Aug-11 21:14pm
v2
Comments
BobJanova 3-Aug-11 8:36am    
I don't understand this question. What is wrong with using the Shift and Ctrl selection modes that the list box already has?
BillWoodruff 8-Aug-11 15:18pm    
Hi Samip, The example you mention, in your comment on my suggestion, is for WPF, should have no problem working in VS 2010, but will not work for Winforms. best, Bill
BillWoodruff 8-Aug-11 16:46pm    
Hi, It's been a long, long time since I used the standard MS ListBox control (I use a commercial suite for such controls). But, today, I tried the MS ListBox.

It appears to me that using the standard MS ListBox in MultiExtended Selection mode meets all of your criteria. All you have to do is hold the control-key down and you can picks rows at random, and you can also click a row and drag to select new rows. What's the problem ? :)
samip shrestha 9-Aug-11 2:04am    
Hi BillWoodruff

Thank you very much for the solution....it really worked and what i wanted.
Thanks again.
Really appreciated.
samip shrestha 10-Aug-11 3:05am    
Hi Billwoodruff...
Yesterday i said it was working...but it was having a small problem.
First i hold control key down ....and dragged to select more items. When i clicked Ctrl C...to paste the selected items in word... the selected items was
the items whose first character of the list box is C.
I have made a context menu and that works fine ..but i want Ctrl C functionality as many users are familiar with it.
So can u give some idea what i should do so as to identify that i am about to copy the selected items if i click Ctrl C not to select other items whose first character is C.


Thanks in advance.

You can set the Listbox.SelectionMode to MultiSimple

http://msdn.microsoft.com/en-us/library/system.windows.forms.selectionmode.aspx
 
Share this answer
 
Comments
Toniyo Jackson 2-Aug-11 3:16am    
Not MultiSimple. Its MultiExtended. For MultiSimple we have to use Ctrl key.
Amund Gjersøe 2-Aug-11 3:23am    
No, I've tested, and it's the other way around.
Try this.
listBox1.SelectionMode = SelectionMode.MultiExtended;
 
Share this answer
 
Comments
samip shrestha 2-Aug-11 3:36am    
Hi, Toniyo.
Thanks for the reply. Your solution is not exactly what i wanted. let me tell you little things about the issue i have.

I think my explanation is not clear enough. I have many items in the listbox. I have the requirement to select multiple rows in two ways: I need to select multiple rows randomly say rows 1,2,3,6,9,12 etc and also I want to multi select rows by clicking one row and dragging it in a continuous manner say from rows 1 to 5.

And i am developing a window application.

Thanks.
Using SelectionMode = MultiSimple seems to work like you describe.
 
Share this answer
 
Comments
Amund Gjersøe 2-Aug-11 3:22am    
Hehe, maybe I should have refreshed the page before posting.
If you really want to select different items in listbox , then why not use CheckedListBox?
 
Share this answer
 
Comments
samip shrestha 2-Aug-11 3:52am    
i cant use the checkedlistbox, user dont want to have checkbox.
... edit #1 ...

It is possible to simulate click-drag-rectangle selection over a WinForms collection-container control, but to do it, you are going to have to use an overlay of a transparent something: if that something is a Panel you make transparent via over-riding 'CreateWindowEx using the 'WS_EX_TRANSPARENT setting, you're probably not going to be satisfied, visually, with the result; if that something is a Form, you can set the opacity of the Form to give you good visual feedback.

To implement this solution means you are going to have to really "get down" with the ListBox control, and figure out how to detect where the MouseDown that triggers selection is, and, on MouseUp, you are going to need to iterate the rows of the ListBox and test for 'containment' in the bounding box of the selection rectangle at the time of MouseUp.

This is non-trivial, and may require you to handle some quirks of the ListBox ... which are unknown to me ... so I can't say with confidence doing this would meet your needs: I have implemented this on a TreeView, and it works, but use caution if you pursue this strategy.

... end edit #1 ...

In your message title, you imply you do not want to drag the mouse, but in the body of your message, you mention dragging ... that's kind of ambiguous ... so ...

I think what you are saying here is that you want the option to mouse-down on a row, and then click-drag a selection rectangle, and, when you mouse-up, then all rows within the selection rectangle are selected.

Some commercial .NET List-type controls support such selection-rectangle use, like one I use now.

If you want this selection-rectangle facility with the 'native' Windows .NET ListBox control ... that means you are going to actually need to draw a selection rectangle yourself, and compute the intersection with rows, then select them.

It may mean you will need to sub-class the native Windows ListBox, and handle the Paint event yourself; perhaps a daunting task ?

But, there may well be an owner-drawn sub-class of the old COM-based .NET ListBox here on CodeProject, or an example of using a selection rectangle with some other type of collection-manager control which you could apply to a ListBox: it's worth searching for.

good luck, Bill
 
Share this answer
 
v4
Comments
samip shrestha 8-Aug-11 2:29am    
Hi BillWoodruff..
Thanks for the idea. i have found one the example here on CodeProject
http://www.codeproject.com/KB/WPF/listboxselector.aspx?msg=3984906#xx3984906xx

but i cant make it run in VS 2008. I guess its developed in VS 2010.
Hi Everyone,

I would like to thank everyone who gave their idea to help me. The concept given by Mr BillWoodruff works perfectly for me ...hold the control-key down and I can pick rows at random and I can also click a row and drag to select new rows.

Thanks again
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900