Click here to Skip to main content
15,891,777 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to run a new executable with data? Pin
Eddy Vluggen5-May-09 7:11
professionalEddy Vluggen5-May-09 7:11 
QuestionListView item move up-down Pin
CodingLover5-May-09 5:37
CodingLover5-May-09 5:37 
AnswerRe: ListView item move up-down Pin
Jimmanuel5-May-09 6:00
Jimmanuel5-May-09 6:00 
QuestionRe: ListView item move up-down Pin
CodingLover5-May-09 6:24
CodingLover5-May-09 6:24 
AnswerRe: ListView item move up-down Pin
Jimmanuel5-May-09 6:36
Jimmanuel5-May-09 6:36 
QuestionRe: ListView item move up-down Pin
CodingLover5-May-09 16:10
CodingLover5-May-09 16:10 
AnswerRe: ListView item move up-down Pin
Jimmanuel6-May-09 0:45
Jimmanuel6-May-09 0:45 
AnswerRe: ListView item move up-down Pin
RichardWakelin27-Aug-09 2:12
RichardWakelin27-Aug-09 2:12 
Just encountered some exceptions with my original attempt to solve this when I stumbled across this thread.

Here's my current solution (seems pretty robust so far):

==== Pop this in a file ListViewSelectionMover.cs to keep things neat ====

using System.Windows.Forms;

namespace MonkeyWorkshop
{

      /// <summary>
      /// A static class with methods for moving selected items in a ListView up or down.
      ///
      /// Works for single and multiple selections. Calls do not require any items to
      /// be selected. Repaints of the ListView are kept to a minimum.
      ///
      /// Algorithms find a non-selected item (movable) before any selected item
      /// before considering a movement (swap). This avoids "leapfrogging" that could
      /// occur by running through the selected idicies when they start to bunch
      /// at the top or bottom of the list.
      ///
      /// Client code should consider making their ListView.HideSelection = false
      /// or loss of focus (e.g. pressing a "Move Up" button) will conceal which
      /// items are selected - although the selection will remain.
      /// </summary>
      public static class ListViewSelectionMover
      {
            public static void MoveSelectedUp(ListView listView)
            {
                  int i = 0;
                  bool anyThingMoved = false;
                  bool movable = false;

                  int listLength = listView.Items.Count;

                  while (i < listLength)
                  {
                        movable = !(listView.SelectedIndices.Contains(i));

                        if (movable && (i + 1 < listLength))
                        {
                              if (listView.SelectedIndices.Contains(i + 1))
                              {
                                    if (anyThingMoved == false)
                                    {
                                          anyThingMoved = true;
                                          listView.BeginUpdate();
                                    }
                                    ListViewItem swap = listView.Items[i];
                                    listView.Items.RemoveAt(i);
                                    listView.Items.Insert(i + 1, swap);
                              }
                        }
                        i++;
                  }

                  if (anyThingMoved)
                  {
                        listView.EndUpdate();
                  }

                  return;
            }

            public static void MoveSelectedDown(ListView listView)
            {
                  int i = listView.Items.Count - 1;
                  bool anyThingMoved = false;
                  bool movable = false;

                  while (i >= 0)
                  {
                        movable = !(listView.SelectedIndices.Contains(i));

                        if (movable && (i - 1 >= 0))
                        {
                              if (listView.SelectedIndices.Contains(i - 1))
                              {
                                    if (anyThingMoved == false)
                                    {
                                          anyThingMoved = true;
                                          listView.BeginUpdate();
                                    }
                                    ListViewItem swap = listView.Items[i];
                                    listView.Items.RemoveAt(i);
                                    listView.Items.Insert(i - 1, swap);
                              }
                        }
                        i--;
                  }

                  if (anyThingMoved)
                  {
                        listView.EndUpdate();
                  }

                  return;
            }


      }   /// Class
}


======
Calling code would therefore need to use:

using MonkeyWorkshop;   // Or change the namespace in the above code

Call with:

   ListViewSelectionMover.MoveSelectedUp(yourListLiew);

Or clearly...
   ListViewSelectionMover.MoveSelectedDown(yourListLiew);

Rich
PS. Thanks for the input
QuestionDataGridviewRow - Make it invisible . Pin
unitecsoft5-May-09 5:11
unitecsoft5-May-09 5:11 
AnswerRe: DataGridviewRow - Make it invisible . Pin
ATCsharp5-May-09 5:15
ATCsharp5-May-09 5:15 
Questionimporting from excel Pin
Solly745-May-09 4:38
Solly745-May-09 4:38 
AnswerRe: importing from excel Pin
fly9045-May-09 5:11
fly9045-May-09 5:11 
GeneralRe: importing from excel Pin
Solly745-May-09 5:15
Solly745-May-09 5:15 
AnswerRe: importing from excel Pin
riced5-May-09 6:57
riced5-May-09 6:57 
GeneralRe: importing from excel Pin
Solly745-May-09 11:54
Solly745-May-09 11:54 
GeneralRe: importing from excel Pin
riced5-May-09 22:16
riced5-May-09 22:16 
QuestionCustom Classes in web services Pin
BASONJS5-May-09 4:07
BASONJS5-May-09 4:07 
QuestionAdding a WebReference using WSE 3.0 Pin
ATCsharp5-May-09 3:42
ATCsharp5-May-09 3:42 
Questioni got some xml questions Pin
Miroslav885-May-09 3:25
Miroslav885-May-09 3:25 
GeneralRe: i got some xml questions Pin
harold aptroot5-May-09 3:33
harold aptroot5-May-09 3:33 
GeneralRe: i got some xml questions Pin
Miroslav885-May-09 9:43
Miroslav885-May-09 9:43 
GeneralRe: i got some xml questions Pin
harold aptroot5-May-09 10:57
harold aptroot5-May-09 10:57 
AnswerRe: i got some xml questions Pin
led mike5-May-09 4:31
led mike5-May-09 4:31 
QuestionConvert Variant in VB to C# Pin
klaydze5-May-09 3:22
klaydze5-May-09 3:22 
AnswerRe: Convert Variant in VB to C# Pin
Le centriste5-May-09 5:19
Le centriste5-May-09 5:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.