How to copy selected items from one list to another when a button is clicked in C # net?

How can I copy selected items from one list to another when I click .. ?? without any redundancy can I also provide the opportunity for multiple selection of elements and adding them to the volume without using ctrl from the keyboard? what makes it convenient for the user, can we use the checkboxes and how will they work? The code below is used to copy records for a single item selection, and also gives duplicate records when you reselect this item ... please help me fix the flaws ...

private void btn_Add_Click(object sender, EventArgs e)        
{        
    CopySelectedItems(source_name, target_name);     
}

private void CopySelectedItems(ListView source, ListView target) 
{        
    foreach (ListViewItem item in source.SelectedItems) {
        target.Items.Add((ListViewItem)item.Clone());
    }
}
+3
source share
4

.

a b:

private static void CopySelectedItems(ListView source, ListView target)
{
    foreach (ListViewItem item in source.SelectedItems)
    {
        target.Items.Add((ListViewItem)item.Clone());
    }
}

a b:

private static void MoveSelectedItems(ListView source, ListView target)
{    
    while (source.SelectedItems.Count > 0)
    {
        ListViewItem temp = source.SelectedItems[0];
        source.Items.Remove(temp);
        target.Items.Add(temp);
    }            
}


, , ListView. , - ? , , , , ListView ( :

private static void CopySelectedItems(ListView source, ListView target)
{
    foreach (ListViewItem item in source.SelectedItems)
    {
        ListViewItem clone = (ListViewItem)item.Clone();
        target.Items.Insert(GetInsertPosition(clone, target), clone); ;
    }
}

private static int GetInsertPosition(ListViewItem item, ListView target)
{
    const int compareColumn = 1;
    foreach (ListViewItem targetItem in target.Items)
    {
        if (targetItem.SubItems[compareColumn].Text.CompareTo(item.SubItems[compareColumn].Text) > 0)
        {
            return targetItem.Index;
        }
    }
    return target.Items.Count;
}

, .

+5

SelectedItems ListView ListView.

:

foreach(var item in lst1.SelectedItems)
{
    var lvi = lst2.Items.Add(item.Text);
    lvi.ImageIndex = item.ImageIndex;
    ...
}
0

, , View , , View?

, , , .

0

. - :

            var insertPos = 0;
            foreach ( ListViewItem s in sourceList.SelectedItems )
            {
                s.Remove ( );
                var copyCode = Int32.Parse ( s.Text );
                while ( insertPos < destinationList.Items.Count )
                {
                    var itemAtCandidate = Int32.Parse ( destinationList.Items [ insertPos ].Text );
                    if ( itemAtCandidate > copyCode )
                        break;
                    insertPos++;
                }
                destinationList.Items.Insert ( insertPos, s );
            }

"sourceList" "destinationList" .

0

Source: https://habr.com/ru/post/1734521/


All Articles