PLZ Help, copy checked listview items to another listview

  • Thread starter Thread starter Tim.Geiges
  • Start date Start date
T

Tim.Geiges

This seems like ti should be very simple but I am sort of a newb and
this one is killing me
I am just trying to copy all the items that are checked in listView2
over to listView1 when a button is clicked. can anyone give me an
example please?

Thank you for your time,

Tim
 
This seems like ti should be very simple but I am sort of a newb and
this one is killing me
I am just trying to copy all the items that are checked in listView2
over to listView1 when a button is clicked. can anyone give me an
example please?

Thank you for your time,

Tim

Hi Tim,

What you can do is loop through all the items, check the 'Checked' property,
and add to the next listview:

///
public void CopyChecked ( ListView sourceListView, ListView, destListView )
{
foreach ( ListViewItem item in sourceListView.Items )
if ( item.Checked )
destListView.Items.Add( item.Clone() as ListViewItem );
}
///
 
Tim,

/// <summary>
/// remvoe list of objects from src listbox(if they are in the src) and
add them to dest listbox
/// </summary>
/// <param name="items"></param>
/// <param name="src"></param>
/// <param name="dest"></param>
public static int MoveList(IList itemsToMove, ListBox src, ListBox dest)
{

int c = itemsToMove.Count;

if( c==0 ) return c;

Object[] items = new Object[c];
itemsToMove.CopyTo(items, 0);


foreach(Object item in items)
{
if(src.Items.Contains(item)) src.Items.Remove(item);
}

dest.Items.AddRange(items);

return c;
}

HTH
Ryan Liu
 
Back
Top