Updating Listbox Items

Short and Simple: How do I do this? ;)
[39 byte] By [codefund.com] at [2007-12-16]
# 1
I still can't figure out how to update the value of a listbox item. Does anyone know how to do this?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
what code have you tried? Are you DataBinding or just adding the items programmatically?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Right -- the question is, what exactly does "update a value in a listbox" mean? Are you attempting to modify one specific item in the list? What triggers the update? Where is the new value coming from?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
I am reading in a file and placing each item into a listbox. Then when the user clicks a line, they will be able to edit the text. A button is triggering the update. What I need is for the line to be replaced with a new value. I noticed there is not a "replace" method for the listbox. So I attempted to get the index and remove then add. However, I get an error when attempting to do so. I then tried to just take the listbox item and set a value to it. Again I get an error.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
Hmmm.... This seems to work for me:
Me.ListBox1.Items.RemoveAt(3)
Me.ListBox1.Items.Insert(3, "New Item")

Is this significantly different than what you tried?

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6
Here is how I'm attempting this:Dim myPlace As Integer = ListBox1.SelectedIndex
Me.ListBox1.Items.RemoveAt(myPlace)
Me.ListBox1.Items.Insert(myPlace, myNewValue)When I attempt this, I get the following error:

<color="darkred">Index was outside the bounds of the array.</color>

Do you know what's going on? I can't seem to figure it out.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7
Have you debugged through this? That is, single-stepped through the code to verify the value of myPlace? This code works just fine here -- I was able to replace each and every element of my list box (I tried them all) using your exact code. Are you possibly selecting multiple elements at once? Using a checked list box? I mean, what's the different between my setup, and yours?

Here's all the code in my form (I put a button named Button1 on the form):
Dim lst As New ListBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(lst)
lst.Items.Add("Item1")
lst.Items.Add("Item2")
lst.Items.Add("Item3")
lst.Items.Add("Item4")
lst.Items.Add("Item5")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myPlace As Integer = lst.SelectedIndex
lst.Items.RemoveAt(myPlace)
lst.Items.Insert(myPlace, "Jason")
End Sub

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 8
Here is my entire sub: Private Sub rowUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rowUpdate.Click
'Gather necessary info
Dim myPlace As Integer = listConfig.SelectedIndex
Dim myTempString As String = UpdateThisType(myPlace)

'Update the list and refresh
listConfig.Items.RemoveAt(myPlace)
listConfig.Items.Insert(myPlace, myTempString)
listConfig.Refresh()

'Do Save Toggle
DoSaveToggle()
End SubNothing appears to be different. If I place a MessageBox right before the update of the list and write the value of myPlace, it is an Integer. I even tried using the CINT function to convert it to an Integer. Same error.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 9
You say you checked to see that you had an integer. Did you check to see WHAT integer? Have you verified that myPlace has a reasonable value both before and after calling UpdateThisType? (It isn't likely that UpdateThisType modifies myPlace, but it could. It's also possible that UpdateMyPlace is modifying the list box (I don't see the code here)).

Debugging is a skill that develops with time, but there's no reason to twist and turn so much on simple code -- you might consider some basic debugging steps, such as using the tools provided by Visual Studio.NET for tracing and single-stepping. If myPlace contains a valid value when you retrieve the SelectedIndex property (and it must), then the following code must work:
listConfig.Items.RemoveAt(myPlace)
listConfig.Items.Insert(myPlace, myTempString)
Then, once you've determined that this works (and I can't see any reason why it wouldn't), figure out why adding in the call to UpdateThisType is causing trouble. If the first test didn't work, then there's something else wrong with the control. Try the code with a newly created, default-settings list box (as I did when testing your code, and it worked fine), and if that works, try to figure out which properties are different.

In any case, when you post a question here, and someone tries it and is unable to reproduce your problem, you have to assume that it's some property of the control that's causing you trouble. Until you post a small example that can be either examined (not as helpful) or run (more helpful) by folks here, there's really no point in going round and round. That is, you need to try to*space
space*the problem down into its various pieces, and use the process of elimination to determine the best source of the problem. In this case, since the code works fine here, and not there, you have to assume that it's either the portion of the code that is different (the call to UpdateThisType), or the control itself. Check both those things, and I'll bet you find the problem. (You shouldn't need the call the Refresh--it wasn't required here when I tried the code.)

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 10
Ken,

It is only the RemoveAt method. If I comment that out, I can add anything. It appears that I cannot delete any row that has is contains a double-quote. Now I think I'm onto something. Do you have a clue why that would happen?

Jason

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 11
Items with double-quotes work fine here. I tried this listbox, and it worked fine:
Me.ListBox1.Items.AddRange(New Object() {"Mike", "Paul", "Ken ", "P""e""ter", "Mary", "Emily", "Sandy"})
Me.ListBox1.Location = New System.Drawing.Point(80, 104)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(152, 121)
Me.ListBox1.TabIndex = 1

Must be something else. Try it with different text, and/or a different list box.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 12
I've tried this value: <?xml version="1.0" encoding="utf-8" ?> and can't get the row to delete. What can I be doing wrong?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 13
Interesting. You've hit on something here. I don't get an error, but it doesn't work correctly, either. Working on trying to figure out exactly WHY it's failing.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 14
Nope. my own stupid fault. No problem. Works fine. It's not that. That text works fine. Here's my form. Listbox looks like this:

Me.ListBox1.Items.AddRange(New Object() {"Mike", "<?xml version=""1.0"" encoding=""utf-8"" ?>", "Ken ", _
"P""e""ter", "Mary", "Emily", "Sandy"})
Me.ListBox1.Location = New System.Drawing.Point(80, 104)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(152, 121)
Me.ListBox1.TabIndex = 1

and the code for a button click looks like this:

Dim i As Integer = ListBox1.SelectedIndex
Me.ListBox1.Items.RemoveAt(i)
Me.ListBox1.Items.Insert(i, "Jerry")

I had modified my code for one of my previous posts, and hadn't put it back. This all works just fine. Keep digging.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...