How can i remove an array item by selecting an item from listobx and press remove/delete button?

for an example, if i want remove index 2 from listbox, so the array should remove index 2 item and move the item of index 3 to index 2 and so on.
is needed for so much. :)

Hmmm I think you are going to have to rebuild the Array each time or end up with a load of empty values at the end of the array...

You will need to implement an ArrayList then cast from the arraylist to a string.

Example:

Dim ar As New ArrayList

'Removing'
ar.RemoveAt(0)


'Casting'
 Dim s As String

 s = TryCast(ar(0),String)

'If the cast fails it will return Nothing. Make sure to test for failure.'

If s IsNot Nothing Then
'Do work'
End if

'-or-'

If IsNothing(s) = false Then
'Do work'
End if

Hope this helps! :)

Your Do work is mean what? @@
by the way, i show you my code, and also what i've tried.

 Private Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        If lstDetail.SelectedIndex = -1 Then
            MessageBox.Show("Please do selection", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            lstDetail.Items.RemoveAt(lstDetail.SelectedIndex)
            'studentID(lstDetail.SelectedIndex).re()

        End If

    End Sub

well , you have to use the array list , for this purpose ,
here is what i tried at my end first i add a list box and a button on my form , i add 4 items a b c d manually in the list box , then i declare an arraylist and used this code at the load event of my form

       Dim i As Integer
        For i = 0 To ListBox1.Items.Count - 1

            Dim a As String
            a = ListBox1.Items(i).ToString()
            MsgBox(a)
            arr.Add(a)
        Next

this code will insert all the items of the list box in my array list , now i used this code the remove button

 Dim i As Integer
    i = ListBox1.SelectedIndex
    ListBox1.Items.RemoveAt(i)
    arr.RemoveAt(i)

this code will remove items of from the listbox and also from the array list ,

this code is random , and not in a ideal format , may be this approch is acceptable for you , but i hope this code will help you.

Regards

okay, i know what your code means, but what if removed an item from the listbox and also from the array, at the sametime, the array after it will moving up.
for an example,
i want remove 'b', so the listbox and array will remove at index 1 right?
but how to move index 2 to index 1, index 3 to index 2, and index 3 become empty?
this is my problem. :)

sorry i cant get your point , well in above code , i use array list , if you remove index 2 then the item at index 3 will auto move to index 2 , same case in list box , well if you be more specific about your point then i think i can give you better answer ,

Regards

Dim arryList() as String = {"a", "b", "c"}

'Now if i delete index 1, which mean "b"
'It Should become like this = {"a", "", "c"}

'and what i want is move the "c" to index 1
'which mean i want the result is something like this after removed index 1("b")

resultArryList() as String = {"a", "c", ""}

i Hope you get my point. :)

This article has been dead for over six months. Start a new discussion instead.