Moving all selected items in a ListBox with DataSource











up vote
0
down vote

favorite












I have a ListBox which has an ObservableCollection<string> 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 prefixed with *):



  Item 1
* Item 2
Item 3
* Item 4
* Item 5
* Item 6
Item 7


I want it to look like this after moving down once:



  Item 1
Item 3
* Item 2
Item 7
* Item 4
* Item 5
* Item 6


or this after moving up once:



* Item 2
Item 1
* Item 4
* Item 5
* Item 6
Item 3
Item 7


I already stumbled across ObservableCollection<T>.Move(int oldIndex, int newIndex) but I only can get it to work with moving a single item.



What would be a good algorithm?










share|improve this question






















  • have you tried using a for loop? when moving upwards let i run from 0 to Count and when moving down, let it run backwards
    – Mong Zhu
    Nov 20 at 7:37















up vote
0
down vote

favorite












I have a ListBox which has an ObservableCollection<string> 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 prefixed with *):



  Item 1
* Item 2
Item 3
* Item 4
* Item 5
* Item 6
Item 7


I want it to look like this after moving down once:



  Item 1
Item 3
* Item 2
Item 7
* Item 4
* Item 5
* Item 6


or this after moving up once:



* Item 2
Item 1
* Item 4
* Item 5
* Item 6
Item 3
Item 7


I already stumbled across ObservableCollection<T>.Move(int oldIndex, int newIndex) but I only can get it to work with moving a single item.



What would be a good algorithm?










share|improve this question






















  • have you tried using a for loop? when moving upwards let i run from 0 to Count and when moving down, let it run backwards
    – Mong Zhu
    Nov 20 at 7:37













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a ListBox which has an ObservableCollection<string> 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 prefixed with *):



  Item 1
* Item 2
Item 3
* Item 4
* Item 5
* Item 6
Item 7


I want it to look like this after moving down once:



  Item 1
Item 3
* Item 2
Item 7
* Item 4
* Item 5
* Item 6


or this after moving up once:



* Item 2
Item 1
* Item 4
* Item 5
* Item 6
Item 3
Item 7


I already stumbled across ObservableCollection<T>.Move(int oldIndex, int newIndex) but I only can get it to work with moving a single item.



What would be a good algorithm?










share|improve this question













I have a ListBox which has an ObservableCollection<string> 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 prefixed with *):



  Item 1
* Item 2
Item 3
* Item 4
* Item 5
* Item 6
Item 7


I want it to look like this after moving down once:



  Item 1
Item 3
* Item 2
Item 7
* Item 4
* Item 5
* Item 6


or this after moving up once:



* Item 2
Item 1
* Item 4
* Item 5
* Item 6
Item 3
Item 7


I already stumbled across ObservableCollection<T>.Move(int oldIndex, int newIndex) but I only can get it to work with moving a single item.



What would be a good algorithm?







c# .net winforms






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 7:12









Jonas Kohl

184111




184111












  • have you tried using a for loop? when moving upwards let i run from 0 to Count and when moving down, let it run backwards
    – Mong Zhu
    Nov 20 at 7:37


















  • have you tried using a for loop? when moving upwards let i run from 0 to Count and when moving down, let it run backwards
    – Mong Zhu
    Nov 20 at 7:37
















have you tried using a for loop? when moving upwards let i run from 0 to Count and when moving down, let it run backwards
– Mong Zhu
Nov 20 at 7:37




have you tried using a for loop? when moving upwards let i run from 0 to Count and when moving down, let it run backwards
– Mong Zhu
Nov 20 at 7:37












2 Answers
2






active

oldest

votes

















up vote
0
down vote













It would be easier (and in some cases more intuitive to the user) if all selected lines drop with no gaps between them, but it should be possible to implement either way. You can absolutely use the Move() method of ObservableCollection, to move multiple you will have to gather multiple startindexes (for each selected item) into a list and then iterate through that list using the Move() method as well as making some calculations based on what index position the cursor is on and the sorted order of your selected items list to determine what the 'newindex' is for each item.



Edit: also keep in mind the domino effect of changing indexes that you will have to accomodate for in your calculations.






share|improve this answer




























    up vote
    0
    down vote













    As already mention in my comment. You could use a forward and backward loop to accomplish that. Here is a sample program that does the job. I made to buttons to distinguish the UP and DOWN movement:



    ObservableCollection<string> source = new ObservableCollection<string>();
    private void Form1_Load(object sender, EventArgs e)
    {
    for (int i = 1; i < 10; i++)
    {
    source.Add("Item " + i);
    }

    listBox1.DataSource = source;
    }

    private void buttonMoveUp_Click(object sender, EventArgs e)
    {
    foreach (int index in listBox1.SelectedIndices)
    {
    if (index > 0) // don't move the first element upwards
    {
    source.Move(index, index - 1);
    }
    }

    listBox1.DataSource = null;
    listBox1.DataSource = source;
    }

    private void buttonMoveDown_Click(object sender, EventArgs e)
    {
    for (int i = listBox1.SelectedIndices.Count - 1; i >= 0; i--)
    {
    int index = listBox1.SelectedIndices[i];
    if (index < source.Count-1) // don't move the last element downwards
    {
    source.Move(index, index + 1);
    }
    }

    listBox1.DataSource = null;
    listBox1.DataSource = source;
    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387954%2fmoving-all-selected-items-in-a-listbox-with-datasource%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      It would be easier (and in some cases more intuitive to the user) if all selected lines drop with no gaps between them, but it should be possible to implement either way. You can absolutely use the Move() method of ObservableCollection, to move multiple you will have to gather multiple startindexes (for each selected item) into a list and then iterate through that list using the Move() method as well as making some calculations based on what index position the cursor is on and the sorted order of your selected items list to determine what the 'newindex' is for each item.



      Edit: also keep in mind the domino effect of changing indexes that you will have to accomodate for in your calculations.






      share|improve this answer

























        up vote
        0
        down vote













        It would be easier (and in some cases more intuitive to the user) if all selected lines drop with no gaps between them, but it should be possible to implement either way. You can absolutely use the Move() method of ObservableCollection, to move multiple you will have to gather multiple startindexes (for each selected item) into a list and then iterate through that list using the Move() method as well as making some calculations based on what index position the cursor is on and the sorted order of your selected items list to determine what the 'newindex' is for each item.



        Edit: also keep in mind the domino effect of changing indexes that you will have to accomodate for in your calculations.






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          It would be easier (and in some cases more intuitive to the user) if all selected lines drop with no gaps between them, but it should be possible to implement either way. You can absolutely use the Move() method of ObservableCollection, to move multiple you will have to gather multiple startindexes (for each selected item) into a list and then iterate through that list using the Move() method as well as making some calculations based on what index position the cursor is on and the sorted order of your selected items list to determine what the 'newindex' is for each item.



          Edit: also keep in mind the domino effect of changing indexes that you will have to accomodate for in your calculations.






          share|improve this answer












          It would be easier (and in some cases more intuitive to the user) if all selected lines drop with no gaps between them, but it should be possible to implement either way. You can absolutely use the Move() method of ObservableCollection, to move multiple you will have to gather multiple startindexes (for each selected item) into a list and then iterate through that list using the Move() method as well as making some calculations based on what index position the cursor is on and the sorted order of your selected items list to determine what the 'newindex' is for each item.



          Edit: also keep in mind the domino effect of changing indexes that you will have to accomodate for in your calculations.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 7:35









          heap1

          598




          598
























              up vote
              0
              down vote













              As already mention in my comment. You could use a forward and backward loop to accomplish that. Here is a sample program that does the job. I made to buttons to distinguish the UP and DOWN movement:



              ObservableCollection<string> source = new ObservableCollection<string>();
              private void Form1_Load(object sender, EventArgs e)
              {
              for (int i = 1; i < 10; i++)
              {
              source.Add("Item " + i);
              }

              listBox1.DataSource = source;
              }

              private void buttonMoveUp_Click(object sender, EventArgs e)
              {
              foreach (int index in listBox1.SelectedIndices)
              {
              if (index > 0) // don't move the first element upwards
              {
              source.Move(index, index - 1);
              }
              }

              listBox1.DataSource = null;
              listBox1.DataSource = source;
              }

              private void buttonMoveDown_Click(object sender, EventArgs e)
              {
              for (int i = listBox1.SelectedIndices.Count - 1; i >= 0; i--)
              {
              int index = listBox1.SelectedIndices[i];
              if (index < source.Count-1) // don't move the last element downwards
              {
              source.Move(index, index + 1);
              }
              }

              listBox1.DataSource = null;
              listBox1.DataSource = source;
              }





              share|improve this answer

























                up vote
                0
                down vote













                As already mention in my comment. You could use a forward and backward loop to accomplish that. Here is a sample program that does the job. I made to buttons to distinguish the UP and DOWN movement:



                ObservableCollection<string> source = new ObservableCollection<string>();
                private void Form1_Load(object sender, EventArgs e)
                {
                for (int i = 1; i < 10; i++)
                {
                source.Add("Item " + i);
                }

                listBox1.DataSource = source;
                }

                private void buttonMoveUp_Click(object sender, EventArgs e)
                {
                foreach (int index in listBox1.SelectedIndices)
                {
                if (index > 0) // don't move the first element upwards
                {
                source.Move(index, index - 1);
                }
                }

                listBox1.DataSource = null;
                listBox1.DataSource = source;
                }

                private void buttonMoveDown_Click(object sender, EventArgs e)
                {
                for (int i = listBox1.SelectedIndices.Count - 1; i >= 0; i--)
                {
                int index = listBox1.SelectedIndices[i];
                if (index < source.Count-1) // don't move the last element downwards
                {
                source.Move(index, index + 1);
                }
                }

                listBox1.DataSource = null;
                listBox1.DataSource = source;
                }





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  As already mention in my comment. You could use a forward and backward loop to accomplish that. Here is a sample program that does the job. I made to buttons to distinguish the UP and DOWN movement:



                  ObservableCollection<string> source = new ObservableCollection<string>();
                  private void Form1_Load(object sender, EventArgs e)
                  {
                  for (int i = 1; i < 10; i++)
                  {
                  source.Add("Item " + i);
                  }

                  listBox1.DataSource = source;
                  }

                  private void buttonMoveUp_Click(object sender, EventArgs e)
                  {
                  foreach (int index in listBox1.SelectedIndices)
                  {
                  if (index > 0) // don't move the first element upwards
                  {
                  source.Move(index, index - 1);
                  }
                  }

                  listBox1.DataSource = null;
                  listBox1.DataSource = source;
                  }

                  private void buttonMoveDown_Click(object sender, EventArgs e)
                  {
                  for (int i = listBox1.SelectedIndices.Count - 1; i >= 0; i--)
                  {
                  int index = listBox1.SelectedIndices[i];
                  if (index < source.Count-1) // don't move the last element downwards
                  {
                  source.Move(index, index + 1);
                  }
                  }

                  listBox1.DataSource = null;
                  listBox1.DataSource = source;
                  }





                  share|improve this answer












                  As already mention in my comment. You could use a forward and backward loop to accomplish that. Here is a sample program that does the job. I made to buttons to distinguish the UP and DOWN movement:



                  ObservableCollection<string> source = new ObservableCollection<string>();
                  private void Form1_Load(object sender, EventArgs e)
                  {
                  for (int i = 1; i < 10; i++)
                  {
                  source.Add("Item " + i);
                  }

                  listBox1.DataSource = source;
                  }

                  private void buttonMoveUp_Click(object sender, EventArgs e)
                  {
                  foreach (int index in listBox1.SelectedIndices)
                  {
                  if (index > 0) // don't move the first element upwards
                  {
                  source.Move(index, index - 1);
                  }
                  }

                  listBox1.DataSource = null;
                  listBox1.DataSource = source;
                  }

                  private void buttonMoveDown_Click(object sender, EventArgs e)
                  {
                  for (int i = listBox1.SelectedIndices.Count - 1; i >= 0; i--)
                  {
                  int index = listBox1.SelectedIndices[i];
                  if (index < source.Count-1) // don't move the last element downwards
                  {
                  source.Move(index, index + 1);
                  }
                  }

                  listBox1.DataSource = null;
                  listBox1.DataSource = source;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 9:03









                  Mong Zhu

                  15.1k52348




                  15.1k52348






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387954%2fmoving-all-selected-items-in-a-listbox-with-datasource%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Feedback on college project

                      Futebolista

                      Albești (Vaslui)