Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: juanjoxx en 25 Marzo 2008, 16:39 pm



Título: autocompletar en listview
Publicado por: juanjoxx en 25 Marzo 2008, 16:39 pm
hola a todos  ;D de nuevo ...
bueno esta vez quisiera q me ayuden en autompletar en el listview aqui les dejo el code  ;D
para autocompletar en el text no hay problema, sino lo  q quiero es q automplete por la segunda columna  :huh: taba chancando pero nu me sale haber si me dan una manito ....
                                            Gracias  ;D

ahhh solo copiar y pegar, en el form agregar 01 listview y 01 caja de texto

Código:
 Private Sub Form_Load()
Dim ElItem As ListItem
Dim i As Integer
With ListView1
    .View = lvwReport
    .GridLines = True
    .LabelEdit = lvwManual
    .ColumnHeaders.Add , , "Codigo", 800
    .ColumnHeaders.Add , , "Nombre"
    .FullRowSelect = True
    .TextBackground = lvwTransparent
End With
    Set ElItem = ListView1.ListItems.Add(, , Format("3215", "0000"))
    ElItem.SubItems(1) = "Juan"
        Set ElItem = ListView1.ListItems.Add(, , Format("2145", "0000"))
    ElItem.SubItems(1) = "pedro"
        Set ElItem = ListView1.ListItems.Add(, , Format("3568", "0000"))
    ElItem.SubItems(1) = "Jose"
        Set ElItem = ListView1.ListItems.Add(, , Format("4568", "0000"))
    ElItem.SubItems(1) = "maria"
        Set ElItem = ListView1.ListItems.Add(, , Format("2160", "0000"))
    ElItem.SubItems(1) = "Carlos"
        Set ElItem = ListView1.ListItems.Add(, , Format("1000", "0000"))
    ElItem.SubItems(1) = "Cesar"
        Set ElItem = ListView1.ListItems.Add(, , Format("1500", "0000"))
    ElItem.SubItems(1) = "Jorge"
        Set ElItem = ListView1.ListItems.Add(, , Format("8888", "0000"))
    ElItem.SubItems(1) = "Juan Antonio"
        Set ElItem = ListView1.ListItems.Add(, , Format("8099", "0000"))
    ElItem.SubItems(1) = "Esteba"
        Set ElItem = ListView1.ListItems.Add(, , Format("6801", "0000"))
    ElItem.SubItems(1) = "Ricardo"
End Sub

Sub Autocompletar(ListView As ListView, TBox As TextBox)
       
    ' variable para usar con el método FindItem que _
      permite buscar en el LV
    Dim item As ListItem
    Dim seleccion As Integer
       
    ' busca en el item, la cadena escrita en el textbox, si coincide _
      devuelve una referencia al item
    Set item = ListView.FindItem(TBox.Text, 0, , 1)
           
        ' verifica que el item no sea un valor nothing
        If Not item Is Nothing Then
            ' Muestra la selección pormas que no tenga el foco
            ListView.HideSelection = False
               
            ' desplaza la lista
            item.EnsureVisible
               
            ' selecciona el item
            item.Selected = True
 
            If Not Flag Then
                ' Almacena la posición de la selección en el textbox
                seleccion = TBox.SelStart
                   
                ' Asigna el texto completo del item encontrado
                TBox.Text = CStr(item)
                    If Not TBox.Text = vbNullString Then
                        ' posición de la selección
                        TBox.SelStart = seleccion
                        ' selecciona el texto
                        TBox.SelLength = Len(TBox.Text) - seleccion
                    End If
            End If
        Else
            ' Oculta la selección ya que no hay coincidencia
            ListView.HideSelection = True
        End If
 
End Sub


Private Sub Text1_Change()
    ' autocompleta el Listview
    Autocompletar ListView1, Text1
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Flag = False
    ' si se presionan el tecla de retroceso y la de borrar _
      no se autocompleta
    If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
        Flag = True
    End If
End Sub
 

por cierto el code no es mio pero me ayudo bastante xD!!


Título: Re: autocompletar en listview
Publicado por: ((( SPAWN ))) en 25 Marzo 2008, 20:13 pm
Hola como estas, lo q yo se es q la busqueda en los lvwSubitem o (valor=1) siempre busca la cadena completa, y en el lvwText o (valor=0), la búsqueda parcial sólo es desde el principio, al codigo de la funcion Autocompletar le agregue un checkbox para comprobar si se quiere realizar la busqueda por nombre.

Código:
Sub Autocompletar(ListView As ListView, TBox As TextBox)
Dim item As ListItem
Dim seleccion As Integer

    If chkNombre.Value = 0 Then
      Set item = ListView.FindItem(TBox.Text, 0, , 1)
          If Not item Is Nothing Then
              ListView.HideSelection = False
              item.EnsureVisible
              item.Selected = True
              If Not Flag Then
                  seleccion = TBox.SelStart
                  TBox.Text = CStr(item)
                      If Not TBox.Text = vbNullString Then
                          TBox.SelStart = seleccion
                          TBox.SelLength = Len(TBox.Text) - seleccion
                      End If
              End If
          Else
              ListView.HideSelection = True
          End If
    Else
      Set item = ListView.FindItem(TBox.Text, lvwSubItem, , lvwPartial)
          If Not item Is Nothing Then
              ListView.HideSelection = False
              item.EnsureVisible
              item.Selected = True
              TBox.SelStart = seleccion
              TBox.SelLength = Len(TBox.Text) - seleccion
          Else
              ListView.HideSelection = True
          End If
    End If
End Sub

Espero haber ayudado en algo. Saludos


Título: Re: autocompletar en listview
Publicado por: juanjoxx en 26 Marzo 2008, 11:17 am
gracias ((( SPAWN )))  por responder ... ahora  a probar  ;D