473,804 Members | 1,974 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 3532
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
16471
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
8903
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
1991
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
4540
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
15627
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
1054
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
2857
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
9716
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
10354
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
10101
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
9177
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...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6870
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
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.