Move items in a ListView from one to another

All tutorials created in C# to be posted in here.
5 posts Page 1 of 1
Contributors
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

C# Version

Moving items from one ListView to another is not something included in the .NET Framework. This tutorial will show you how to accomplish it yourself.
First, create your Windows Forms Project and add a ListView to Form1.
We will use 4 events from the ListView: ItemDrag, DragEnter, DragOver and DragDrop.
The code for ItemDrag:
Code: Select all
        private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            ListViewItem[] items = new ListViewItem[listView1.SelectedItems.Count];

            for (int i = 0; i < listView1.SelectedItems.Count; i++) 
                items[i] = listView1.SelectedItems[i];

            DataObject data = new DataObject("ListViewItem[]", items);
            listView1.DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Move);
        }
This collects every selected item in the ListView and stores them in a DataObject class.
The DataObject class is a class to store the format and data of anything that is being dragged in a drag-drop operation. Then we use the DoDragDrop method in the ListView to start the operation. The second parameters specify which effects are applied to the mouse cursor.

The next piece is the DragEnter event. It is excecuted when the mouse enters the ListView during a drag-drop operation.
Code: Select all
        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("ListViewItem[]"))
            {
                if ((e.KeyState & 8) == 8)
                {
                    e.Effect = DragDropEffects.Copy;
                }
                else
                {
                    e.Effect = DragDropEffects.Move;
                }
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

First it checks if the "ListViewItem[]" format is on the drag-drop 'clipboard'. If it is, it continues to check if the Control key is down. The mouse effect is Copy if it is pressed, and only move if its not pressed. If the "ListViewItem[]" data format is not available then there is no effect and the drop will not be accepted.

The 3rd handler is the DragOver event, it is executed whenever the mouse is over the ListView's bounds with something dragged. The code is the same as the DragEnter code, but it updates all the time so the the mouse cursor looks as it would either copy or move the items.

The last event to handle is the DragDrop event. When you drop the items, we have to update the items in both the ListView where the items were, and the ListView they are dropped to.
Code: Select all
        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("ListViewItem[]"))
            {
                ListViewItem[] items = e.Data.GetData("ListViewItem[]") as ListViewItem[];
                if ((e.KeyState & 8) == 8)
                {
                    foreach (ListViewItem i in items)
                        listView1.Items.Add((ListViewItem)i.Clone());
                }
                else
                {
                    foreach (ListViewItem i in items)
                        i.Remove();
                    listView1.Items.AddRange(items);
                }
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
This checks if there is data associated with the "ListViewItem[]" format. If there is, it saves the items in a ListViewItem[] variabe. If the Control key is down, it clones every item and adds it to the new ListView, but if not, it removes every item from the previous ListView and adds them to the new.
You do not have the required permissions to view the files attached to this post.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
DreadNought
VIP - Donator
VIP - Donator
Posts: 116
Joined: Fri Jan 08, 2010 12:37 pm

I just noticed it says ListView not ListBox -.-

But I created a code to copy items from a listbox to another, I really did think you used all that code for a listbox copy :P

This code is capable of copying(on my slow ass computer) 5000 values(9 digits long) in 2.2seconds.

5 values(9 digits long) in 0.1ms

1500 values (between 2-9 digits long) in 0.5ms

I am running 2GB ram, so bad results, but its fast. and I coded this so I might aswell release it even though its not really revelant
Code: Select all
        private void button1_Click_1(object sender, EventArgs e)
        {
            //Clear listbox1 then add 5000 random objects
            if (listBox1.Items.Count > 0)
                listBox1.Items.Clear();
            for (ushort u = 0; u < 1500; u++)
                listBox1.Items.Add(new Random().Next((u * 15)));

            DateTime start = DateTime.Now;
            object[] data = new object[listBox1.Items.Count];
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                data[i] = listBox1.Items[i].ToString();
            }

            //Copy to other listbox
            foreach (object item in data)
            {
                listBox2.Items.Add(item);
            }
            DateTime End = DateTime.Now;
            TimeSpan t = End - start;
            MessageBox.Show("Copied " + data.Length + " objects in: " + t.Seconds + " seconds and " + t.TotalMilliseconds, "Complete");
        }

Oh and I got bored so:

You can copy 500,000 values in: 10.247 seconds(not bad eh? Generating all the random values took like, a few minutes, but the actual copying is what matter)

Sorry I kind hijacked your thread, I wrote the code because of the thread, it might aswell go here :D

#edit
A better random number calculation would be:
Code: Select all
                listBox1.Items.Add(new Random().Next((DateTime.Now.Millisecond * 15 ^ 3)));
That way you wont have a problem if your for loop is a byte, ulong, sbyte, short, or whatever
Bound and boom tech,
The Future Of Coding
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

DreadNought wrote:
I just noticed it says ListView not ListBox -.-

But I created a code to copy items from a listbox to another, I really did think you used all that code for a listbox copy :P

This code is capable of copying(on my slow ass computer) 5000 values(9 digits long) in 2.2seconds.

5 values(9 digits long) in 0.1ms

1500 values (between 2-9 digits long) in 0.5ms

I am running 2GB ram, so bad results, but its fast. and I coded this so I might aswell release it even though its not really revelant
Code: Select all
        private void button1_Click_1(object sender, EventArgs e)
        {
            //Clear listbox1 then add 5000 random objects
            if (listBox1.Items.Count > 0)
                listBox1.Items.Clear();
            for (ushort u = 0; u < 1500; u++)
                listBox1.Items.Add(new Random().Next((u * 15)));

            DateTime start = DateTime.Now;
            object[] data = new object[listBox1.Items.Count];
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                data[i] = listBox1.Items[i].ToString();
            }

            //Copy to other listbox
            foreach (object item in data)
            {
                listBox2.Items.Add(item);
            }
            DateTime End = DateTime.Now;
            TimeSpan t = End - start;
            MessageBox.Show("Copied " + data.Length + " objects in: " + t.Seconds + " seconds and " + t.TotalMilliseconds, "Complete");
        }

Oh and I got bored so:

You can copy 500,000 values in: 10.247 seconds(not bad eh? Generating all the random values took like, a few minutes, but the actual copying is what matter)

Sorry I kind hijacked your thread, I wrote the code because of the thread, it might aswell go here :D

#edit
A better random number calculation would be:
Code: Select all
                listBox1.Items.Add(new Random().Next((DateTime.Now.Millisecond * 15 ^ 3)));
That way you wont have a problem if your for loop is a byte, ulong, sbyte, short, or whatever
You should know that ^ is not the mathematical power operator, it is the logical XOR operator in C#. Use Math.Pow(DateTime.Now.Millisecond * 15) instead ;)
You can also use listBox1.Items.CopyTo(data, 0) or directly listBox1.Items.CopyTo(listBox2.Items, 0) I think. I didn't test it though.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
DreadNought
VIP - Donator
VIP - Donator
Posts: 116
Joined: Fri Jan 08, 2010 12:37 pm

Array.Copy would be faster then CopyTo.
Bound and boom tech,
The Future Of Coding
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Array.CopyTo calls Array.Copy, so the only amount it will be faster is the time it takes to call another function ;)
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
5 posts Page 1 of 1
Return to “C-Sharp Tutorials”