473,725 Members | 2,067 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 3523
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
16467
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
8897
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
1984
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
4531
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
15619
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
1050
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
7432
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
2820
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
2851
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
8750
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,...
1
9168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9105
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...
1
6701
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
4509
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
4780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3218
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
2
2632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2154
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.