473,796 Members | 2,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

? bug in listbox.AddRang e : duplication

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

private void button1_Click(o bject sender, EventArgs e)
{
object[] bunchOfStuff = { "foo", "fee" };
listBox1.Items. Clear();
System.Windows. Forms.ListBox.O bjectCollection lboxObjCol = new
ListBox.ObjectC ollection(listB ox1, bunchOfStuff);
listBox1.Items. AddRange(lboxOb jCol);

}

--
Andrew
Feb 15 '06 #1
3 3530
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.co m

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

private void button1_Click(o bject sender, EventArgs e)
{
object[] bunchOfStuff = { "foo", "fee" };
listBox1.Items. Clear();
System.Windows. Forms.ListBox.O bjectCollection lboxObjCol = new
ListBox.ObjectC ollection(listB ox1, bunchOfStuff);
listBox1.Items. AddRange(lboxOb jCol);

}

--
Andrew

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

System.Windows. Forms.ListBox.O bjectCollection lboxObjCol = new
ListBox.ObjectC ollection(listB ox1, 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(o bject sender, EventArgs e)
{
object[] bunchOfStuff = { "foo", "fee" };
listBox1.Items. Clear();
System.Windows. Forms.ListBox.O bjectCollection lboxObjCol = new
ListBox.ObjectC ollection(listB ox1, bunchOfStuff);
listBox1.Items. AddRange(lboxOb jCol);

}

--
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.O bjectCollection lboxObjCol = new
ListBox.ObjectC ollection(listB ox1, 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(o bject sender, EventArgs e)
{
object[] bunchOfStuff = { "foo", "fee" };
listBox1.Items. Clear();
System.Windows. Forms.ListBox.O bjectCollection lboxObjCol = new
ListBox.ObjectC ollection(listB ox1, bunchOfStuff);
listBox1.Items. AddRange(lboxOb jCol);

}

--
Andrew

Feb 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
16470
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 the the bottom. Anybody knows how to be implemented? Thanks
3
8900
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 Me.ListBoxPerson.Items.AddRange(ary)
1
1989
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. He would now like different "text" and "value" properties for each item in the listbox. Given the number of items, what would be the most efficient way of doing this?
7
4539
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 ASP.Net stuff when I need a Windows Form control. I've seen dual listbox populators in countless Windows applications, and have seen them run very fast, so I figured this would be extremely popular. Here's how it should work:
2
15625
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 load a range : listBox1.Items.AddRange(lboxObjCol); so I try to load the lboxObjCol as shown: System.Windows.Forms.ListBox.ObjectCollection lboxObjCol = new ListBox.ObjectCollection(listBox1, (object)aLRandoms);
0
1053
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 version of the listbox handles this just fine. Does anyone know how to convert the FileInfo to ListItem for the
5
7436
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 visibility and/or presence of the Listbox and associated label, and I want to encapsulate some of associated code. I want to be able to pass the collection of strings that will fill the Listbox to the User Control and have it pass on the collection...
4
2823
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 ideas?? void mWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
5
2855
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 is what the ListBox uses for its display. Correct?
0
9684
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.