Sync list to list?

I have a list on my WinForms where users can move items up and down, and this list also matches the list I have, and I was wondering what would be the most efficient way to keep synchronized.

For example, to move an item down, I:

int i = this.recoveryList.SelectedIndex;
object o = this.recoveryList.SelectedItem;

if (i < recoveryList.Items.Count - 1)
{
  this.recoveryList.Items.RemoveAt(i);
  this.recoveryList.Items.Insert(i + 1, o);
  this.recoveryList.SelectedIndex = i + 1;
}

And I have:

public List<RouteList> Recovery = new List<RouteList>();

Which I would like to update in the list.

Should I just clear the restore and update with the current list data, or is there a better way to update them when moving up and down?

I basically ask because the types from list to list are different.

+3
source share
2 answers

.Net . , :

public BindingList<RouteList> Recovery = new BindingList<RouteList>();

BindingList :

listBox1.DataSource = Recovery;

BindingList . listBox , , :

public partial class Form1 : Form
{
    private readonly BindingList<string> list = new BindingList<string> { "apple", "pear", "grape", "taco", "screwdriver" };

    public Form1()
    {
        InitializeComponent();
        listBox1.DataSource = list;
        listBox2.DataSource = list;
    }

    private void listBox1_KeyUp(object sender, KeyEventArgs e)
    {
        var tmp = list[0];
        list[0] = list[listBox1.SelectedIndex];
        list[listBox1.SelectedIndex] = tmp;
    }
}
+2

- , .

ListBox ( ), ObservableCollection. INotifyPropertyChanged .

/, , .

, TOP . , . List, ObservableCollection. ( ) , .

, , Google. , , .

+2

All Articles