Moving items in a listbox in c#
NickName:Sudarshan Taparia Ask DateTime:2016-08-15T16:00:28

Moving items in a listbox in c#

I am adding file names from a directory in a list box which I am able to add successfully.I then should be able to arrange the files in the list box by selecting a particular item & then moving it up or down.

The below function is an attempt to move the file up. I am trying to copy entire listbox items to an object and selected listbox items to a different object.

Then if button is pressed the selected item should be swapped with the item above to it in the list.

I intend to change the new list and copy the items back to the original listbox.But I am unaware how to copy items from object back to the listbox

The below code does not work

private void plus_Click(object sender, EventArgs e)
    {


        object[] items = new object[listBox1.Items.Count];

        listBox1.Items.CopyTo(items, 0);

        object[] selecteditems = new object[listBox1.SelectedItems.Count];

        listBox1.SelectedItems.CopyTo(selecteditems, 0);

        object[] selectedindices = new object[listBox1.SelectedItems.Count];

        listBox1.SelectedIndices.CopyTo(selectedindices, 0);



       // listBox1.Items.CopyTo(items, 0);

        int upper_index = listBox1.Items.Count - 1;

        for (int i = 0; i < listBox1.Items.Count;i++ )
        {
           if ((i == (int)selectedindices[i])&&(i!=0))
            {
                    object temp = items[i];
                    items[i] = items[i - 1];
                    items[i - 1] = temp;
           }
        }








    }

Copyright Notice:Content Author:「Sudarshan Taparia」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/38951357/moving-items-in-a-listbox-in-c-sharp

More about “Moving items in a listbox in c#” related questions

Moving items in a listbox in c#

I am adding file names from a directory in a list box which I am able to add successfully.I then should be able to arrange the files in the list box by selecting a particular item &amp; then movin...

Show Detail

C# Moving items that are listed in listbox into another folder

thank you for your help! I have been trying to list the names of files into Listbox and move the files that are listed into the Listbox into another folder but I couldn't do it. I already got the p...

Show Detail

moving object from listbox to another listbox in C# winform

How i can moving object( that is include name and id) from listbox to another listbox and save it? i wrote this: if (lstActivity.SelectedIndex != -1) { int intSelecte...

Show Detail

Moving items in a ListBox

I have a ListBox that has a bunch of items in it. However, I want to be able to sort the items in the list based on a match in the Item. So if I had some items that looked like this in the ListBox...

Show Detail

Moving all selected items in a ListBox with DataSource

I have a ListBox which has an ObservableCollection&lt;string&gt; as its DataSource. Now I want to be able to move every selected item up or down. So if the list looks like this (selected items are

Show Detail

Transfer multi selected items from ListBox to another ListBox

I would like to know how to move the items I have selected from left ListBox to the right ListBox? I tried to search but I've only seen moving the single item or all items from one ListBox to another.

Show Detail

Move list box items from one listbox to another listbox?

I am trying to move listbox items between two listboxes.Before that i created two arrraylists for each listbox.so before moving the listbox item i am trying to add the items to the arraylist. her...

Show Detail

ListBox long items hints

There's a ListBox with some long items. These long items are getting beyond the right edge of the ListBox and here comes an idea to show hints for such items when the mouse is over them. I've foun...

Show Detail

moving items from one listbox to another c# windows application

I have 2 listboxes in my application. The data is being retrieved from a SQL server database. In listbox1, I want to select few items from this listbox and add them to second one through add butt...

Show Detail

Listbox item move is moving the wrong items

I have some C# functions that are supposed to move ListItems from one ASP.NET ListBox to another, and this is what they look like: protected void btnSelectRec_Click(object sender, EventArgs e) {

Show Detail