change image by row in listview vb6

1

I am modifying an application of my work. It is made in VB6, the idea is that I get an OK or an error by means of a winsock, and I must dump that state to a row in a listview.

I have achieved almost that it works, when I receive, I look in the rows of the listview if the id or name of the record exists, and if so I only modify if it is Ok or Type of Error, but I can not reflect that state changing the image from the first column.

I have the listview1 linked to an imagelist1, where I inserted 2 images:

  • the 1st is index 1 key estado_ok
  • the second index 2 state_err

I need to be able to change the icon at run time. I pass the code involved:

Private Sub Form_Load()
Dim ch As ColumnHeader

    With ListView1
        .ColumnHeaders.Clear
        .ListItems.Clear
        .View = lvwReport
        .SmallIcons = ImageList1
        .SortKey = 0
        .SortOrder = lvwAscending
        .Sorted = True
        Set ch = .ColumnHeaders.Add(, , "           Equipo", 1700)
        Set ch = .ColumnHeaders.Add(, , "Estado", 1700)
        Set ch = .ColumnHeaders.Add(, , "Fecha reporte", 2300, lvwColumnCenter)
        Set ch = .ColumnHeaders.Add(, , "Tickets", 1074, lvwColumnCenter)
    End With

    Label1 = Winsock1.LocalIP
'    ReDim acumulado(0)


    'Cargo parámetros winsock y oculto el form1
     Call Carga_Inicial

    'Habilito el systray oculto y ok
     Call Carga_Ok(1)

End Sub

And here is where I should change the icon, and everything I tried gives me an error

    If cadena(1) <> "Impresora Ok !" Then  
        ListView1.SmallIcon = ImageList1.ListImages.Item(1)
        Else
        ListView1.SmallIcon = ImageList1.ListImages.Item(2)
    End If

I know it's very bad, but I can not find it back.

    
asked by look68 10.02.2017 в 21:38
source

1 answer

1

In the code in which the element icon must be changed, you are trying to modify the reference of the control and not the property of the element.

The code must determine the index of the control element ListView that must be modified and then change the property SmallIcon of the element:

Dim nElemento as Integer

'Obtener índice del elemento a modificar de alguna forma
'Asumiendo que se desea cambiar el ícono del primer elemento de la lista
nElemento = 1

If cadena(1) <> "Impresora Ok !" Then  
    ListView1.ListItems(nElemento).SmallIcon = 1 'o "estado_ok"
Else
    ListView1.ListItems(nElemento).SmallIcon = "estado_err" 'o 2
End If

The properties Icon and SmallIcon of objects ListItem can be assigned the corresponding index in the control ImageList associated with the properties Icons and SmallIcons respectively of the% control ListView that belong. The index can be the number of the position of the image in the control ImageList according to how the images were added starting with 1; or it can be the value of the Key property associated with the image in the ImageList control.

Ok the routines that you add and modify:

Private Sub agregar_item(cadena1 As String)
Dim subelemento As ListItem
Dim n As Integer
Dim aSubItems() As String

    aSubItems = Split(cadena1, ",")

    If aSubItems(3) = "TERMINAL1" Then aSubItems(3) = "TERMINAL 1"
    If aSubItems(1) = "AUTOOK" Then aSubItems(1) = "Impresora Ok !"
    If aSubItems(1) = "AUTOERROR" Then aSubItems(1) = "Fuera de Servicio !"
    If aSubItems(1) = "AUTOPAPEL" Then aSubItems(1) = "Cambiar Papel !"

    Set subelemento = ListView1.ListItems.Add(, , aSubItems(3), , 1)
        subelemento.SubItems(1) = aSubItems(1)
        subelemento.SubItems(2) = aSubItems(4)
        subelemento.SubItems(3) = aSubItems(2)

    ListView1.SortKey = 0
    ListView1.SortOrder = lvwAscending
    ListView1.Sorted = True
    Erase aSubItems
End Sub

Private Function modificar_lv(nom_terminal2 As String) As Boolean
Dim i As Integer
Dim cad_item As String

    For i = 1 To ListView1.ListItems.Count
        cad_item = ListView1.ListItems.Item(i).Text
'            If cad_item = nom_terminal2 Then ListView1.ListItems.Remove (i): MsgBox ("Item Removido!")
            If cad_item = nom_terminal2 Then
                ListView1.ListItems.Item(i).SubItems(1) = mensaje_error
                ListView1.ListItems.Item(i).SubItems(2) = mensaje_fechayhora
                ListView1.ListItems.Item(i).SubItems(3) = mensaje_cuenta
                modificar_lv = True
                Exit Function
            End If
    Next i

        ListView1.SortKey = 0
        ListView1.SortOrder = lvwAscending
        ListView1.Sorted = True

i = 0
cad_item = ""

End Function
    
answered by 10.02.2017 в 22:11