473,382 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

ListBox Drag Items

Hi All,
I need some code to drag items in a list box either up or down along with
not just the text but with the itemdata too.
Can anyone hook me up?

TIA John
Jul 17 '05 #1
4 8473
> I need some code to drag items in a list box either up or
down along with not just the text but with the itemdata too.
Can anyone hook me up?
This may not be as "visual" as you might be looking for (can't be sure
because you didn't say); but the ItemData moves with the item. Here is a
previous post of mine that shows a method for doing that.

Rick - MVP

Use this sample project as a guide. Put a ListBox in a new project
(leave its Name as the default of List1). Paste this code into the
Form's code window and Run the project.

********START PASTE********
Dim DragIndex As Long

Private Sub Form_Load()
' Just load the ListBox up with something
With List1
For X = 0 To 10
.AddItem "Item #" & CStr(X + 1)
Next
End With
End Sub

Private Sub List1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With List1
DragIndex = .ListIndex
End With
End Sub

Private Sub List1_MouseUp(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With List1
If DragIndex <> .ListIndex Then
ListText = .List(DragIndex)
.RemoveItem DragIndex
.AddItem ListText, .ListIndex + Abs(Shift = vbShiftMask)
.ListIndex = .NewIndex
End If
End With
End Sub
********END PASTE********

Now click on an item and move your cursor to a new spot in the list.
When you release the mouse button, the item will be **inserted at** the
location of the mouse indicator; i.e., all existing items from the drop
point to the end of the list will placed after the item that was dragged
into the new position. This means that you can't drop an item into the
last position. To allow for that, you can press the Shift Key when you
drop the item. Doing that will place the dragged item **after** the
itemt the drop point. The Shift Key action works anywhere in the list,
but its main purpose is to allow an item to be dragged to the last
position.

There was a somewhat lengthy thread about a year ago in the vb.general n
ewsgroup on the news.devx.com public news server in which someone
offered code that inserted the dragged item **before** if that occurred
at a ListIndex lower in value than where the dragged item came from and
inserted the dragged item **after** otherwise. I found that somewhat
unnatural but others in the thread disagreed (if I recall correctly).
Here is that person's function and my response to his post:

"Reid Nix" <re******@nixlogic.com> wrote in message
news:3c******@10.1.10.29... ok...but if you simply load a var before the change , it will work without shift.
try it...this is all the code you need

Dim iFirst As Integer
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
If Button = 2 Then PopupMenu mnuItems
iFirst = List1.ListIndex
End Sub

Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim asd As String
Dim ii As Integer
ii = List1.ListIndex
asd = List1.List(iFirst)
With List1
.RemoveItem iFirst
.AddItem asd, ii
.ListIndex = .NewIndex
End With

End Sub


The only problem I have with your solution is the drop works differently
depending on whether the item is dragged up or down the list. If you
drag Item #5 and drop it on Item #10, it is placed **after** Item #10.
Now drag Item #9 and drop it on Item #3, it is placed **before** Item
#3. The location of the dropped item with respect to the item it's
dropped on is different depending on which way the item is dragged.
Think of the (old DOS type) word processor equivalent (where the cursor
highlighted a letter as opposed to being a thin line between
letters)...if I move the cursor so that the letter B is highlighted for
these characters ABC, then if I cursored left in order to highlight the
B, then the new text is inserted **before** the B; but if I had cursored
right to highlight the B, then the new text is inserted **after** the B.
Personally, I find that inconsistent.

Jul 17 '05 #2
Thanks, I'll give it a try, curious, I don't even see .ItemData in your
code. How does it get moved?

John G.
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:vY********************@comcast.com...
I need some code to drag items in a list box either up or
down along with not just the text but with the itemdata too.
Can anyone hook me up?


This may not be as "visual" as you might be looking for (can't be sure
because you didn't say); but the ItemData moves with the item. Here is a
previous post of mine that shows a method for doing that.

Rick - MVP

Use this sample project as a guide. Put a ListBox in a new project
(leave its Name as the default of List1). Paste this code into the
Form's code window and Run the project.

********START PASTE********
Dim DragIndex As Long

Private Sub Form_Load()
' Just load the ListBox up with something
With List1
For X = 0 To 10
.AddItem "Item #" & CStr(X + 1)
Next
End With
End Sub

Private Sub List1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With List1
DragIndex = .ListIndex
End With
End Sub

Private Sub List1_MouseUp(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With List1
If DragIndex <> .ListIndex Then
ListText = .List(DragIndex)
.RemoveItem DragIndex
.AddItem ListText, .ListIndex + Abs(Shift = vbShiftMask)
.ListIndex = .NewIndex
End If
End With
End Sub
********END PASTE********

Now click on an item and move your cursor to a new spot in the list.
When you release the mouse button, the item will be **inserted at** the
location of the mouse indicator; i.e., all existing items from the drop
point to the end of the list will placed after the item that was dragged
into the new position. This means that you can't drop an item into the
last position. To allow for that, you can press the Shift Key when you
drop the item. Doing that will place the dragged item **after** the
itemt the drop point. The Shift Key action works anywhere in the list,
but its main purpose is to allow an item to be dragged to the last
position.

There was a somewhat lengthy thread about a year ago in the vb.general n
ewsgroup on the news.devx.com public news server in which someone
offered code that inserted the dragged item **before** if that occurred
at a ListIndex lower in value than where the dragged item came from and
inserted the dragged item **after** otherwise. I found that somewhat
unnatural but others in the thread disagreed (if I recall correctly).
Here is that person's function and my response to his post:

"Reid Nix" <re******@nixlogic.com> wrote in message
news:3c******@10.1.10.29...
ok...but if you simply load a var before the change , it will work

without
shift.
try it...this is all the code you need

Dim iFirst As Integer
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)
If Button = 2 Then PopupMenu mnuItems
iFirst = List1.ListIndex
End Sub

Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As

Single,
Y As Single)
Dim asd As String
Dim ii As Integer
ii = List1.ListIndex
asd = List1.List(iFirst)
With List1
.RemoveItem iFirst
.AddItem asd, ii
.ListIndex = .NewIndex
End With

End Sub


The only problem I have with your solution is the drop works differently
depending on whether the item is dragged up or down the list. If you
drag Item #5 and drop it on Item #10, it is placed **after** Item #10.
Now drag Item #9 and drop it on Item #3, it is placed **before** Item
#3. The location of the dropped item with respect to the item it's
dropped on is different depending on which way the item is dragged.
Think of the (old DOS type) word processor equivalent (where the cursor
highlighted a letter as opposed to being a thin line between
letters)...if I move the cursor so that the letter B is highlighted for
these characters ABC, then if I cursored left in order to highlight the
B, then the new text is inserted **before** the B; but if I had cursored
right to highlight the B, then the new text is inserted **after** the B.
Personally, I find that inconsistent.

Jul 17 '05 #3
> Thanks, I'll give it a try, curious, I don't even see .ItemData in
your
code. How does it get moved?


It doesn't!<g> Now it would have if I had copied the correct file from
my archives though. Try this code instead of what I posted originally.

********START PASTE********
Dim DragIndex As Long
Dim OldItemData As Long

Private Sub Form_Load()
Dim X As Long
' Just load the ListBox up with something
With List1
For X = 0 To 10
.AddItem "Item #" & CStr(X + 1)
.ItemData(X) = X + 1
Next
End With
End Sub

Private Sub List1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With List1
DragIndex = .ListIndex
End With
End Sub

Private Sub List1_MouseUp(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With List1
If DragIndex <> .ListIndex Then
ListText = .List(DragIndex)
OldItemData = .ItemData(DragIndex)
.RemoveItem DragIndex
.AddItem ListText, .ListIndex + Abs(Shift = vbShiftMask)
.ItemData(.NewIndex) = OldItemData
.ListIndex = .NewIndex
End If
End With
End Sub
********END PASTE********

You can use this code (placed in a CommandButton Click event for
convenience) to view the end result.

Private Sub Command1_Click()
Dim X As Long
For X = 0 To 10
Debug.Print List1.List(X), List1.ItemData(X)
Next
End Sub

Rick - MVP

Jul 17 '05 #4
Ok Thanks again I think I have a soultion to avoid the shift key I'll work
it out and get back to you

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message
news:iL********************@comcast.com...
Thanks, I'll give it a try, curious, I don't even see .ItemData in

your
code. How does it get moved?


It doesn't!<g> Now it would have if I had copied the correct file from
my archives though. Try this code instead of what I posted originally.

********START PASTE********
Dim DragIndex As Long
Dim OldItemData As Long

Private Sub Form_Load()
Dim X As Long
' Just load the ListBox up with something
With List1
For X = 0 To 10
.AddItem "Item #" & CStr(X + 1)
.ItemData(X) = X + 1
Next
End With
End Sub

Private Sub List1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With List1
DragIndex = .ListIndex
End With
End Sub

Private Sub List1_MouseUp(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With List1
If DragIndex <> .ListIndex Then
ListText = .List(DragIndex)
OldItemData = .ItemData(DragIndex)
.RemoveItem DragIndex
.AddItem ListText, .ListIndex + Abs(Shift = vbShiftMask)
.ItemData(.NewIndex) = OldItemData
.ListIndex = .NewIndex
End If
End With
End Sub
********END PASTE********

You can use this code (placed in a CommandButton Click event for
convenience) to view the end result.

Private Sub Command1_Click()
Dim X As Long
For X = 0 To 10
Debug.Print List1.List(X), List1.ItemData(X)
Next
End Sub

Rick - MVP

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eric | last post by:
I'm trying to implement Drag and Drop with objects in list boxes and I'm not having any success. I can drag and drop strings, but when I try to change the code to drag an object it allows me to pick...
5
by: CoolWriter | last post by:
Hi, How can I drag and drop the items of a ListBox? For example, how can I drag the first item to the third items position in a ListBox? Thanks.
0
by: L.Peter | last post by:
Dear Group, I am doing Drag and Drop function and see this problem if I select 3 from listBox1 and drag to listBox2, the value in listBox1 is not updated, even I move (or use up and down keyboard)...
3
by: Dean Slindee | last post by:
In a checked listbox, I am allowing drag/drop of the items within (resequencing). Problem is, when dropping a checked item, the checked state always reverts to unchecked (unwanted). Anyone know...
1
by: Zyrthofar Blackcloak | last post by:
Hi everyone I have a listbox with more items in it than is printed on screen. I need the index number of the first shown item to move another listbox to the same position... Explained differently,...
11
by: John Dann | last post by:
I'm still struggling to find a way of reordering the items within the same single listbox with drag and drop. I think I've got the drag working but it's the drop code I can't figure out. What I...
3
by: thomasp | last post by:
Has anyone got some sample code to do drag and drop from one listbox to another listbox using VB.Net 2005. The below code works for draging and droping one at a time, but not for multiselected...
0
by: John Dann | last post by:
I had a listbox that allows reordering of listed items using the mouse to drag and drop. In the DragDrop event handler, the key instructions are: MyListBox..Items.RemoveAt(PreMoveIndex)...
3
by: Angel Blue01 | last post by:
I have a form with a binding navigator that draws from a database via drag-and-drop-created controls. I'm filling a listbox with the results of a query. The listbox contains data related to the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.