Search code examples
buttoncomboboxtextboxvb6

How do I add the values ​of a ListView in a TextBox / ComboBox? VB6


I have this interface:

Data entry:

They are loaded in the ListView:

What I want is that when double clicking on an item loaded from the list, it will turn up with the values ​​with which it was loaded. That is, I double click on the list item, it must load the values ​​of the ComboBox, TextBox, CheckBox with which it was added to the Listview. This would be so that in case the user has loaded a piece of data incorrectly, it can edit it by double clicking.

I thought about this but it didn't work:

Private Sub List_Contactos_DblClick()

Dim i As Integer
Dim IntPrincipal As Integer
Dim IntVinculo As Integer
Dim IntTipo As Integer

If MebPreFijo.Text = "" And txttel.Text = "" Then
        'For i = 1 To List_Contactos.ListItems.Count

        'CmbTipoTel
        If List_Contactos.ListItems(i).SubItems(1) = "Fijo" Then
            IntTipo = 1
        Else
            IntTipo = 2
        End If
        CmbTipoTel.Text = List_Contactos.ListItems(i).SubItems(1)

        'MebPreFijo
        MebPreFijo.Text = List_Contactos.ListItems(i).SubItems(2)

        'txttel
        txttel.Text = List_Contactos.ListItems(i).SubItems(3)

        'CheckPrincipal
        If List_Contactos.ListItems(i).SubItems(4) = "SI" Then
            IntPrincipal = 1
        Else
            IntPrincipal = 0
        End If
        ChPrincipal.value = IntPrincipal

        'ComboTipoVinculo
        If List_Contactos.ListItems(i).SubItems(5) = "" Then
            IntVinculo = 0
        Else
            Rs.Open "select tv_id from dbo.Tipo_Vinculo where tv_descripcion='" & List_Contactos.ListItems(i).SubItems(5) & "'", Cn, adOpenKeyset, adLockReadOnly
            IntVinculo = Rs!TV_Id
            Rs.Close
        End If
        ChVinculo.value = IntVinculo

        'Limpiar ese item
        List_Contactos.ListItems.Remove List_Contactos.SelectedItem.Index

    'Next i

    Else
    MsgBox "Tiene datos que se pueden perder. Por favor, presione añadir."

End If

End Sub

Suggestions? 'Cause when i press the item, i see the next error:

Index out of bounds Run-time error "35600"

Solution

  • The subscript i is not going to reference the selected item in the listview. Try SelectedItem instead.

    For instance:

    If List_Contactos.SelectedItem.SubItems(1) = "Fijo" Then