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?
c# .net winforms
add a comment |
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?
c# .net winforms
have you tried using a for loop? when moving upwards letirun from0toCountand when moving down, let it run backwards
– Mong Zhu
Nov 20 at 7:37
add a comment |
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?
c# .net winforms
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
c# .net winforms
asked Nov 20 at 7:12
Jonas Kohl
184111
184111
have you tried using a for loop? when moving upwards letirun from0toCountand when moving down, let it run backwards
– Mong Zhu
Nov 20 at 7:37
add a comment |
have you tried using a for loop? when moving upwards letirun from0toCountand 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
add a comment |
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.
add a comment |
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;
}
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 20 at 7:35
heap1
598
598
add a comment |
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 20 at 9:03
Mong Zhu
15.1k52348
15.1k52348
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
have you tried using a for loop? when moving upwards let
irun from0toCountand when moving down, let it run backwards– Mong Zhu
Nov 20 at 7:37