473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 3505
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: GG | last post by:
I know I can scroll to the last item in the listbox by calling SetSelected all the time What I need is when I add items through the Items.Add or Items.AddRange to be called automatically and go...
3
by: George | last post by:
Sub ExcelToListBox() Dim xRange As Object Dim ary Dim xValue As String xRange = oXL.Range("A1:A9") 'has letters A-H ary = xRange.value xValue = ary(3, 1) 'xValue = C...
1
by: Richard Bond | last post by:
Hi, A colleague of mine has added a 60,000 array of strings to a listbox using the addrange method. He did this because it seemed the quickest method of getting 60,000 items into the listbox. ...
7
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've looked for over a week now and can't find anything but...
2
by: andrewcw | last post by:
This is wrong but it seems I might be on track somewhat close: I want to display my ArrayList of Random numbers in the list box. Below you see this as aLRandoms. There seems to be a provision to...
0
by: GentOfTheLake | last post by:
Why won't the webcontrols version of the listbox work with this: DirectoryInfo di = new DirectoryInfo(); FileInfo fi = di.GetFiles(); ListBox lb = new ListBox(); lb.AddRange(fi); The forms...
5
by: markr1000 | last post by:
I must have looked searched in 500+ places that showed up in Google searchs, but not one has an example of what I want to do. I have a Listbox on a User Control because I want to control the...
4
by: Rotsey | last post by:
Hi, I have a listbox and I am adding objects to it with AddRange. The problem is the listbox just shows the name of object class in every row and not the DisplayMember see below. Any...
5
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.