473,765 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8489
> 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(B utton 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******@nixlo gic.com> wrote in message
news:3c******@1 0.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(B utton 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(iFir st)
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******** ************@co mcast.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(B utton 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******@nixlo gic.com> wrote in message
news:3c******@1 0.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(B utton 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(iFir st)
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(B utton As Integer, _
Shift As Integer, X As Single, Y As Single)
With List1
If DragIndex <> .ListIndex Then
ListText = .List(DragIndex )
OldItemData = .ItemData(DragI ndex)
.RemoveItem DragIndex
.AddItem ListText, .ListIndex + Abs(Shift = vbShiftMask)
.ItemData(.NewI ndex) = 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******** ************@co mcast.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(B utton As Integer, _
Shift As Integer, X As Single, Y As Single)
With List1
If DragIndex <> .ListIndex Then
ListText = .List(DragIndex )
OldItemData = .ItemData(DragI ndex)
.RemoveItem DragIndex
.AddItem ListText, .ListIndex + Abs(Shift = vbShiftMask)
.ItemData(.NewI ndex) = 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
1596
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 up the object and the mouse changes when I get over a place that will accept the drop but when I let go of the mouse button it sits there for about 3 seconds then the mouse turns to the normal pointer and the object isn't listed in the drop listBox...
5
9583
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
1332
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) to select number 4, the selectedItem.ToString() still shows 3 Am i doing something wrong? Here is the file **** using System;
3
2910
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 how to set the checked state of a checked listbox item in code. Here is the drag/drop code, which works fine, except for unchecking the dropped item: Private Sub clbQueryItems_DragDrop(ByVal sender As System.Object, ByVal e As...
1
1410
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, the two listboxes have the same number of items, and are linked and show the same portion to the screen. I have been having problems with a listbox in which we can drag&drop items to different indexes, at the same time as being able to...
11
1807
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 have currently is (with the listbox set to AllowDrop): Sub MyListBox_MouseDown(args etc...) DoDragDrop(MyListBox.SelectedItem, DragDropEffects.Move) End Sub Sub MyListBox_DragEnter(args etc...)
3
3305
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 items. I tried setting up an array to capture the selected items and then move them with the dragndrop code, but after selecting the items when the user clicks on the items to drag them the selection goes back to one item. Also I have code for the...
0
1106
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) MyListBox..Items.Insert(PostMoveIndex, itemtext) This all works fine. I then decided that I wanted to display a longer text description of
3
2001
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 record that the user is viewing but cannot be databound because other events and user input are needed when the user selects an item from the listbox. Each time the user clicks the binding navigator to move to the next record I have this: <code>
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9946
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6646
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5272
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2800
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.