Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8318783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:05:50+00:00 2026-06-08T22:05:50+00:00

I have a ListView (WinForms) in which i want to move items up and

  • 0

I have a ListView (WinForms) in which i want to move items up and down with the click of a button. The items to be moved are the ones who are checked. So if item 2, 6 and 9 are selected, they will become 1, 5 and 8 when I press the button for movement upwards and the items that were on those places are moved down a step.

I feel that I have made this unnecessarily complicated, as you can see below. The second SubItem of each ListViewItem is a number which represents its place in the list (starts on 1).

I blame lack of sleep and coffee for the following code, but if you could figure out a simpler way to complete this task I would be thankful.

private void sourceMoveUpButton_Click(object sender, EventArgs e)
    {
        List<Int32> affectedNumbers = new List<Int32>();
        bool foundNonChecked = false;

        List<KeyValuePair<int, ListViewItem>> newList = new List<KeyValuePair<int, ListViewItem>>();

        foreach (ListViewItem item in this.sourceListView.CheckedItems)
        {
            int newNum = int.Parse(item.SubItems[1].Text) - 1;

            if (newNum >= 1)
            {
                foreach (ListViewItem testItem in this.sourceListView.Items)
                {
                    if (int.Parse(testItem.SubItems[1].Text) == newNum && !testItem.Checked)
                    {
                        foundNonChecked = true;
                    }
                }

                if (foundNonChecked)
                {
                    item.SubItems[1].Text = newNum.ToString();
                    affectedNumbers.Add(newNum);
                }
            }
        }

        foreach (ListViewItem item in this.sourceListView.Items)
        {
            int num = int.Parse(item.SubItems[1].Text);

            if (affectedNumbers.Contains(num) && !item.Checked)
            {
                item.SubItems[1].Text = (num + affectedNumbers.Count).ToString();
            }

            newList.Add(new KeyValuePair<int, ListViewItem>(int.Parse(item.SubItems[1].Text), item));
            item.Remove();
        }

        newList.Sort((firstPair, secondPair) =>
            {
                return firstPair.Key.CompareTo(secondPair.Key);
            }
        );

        foreach (KeyValuePair<int, ListViewItem> pair in newList)
        {
            this.sourceListView.Items.Add(pair.Value);
        }
    }

EDIT
I have shorted it down to the following:

foreach (ListViewItem item in this.sourceListView.CheckedItems)
        {
            if (item.Index > 0)
            {
                int newIndex = item.Index - 1;
                this.sourceListView.Items.RemoveAt(item.Index);
                this.sourceListView.Items.Insert(newIndex, item);
            }
        }

        int index = 1;
        foreach (ListViewItem item in this.sourceListView.Items)
        {
            item.SubItems[1].Text = index.ToString();

            index++;
        }

But now, if I select the two topmost items (or similar) they will switch place when I click the button for upwards movement.

SECOND EDIT
Everything works fine for upwards movement with the following:

if (this.sourceListView.CheckedItems[0].Index != 0)
        {
            this.sourceListView.BeginUpdate();

            foreach (ListViewItem item in this.sourceListView.CheckedItems)
            {
                if (item.Index > 0)
                {
                    int newIndex = item.Index - 1;
                    this.sourceListView.Items.RemoveAt(item.Index);
                    this.sourceListView.Items.Insert(newIndex, item);
                }
            }

            this.updateListIndexText();

            this.sourceListView.EndUpdate();
        }

But for downward movement I can’t seem to get it right:

if (this.sourceListView.CheckedItems[this.sourceListView.CheckedItems.Count - 1].Index < this.sourceListView.Items.Count - 1)
        {
            this.sourceListView.BeginUpdate();

            foreach (ListViewItem item in this.sourceListView.CheckedItems)
            {
                if (item.Index < this.sourceListView.Items.Count - 1)
                {
                    int newIndex = item.Index + 1;
                    this.sourceListView.Items.RemoveAt(item.Index);
                    this.sourceListView.Items.Insert(newIndex, item);
                }
            }

            this.updateListIndexText();

            this.sourceListView.EndUpdate();
        }

It works for moving single items down, but when I select more than one, it doesn’t.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:05:52+00:00Added an answer on June 8, 2026 at 10:05 pm

    Try something like this:

    foreach (ListViewItem lvi in sourceListView.SelectedItems)
    {
        if (lvi.Index > 0)
        {
            int index = lvi.Index - 1;
            sourceListView.Items.RemoveAt(lvi.Index);
            sourceListView.Items.Insert(index, lvi);
        }
    }
    

    Basically just removes the item then inserts it above of where it used to be. The ListView automatically handles reshuffling the items down the order after an insert so no worries.

    Edit:
    The reason the two topmost items swap is that the top item will never move (i.e I haven’t implemented a wrap-around move. The 2nd item, however, is free to move and thus goes to the top of the list.

    To resolve this, you can do 1 of 2 things:

    1. Implement a wrap-around reshuffle (i.e top item goes to the bottom)
    2. Prevent any movement if the top item is selected (check listview.Items[0].Selected)

    As for the re-doing of the text, just do it in the original loop.

    Implementation with wraparound:

    foreach (ListViewItem lvi in sourceListView.SelectedItems)
    {
        int index = lvi.Index > 0 ? lvi.Index - 1 : sourceListView.Items.Count - 1;
        sourceListView.Items.RemoveAt(lvi.Index);
        sourceListView.Items.Insert(index, lvi);
    
        if (index != sourceListView.Items.Count - 1) //not a wraparound:
        {
            //just swap the indices over.
            sourceListView.Items[index + 1].SubItems[1].Text = (index + 1).ToString();
            lvi.SubItems[1].Text = index.ToString();
        }
        else //item wrapped around, have to manually update all items.
        {
            foreach (ListViewItem lvi2 in sourceListView.Items)
                lvi2.SubItems[1].Text = lvi2.Index.ToString();
        }
    }
    

    Edit 2:

    Static helper implementation, no wrap-around:

    private enum MoveDirection { Up = -1, Down = 1 };
    
    private static void MoveListViewItems(ListView sender, MoveDirection direction)
    {
        int dir = (int)direction;
        int opp = dir * -1;
    
        bool valid = sender.SelectedItems.Count > 0 &&
                        ((direction == MoveDirection.Down && (sender.SelectedItems[sender.SelectedItems.Count - 1].Index < sender.Items.Count - 1))
                    || (direction == MoveDirection.Up && (sender.SelectedItems[0].Index > 0)));
    
        if (valid)
        {
            foreach (ListViewItem item in sender.SelectedItems)
            {
                int index = item.Index + dir;
                sender.Items.RemoveAt(item.Index);
                sender.Items.Insert(index, item);
    
                sender.Items[index + opp].SubItems[1].Text = (index + opp).ToString();
                item.SubItems[1].Text = (index).ToString();
            }
        }
    }
    

    Example:

    MoveListViewItems(sourceListView, MoveDirection.Up);
    MoveListviewItems(sourceListview, MoveDirection.Down);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to display a ListView in WinForms which should not have any lines
I have a winforms listview with 200 items shown in a details listview. 50
I have another problem with ListView :( Now I need to move items in
Hey all, I have a winforms virtualized Listview that i want to be able
I have an activity which have ListView. When I want to access this ListView
I have listview control.There is an option to remove selected items.After the user removes
I have listview having customized some textview and one imageview. When I long click
I have ListView in my project which has ListItems as in the form of
I have ListView which is saving all data to database. For adding i have
I have a ListView with Horizontal WrapPanel as its ItemsPanelTemplate. I want to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.