Windows Develop Bookmark and Share   
 index > Windows Forms General > Move Listbox1 Items to Listbox2
 

Move Listbox1 Items to Listbox2

I got a block or something in my mind and i can't get trough this...

Usualy its easy to simples move a certain(or more) item from a Listbox to other.

But i want to first check if the item allready is in that listbox....

THE PROBLEM is this lisboxes are use Datasources(are linked to a dataset)

So how can i get to this???

MikeC.pt  Tuesday, March 25, 2008 1:05 PM

Hi,Mick

No, it's not abug with theListBox.Items.Contains() method, it's because the items of the two ListBoxes are not the same, what you see is the text of the two items are the same, but they're belong to two different objects indeed.

For example, if the items are added by Items.Add() method, then the items in both ListBoxes are of string objects, the Contains() method works just fine in this scenario, as

Code Snippet

void Form7_Load(object sender, EventArgs e)

{

listBox1.Items.Add("aaa");

listBox1.Items.Add("bbb");

listBox1.Items.Add("ccc");

}

private void button1_Click(object sender, EventArgs e)

{

if (!this.listBox2.Items.Contains(this.listBox1.SelectedItem))

{

this.listBox2.Items.Add(this.listBox1.SelectedItem);

}

}



but, if the ListBoxes are bound to different tables, the items are different between the two ListBoxes, see the sample below.

Code Snippet

void Form7_Load(object sender, EventArgs e)

{

DataTable dt1 = new DataTable();

dt1.Columns.Add("c1", typeof(int));

dt1.Columns.Add("c2");

for (int j = 0; j < 10; j++)

{

dt1.Rows.Add(j, "aaa" + j.ToString());

}

this.listBox1.DisplayMember = "c2";

this.listBox1.ValueMember = "c1";

this.listBox1.DataSource = dt1;

DataTable dt2 = new DataTable();

dt2.Columns.Add("c1", typeof(int));

dt2.Columns.Add("c2");

dt2.Rows.Add(0, "aaa" + 0.ToString());

bs.DataSource = dt2;

this.listBox2.DisplayMember = "c2";

this.listBox2.ValueMember = "c1";

this.listBox2.DataSource = bs;

}

BindingSource bs = new BindingSource();

private void button1_Click(object sender, EventArgs e)

{

if(this.listBox2.Items.Contains(this.listBox1.SelectedItem))

{
//Always FALSE.
//the dataof the firstitem in the two ListBoxesyou see are seeminglythe same,
//but in factthey are different objects, they're just two different DataRowView objects
//with same data.
//If you compare the this.ListBox1.SelectedItem.GetHashCode()
//with the listBox2.Items[0].GetHashCode(), you'll find they're totally different.

}

}

Code Snippet

void Form7_Load(object sender, EventArgs e)

{

DataTable dt1 = new DataTable();

dt1.Columns.Add("c1", typeof(int));

dt1.Columns.Add("c2");

for (int j = 0; j < 10; j++)

{

dt1.Rows.Add(j, "aaa" + j.ToString());

}

this.listBox1.DisplayMember = "c2";

this.listBox1.ValueMember = "c1";

this.listBox1.DataSource = dt1;

DataTable dt2 = new DataTable();

dt2.Columns.Add("c1", typeof(int));

dt2.Columns.Add("c2");

dt2.Rows.Add(0, "aaa" + 0.ToString());

bs.DataSource = dt2;

this.listBox2.DisplayMember = "c2";

this.listBox2.ValueMember = "c1";

this.listBox2.DataSource = bs;

}

BindingSource bs = new BindingSource();

private void button1_Click(object sender, EventArgs e)

{

DataRowView drv = this.listBox1.SelectedItem as DataRowView;

if (bs.Find("c2", drv["c2"]) < 0)

{

DataTable dt = bs.DataSource as DataTable;

DataRow row = dt.NewRow();

row.ItemArray = drv.Row.ItemArray;

dt.Rows.Add(row);

}

}



Best Regards
Zhi-xin Ye.

Zhi-Xin Ye  Friday, March 28, 2008 4:26 AM
Any help?? :X

MikeC.pt  Wednesday, March 26, 2008 9:45 AM

foreach (object o in listBox1.SelectedItems)

{

if(!yourdatasource.tablesOrWhatever[yourtable].rows.contains(o))

yourdatasource.tablesOrWhatever[yourtable].rows.add(o);

}

listBox2.refresh();

or something like that

DinoMulahusic   Wednesday, March 26, 2008 11:49 AM

That won't do it.

The ideia is to move items (the same as if it was a normal binding) but what i've noticed is that if i want to use a data source(linked to a dataset) and a table adapter.. i won't be able to do things easy by code....

Anyway i've change things.....

No i have a SqlDataReader to 'fill' the first listbox and the items are recognized.

I had a problem with the refresh of the secound as i was allready counting... but its just a matter of Fill() the dataset again for that table.

Now... i sitll can't check if the destination listbox already as the item...

Shouldn't it work with Listbox.Items.Contains() ??????

MikeC.pt  Wednesday, March 26, 2008 3:41 PM

If found something here that might do it...

for exemple

Code Snippet

DataRowView row = (DataRowView)BindingSource.Current;

string Anystring = row["ColumnName"].ToString();

The BindingSource (is the binding source of the destination list)

That would save the current value of the column (Columnname) in other words the value i'm no Positined....

Is there anyway to get ALL values??? i could then check if that collection of values contains any item in the collection of the Listbox

See the point??
MikeC.pt  Wednesday, March 26, 2008 3:48 PM

You can use the foreach loop, as

(DataRowView drv in bs.List)

thanks for the help but like i said ForEach loops won't work...

when you do the check using Contains metod it will simples say they are allways different... this is definetly a bug of visualstudio or something missing in the framework...

This doesn't work if your using BindingSources to fill the listboxes why??? dunno Smile

I've simple changed it all and i'm filling the listboxes usind sqlComands

Still if i use the listbox.Items.Contains(listbox2.SelectedItems) i got no reaction Tongue Tied and the STRINGS are TOTALY EQUAL Tongue Tied

MikeC.pt  Thursday, March 27, 2008 3:52 PM

Hi,Mick

No, it's not abug with theListBox.Items.Contains() method, it's because the items of the two ListBoxes are not the same, what you see is the text of the two items are the same, but they're belong to two different objects indeed.

For example, if the items are added by Items.Add() method, then the items in both ListBoxes are of string objects, the Contains() method works just fine in this scenario, as

Code Snippet

void Form7_Load(object sender, EventArgs e)

{

listBox1.Items.Add("aaa");

listBox1.Items.Add("bbb");

listBox1.Items.Add("ccc");

}

private void button1_Click(object sender, EventArgs e)

{

if (!this.listBox2.Items.Contains(this.listBox1.SelectedItem))

{

this.listBox2.Items.Add(this.listBox1.SelectedItem);

}

}



but, if the ListBoxes are bound to different tables, the items are different between the two ListBoxes, see the sample below.

Code Snippet

void Form7_Load(object sender, EventArgs e)

{

DataTable dt1 = new DataTable();

dt1.Columns.Add("c1", typeof(int));

dt1.Columns.Add("c2");

for (int j = 0; j < 10; j++)

{

dt1.Rows.Add(j, "aaa" + j.ToString());

}

this.listBox1.DisplayMember = "c2";

this.listBox1.ValueMember = "c1";

this.listBox1.DataSource = dt1;

DataTable dt2 = new DataTable();

dt2.Columns.Add("c1", typeof(int));

dt2.Columns.Add("c2");

dt2.Rows.Add(0, "aaa" + 0.ToString());

bs.DataSource = dt2;

this.listBox2.DisplayMember = "c2";

this.listBox2.ValueMember = "c1";

this.listBox2.DataSource = bs;

}

BindingSource bs = new BindingSource();

private void button1_Click(object sender, EventArgs e)

{

if(this.listBox2.Items.Contains(this.listBox1.SelectedItem))

{
//Always FALSE.
//the dataof the firstitem in the two ListBoxesyou see are seeminglythe same,
//but in factthey are different objects, they're just two different DataRowView objects
//with same data.
//If you compare the this.ListBox1.SelectedItem.GetHashCode()
//with the listBox2.Items[0].GetHashCode(), you'll find they're totally different.

}

}

Code Snippet

void Form7_Load(object sender, EventArgs e)

{

DataTable dt1 = new DataTable();

dt1.Columns.Add("c1", typeof(int));

dt1.Columns.Add("c2");

for (int j = 0; j < 10; j++)

{

dt1.Rows.Add(j, "aaa" + j.ToString());

}

this.listBox1.DisplayMember = "c2";

this.listBox1.ValueMember = "c1";

this.listBox1.DataSource = dt1;

DataTable dt2 = new DataTable();

dt2.Columns.Add("c1", typeof(int));

dt2.Columns.Add("c2");

dt2.Rows.Add(0, "aaa" + 0.ToString());

bs.DataSource = dt2;

this.listBox2.DisplayMember = "c2";

this.listBox2.ValueMember = "c1";

this.listBox2.DataSource = bs;

}

BindingSource bs = new BindingSource();

private void button1_Click(object sender, EventArgs e)

{

DataRowView drv = this.listBox1.SelectedItem as DataRowView;

if (bs.Find("c2", drv["c2"]) < 0)

{

DataTable dt = bs.DataSource as DataTable;

DataRow row = dt.NewRow();

row.ItemArray = drv.Row.ItemArray;

dt.Rows.Add(row);

}

}



Best Regards
Zhi-xin Ye.

Zhi-Xin Ye  Friday, March 28, 2008 4:26 AM

I knew it was something about the bound Stick out tongue

Thanks m8 big help Wink

MikeC.pt  Tuesday, April 08, 2008 1:13 PM

You can use google to search for other answers

Custom Search

More Threads

• Webbrowser.DocumentText Need Help
• Automatic Update can't install any updates
• FileDialog
• app.config
• can any one help me
• How do I animate a button click from code?
• MDI Memory Leak
• C# ArrayList to capture selected items in a listbox
• Printing issue
• Visual Studio 2005 - AssemblyInfo in multi-language