PLZ Help, copy checked listview items to another listview

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
 
T

Tom Spink

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 );
}
///
 
R

Ryan Liu

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top