Is this significantly different than what you tried?
<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.
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
'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.
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.)
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
Must be something else. Try it with different text, and/or a different list box.
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.