471,319 Members | 3,253 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

? bug in listbox.AddRange : duplication

Who can explain this ?? : I expect 2 but I get 4 items !

private void button1_Click(object sender, EventArgs e)
{
object[] bunchOfStuff = { "foo", "fee" };
listBox1.Items.Clear();
System.Windows.Forms.ListBox.ObjectCollection lboxObjCol = new
ListBox.ObjectCollection(listBox1, bunchOfStuff);
listBox1.Items.AddRange(lboxObjCol);

}

--
Andrew
Feb 15 '06 #1
3 3444
Andrew,

Well, you are adding the objects twice. The first time is in the
constructor, where you pass bunchOfStuff, the second is when you call
AddRange, passing the array again, resulting in four items.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"andrewcw" <an******@acw.com> wrote in message
news:05**********************************@microsof t.com...
Who can explain this ?? : I expect 2 but I get 4 items !

private void button1_Click(object sender, EventArgs e)
{
object[] bunchOfStuff = { "foo", "fee" };
listBox1.Items.Clear();
System.Windows.Forms.ListBox.ObjectCollection lboxObjCol = new
ListBox.ObjectCollection(listBox1, bunchOfStuff);
listBox1.Items.AddRange(lboxObjCol);

}

--
Andrew

Feb 15 '06 #2
Andrew,
This isn't a bug. This line:

System.Windows.Forms.ListBox.ObjectCollection lboxObjCol = new
ListBox.ObjectCollection(listBox1, bunchOfStuff);

actually adds your 2 items to the Listbox. So the next line simply does it
again.

Comment out the last line and you will have what you want.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"andrewcw" wrote:
Who can explain this ?? : I expect 2 but I get 4 items !

private void button1_Click(object sender, EventArgs e)
{
object[] bunchOfStuff = { "foo", "fee" };
listBox1.Items.Clear();
System.Windows.Forms.ListBox.ObjectCollection lboxObjCol = new
ListBox.ObjectCollection(listBox1, bunchOfStuff);
listBox1.Items.AddRange(lboxObjCol);

}

--
Andrew

Feb 15 '06 #3
Thanks - I can see now why it is loaded 2X thanks to you and Nicholas for
setting me straight :)
--
Andrew
"Peter Bromberg [C# MVP]" wrote:
Andrew,
This isn't a bug. This line:

System.Windows.Forms.ListBox.ObjectCollection lboxObjCol = new
ListBox.ObjectCollection(listBox1, bunchOfStuff);

actually adds your 2 items to the Listbox. So the next line simply does it
again.

Comment out the last line and you will have what you want.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"andrewcw" wrote:
Who can explain this ?? : I expect 2 but I get 4 items !

private void button1_Click(object sender, EventArgs e)
{
object[] bunchOfStuff = { "foo", "fee" };
listBox1.Items.Clear();
System.Windows.Forms.ListBox.ObjectCollection lboxObjCol = new
ListBox.ObjectCollection(listBox1, bunchOfStuff);
listBox1.Items.AddRange(lboxObjCol);

}

--
Andrew

Feb 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by George | last post: by
1 post views Thread by Richard Bond | last post: by
7 posts views Thread by Dave | last post: by
reply views Thread by GentOfTheLake | last post: by
5 posts views Thread by markr1000 | last post: by
4 posts views Thread by Rotsey | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.