Public Class Form1
Dim t_A = 100
Dim arreglo(0) As String
Dim indice As Integer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
indice = -1
End Sub
Private Sub btn_agregar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_agregar.Click
If indice = t_A - 1 Then
MessageBox.Show("Ha llegado al tope de elementos permitidos para agregar")
Else
indice += 1
ReDim Preserve arreglo(indice)
arreglo(indice) = txt_elemento.Text
lst_A.Items.Add(arreglo(indice))
txt_elemento.Clear()
txt_elemento.Focus()
End If
End Sub
Private Sub btn_eliminar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_eliminar.Click
Dim item As String
'Valida que el arreglo tenga items
If arreglo.Length = 0 Then
MessageBox.Show("No hay elementos disponibles para eliminar")
Exit Sub
End If
If lst_A.SelectedIndex = -1 Then
MessageBox.Show("Debe seleccionar un item para eliminar")
Exit Sub
End If
lst_A.Items.Remove(lst_A.SelectedItem)
indice = -1
ReDim arreglo(lst_A.Items.Count - 1)
For Each item In lst_A.Items
indice += 1
arreglo(indice) = item
Next
End Sub
Private Sub btn_insertar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_insertar.Click
Dim item As String
If indice = t_A - 1 Then
MessageBox.Show("Ha llegado al tope de elementos permitidos para agregar")
Else
lst_A.Items.Insert(lst_A.SelectedIndex, txt_elemento.Text)
indice = -1
ReDim arreglo(lst_A.Items.Count - 1)
For Each item In lst_A.Items
indice += 1
arreglo(indice) = item
Next
txt_elemento.Clear()
txt_elemento.Focus()
End If
End Sub
End Class