473,795 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error with ListBox and ArrayList when Deleting Last Entry

My code works perfectly 100% when adding items to my ArrayList and updating the listbox.
Works perfectly when deleting an item in the ArrayList when it is not the last entry but if it is
the last entry and you select another index it goes boom!!!.

Funny thing is the SelectedIndex Changed routine fires off perfectly and then crash after the return
from this code.

Now looking around the news groups it appears to be a bug in the .net implementation.

The only way I have found to get around it is to select the previous item before removing the last
item. I do this by setting the SelectedIndex to what it is -1. Now it works perfectly until there is
nothing left in the list to remove and I will assume it will go boom again then.

Does anyone know of another work around for this bug?

Also is it fixed in VS 2005?

Cheers
Nov 21 '05 #1
5 2435
John,

The most made mistake with the listbox and combobox is that people forget
that when the index of that is changed, than direct the selectedindex change
is fired.

Can that be in this case as well the situation?

Cor
Nov 21 '05 #2
"John Veldthuis" <jo***@no.spamo .paradise.net.n z> schrieb:
My code works perfectly 100% when adding items to my ArrayList and
updating the listbox.
Works perfectly when deleting an item in the ArrayList when it is not the
last entry but if it is
the last entry and you select another index it goes boom!!!.

Funny thing is the SelectedIndex Changed routine fires off perfectly and
then crash after the return
from this code.


How are you binding the items of the arraylist to the listbox control? How
do you update the listbox control? I suggest to post some code.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
On Fri, 21 Oct 2005 12:53:43 +0200, "Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote:
"John Veldthuis" <jo***@no.spamo .paradise.net.n z> schrieb:
My code works perfectly 100% when adding items to my ArrayList and
updating the listbox.
Works perfectly when deleting an item in the ArrayList when it is not the
last entry but if it is
the last entry and you select another index it goes boom!!!.

Funny thing is the SelectedIndex Changed routine fires off perfectly and
then crash after the return
from this code.


How are you binding the items of the arraylist to the listbox control? How
do you update the listbox control? I suggest to post some code.


dim ch1recipes as clsRecipes

file in the array here by loading from an SQL database.
for the listbox to display I simply set the datasource

lbRecipes.DataS ource = ch1recipes

The class for ch1recipes has an override on the ToString method to display a full name. Everything
working fine to here.

Next remove the item if the user selects it and clicks the delete button.

lbRecipe.DataSo urce = Nothing
ch1recipes.Remo ve(lbRecipe.Sel ectedIndex) ' This removes the data from the ArrayList

' Remove the data from the SQL Database here

lbRecipe.DataSo urce = ch1recipes ' Needed to remove and add so that display is updated properly

Now everything works fine unless the item selected in the listbox is the last one. All the code
works fine. The item is removed properly from the arraylist and removed from the SQL database.
If it is the last one then as soon as the SelectedIndex method exits the subroutine it crashes. It
does not crash in user code so cannot be de-bugged.

Searching through the Google News I found several messages about exactly the same thing around 2002
and it was identified as a bug and reproducable 100% of the time. There was one suggetion as a work
around and that was to select the previous item in the listbox via code before removing the last
item in the arraylist. I tried this and it does cure the problem but is only a messy work around.

Nov 21 '05 #4
"John Veldthuis" <jo***@no.spamo .paradise.net.n z> schrieb:
My code works perfectly 100% when adding items to my ArrayList and
updating the listbox.
Works perfectly when deleting an item in the ArrayList when it is not
the
last entry but if it is
the last entry and you select another index it goes boom!!!.

Funny thing is the SelectedIndex Changed routine fires off perfectly and
then crash after the return
from this code.


How are you binding the items of the arraylist to the listbox control?
How
do you update the listbox control? I suggest to post some code.


dim ch1recipes as clsRecipes

file in the array here by loading from an SQL database.
for the listbox to display I simply set the datasource

lbRecipes.DataS ource = ch1recipes

The class for ch1recipes has an override on the ToString method to display
a full name. Everything
working fine to here.

Next remove the item if the user selects it and clicks the delete button.

lbRecipe.DataSo urce = Nothing
ch1recipes.Remo ve(lbRecipe.Sel ectedIndex) ' This removes the data from the
ArrayList

' Remove the data from the SQL Database here

lbRecipe.DataSo urce = ch1recipes ' Needed to remove and add so that
display is updated properly


You do not need to disconnect the arraylist from the listbox. Use the code
below instead to update the listbox control:

\\\
Private Sub Form1_Load( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles MyBase.Load
m_Data.Add(New Foo("Bla", 12))
Me.ListBox1.Dis playMember = "Name"
Me.ListBox1.Dat aSource = m_Data
End Sub

Private Sub Button1_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Button1.Click
m_Data.Add(New Foo("Goo", 13))
DirectCast(m_Da ta(0), Foo).Name = "Baz"

' Force update.
DirectCast( _
Me.ListBox1.Bin dingContext(Me. ListBox1.DataSo urce), _
CurrencyManager _
).Refresh()
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
On Sat, 22 Oct 2005 13:08:14 +0200, "Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote:
lbRecipe.DataSo urce = ch1recipes ' Needed to remove and add so that
display is updated properly
You do not need to disconnect the arraylist from the listbox. Use the code
below instead to update the listbox control:

' Force update.
DirectCast( _
Me.ListBox1.Bin dingContext(Me. ListBox1.DataSo urce), _
CurrencyManager _
).Refresh()
End Sub
///


Thanks. I looked for something like this but could not see it. BindingContext does not show up under
intellisense either but it works.
Nov 21 '05 #6

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

Similar topics

5
10626
by: Andrew | last post by:
Hi I just started learning wxPython I wanted to know how I could do this in wxPython self.listbox.delete(0, END) for item in self.results: self.listbox.insert(END, item)
3
1894
by: CJM | last post by:
I would like to be able to use an autocompleting listbox - that is, one where you can select an item in the list by typing the first few characters; the standard IE listbox only responds to the first character. I'm happy to pay a few quid for a 3rd-party component/activex control if necessary but I imagine something equally good could be done with client-side code... Any suggestions?
2
2360
by: Joshua Ammann | last post by:
How do you change what row is highlighted in a listbox (so that it highlights the current record you are viewing in the form) when you use the built-in navagation buttons (or any method other than the listbox itself) to move between records in a form? I have a data entry form (single form) with a list box that moves the form to whatever record you click on in the list box. You can also change the current record by the navagation buttons,...
0
1176
by: Vipin Kedia | last post by:
Hi I have written a code for showing the list boxes as selected using a Listitem and the selected property of the items. Now I have 2 list boxes in my page. But it shows only the selected values of the last list box in both the list boxes. If i reverse the calls to the filllistbox methods it shows the value selected for the 1st list box in both the listboxes. If I run application in debug it shows proper values being selected. But...
1
2167
by: Josema | last post by:
Hi, My problem is the next one (in a windows application): - I have a class derived from collectionbase to fill with persons object (id, name) from database. - I have a ListBox wich datasource its the collectionbase above. I want to do: - I would like that when i click in a item of the listbox, deletes from database the values of this item, and from collection too. Then refresh the listbox with the collection modified.
7
11203
by: Ohad Asor | last post by:
Hello all, I have an ASP.NET page I've written using VS.NET2003, which have a ListBox in it. When I press a button in the form, I try to get the selected item in the list by calling ListBox.SelectedItem. And it returns null, even when I select an item. Even when I try to read the value when I get the SelectedIndexChanged event, it still doesn't give me the correct answer.
0
924
by: Clark Stevens | last post by:
I have an ArrayList (CAL) bound to a listbox (Listbox1). I want to delete an item in the ArrayList and reflect the changes back to the listbox. I have no problem deleting ArrayList items, but how do I get the listbox to reflect the deleted items? Here's what I have so far which doesn't work. What am I doing wrong? Private Sub mnuDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDelete.Click If...
4
4964
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be listed first followed by the files.
8
2596
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array; m_ResultArray.Add ( aFoundObject);
0
9519
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
10438
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
10214
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
10001
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
9042
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
5437
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...
1
4113
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
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.