HIGHLIGHT LISTVIEW SEARCHED ITEM

CRACKER 28/04/2014 08:33:41
#437680
Bom dia.

Tenho um form onde tenho apenas uma textbox e uma listview.
Pretendia manter o focus na listview, ao mesmo tempo que vou procurando o item na base de dados.
O meu código é este:

Imports System.Data.OleDb

Public Class Form1

Dim connF As New OleDbConnection
Dim myqryF As String = Nothing
Dim mycmdF As New OleDbCommand
Dim mydrF As OleDbDataReader
Dim strF As String = Nothing

Sub SearchListview()
ListView1.Items.Clear()
[ô] connF.ConnectionString = [Ô]Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[Ô] & Application.StartupPath & [Ô]\Farms.accdb[Ô]
myqryF = [Ô]SELECT * FROM Farms where (Nome LIKE [ô]%[Ô] & TextBox1.Text & [Ô]%[ô])[Ô]
[ô]Function for searching by candidate ID number
Using connF As New OleDbConnection([Ô]Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[Ô] & Application.StartupPath & [Ô]\Farms.accdb[Ô])
Using cmd As New OleDbCommand(myqryF, connF)
cmd.CommandType = CommandType.Text
connF.Open()
Using reader As OleDbDataReader = cmd.ExecuteReader()
If reader.HasRows = True Then
While reader.Read()
Dim li As New ListViewItem
li.Text = reader([Ô]ID[Ô]).ToString
li.SubItems.Add(reader([Ô]Nome[Ô]).ToString)
li.SubItems.Add(reader([Ô]X[Ô]).ToString)
li.SubItems.Add(reader([Ô]Y[Ô]).ToString)
li.SubItems.Add(reader([Ô]Bosque[Ô]).ToString)
li.SubItems.Add(reader([Ô]Argila[Ô]).ToString)
li.SubItems.Add(reader([Ô]Ferro[Ô]).ToString)
li.SubItems.Add(reader([Ô]Armazem[Ô]).ToString)
li.SubItems.Add(reader([Ô]Esconderijo[Ô]).ToString)
ListView1.Items.Add(li)
li.Selected = True
ListView1.SelectedItems(0).EnsureVisible()
End While
End If
End Using
End Using
End Using
End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
SearchListview()
End Sub

Sub FillListview()
ListView1.Items.Clear()
myqryF = [Ô]SELECT * from Farms ORDER BY id ASC[Ô]
mycmdF = New OleDbCommand(myqryF, connF)
mydrF = mycmdF.ExecuteReader

While mydrF.Read
With ListView1
.Items.Add(mydrF([Ô]ID[Ô]))
With .Items(.Items.Count - 1).SubItems
.Add(mydrF([Ô]Nome[Ô]))
.Add(mydrF([Ô]X[Ô]))
.Add(mydrF([Ô]Y[Ô]))
.Add(mydrF([Ô]Bosque[Ô]))
.Add(mydrF([Ô]Argila[Ô]))
.Add(mydrF([Ô]Ferro[Ô]))
.Add(mydrF([Ô]Armazem[Ô]))
.Add(mydrF([Ô]Esconderijo[Ô]))
End With
End With
End While
End Sub

Sub ConnToDB()
Try
With connF
If .State = ConnectionState.Open Then .Close()
.ConnectionString = [Ô]Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[Ô] & Application.StartupPath & [Ô]\Farms.accdb[Ô]
.Open()
End With
Catch ex As Exception
MessageBox.Show([Ô]Unable to connect[Ô], [Ô]Error[Ô], MessageBoxButtons.OK, MessageBoxIcon.Error)
Application.Exit()
End Try
End Sub

Sub LVsettings()
With ListView1.Columns
.Add([Ô]ID[Ô], 0, HorizontalAlignment.Center)
.Add([Ô]Nome[Ô], 200, HorizontalAlignment.Center)
.Add([Ô]X[Ô], 60, HorizontalAlignment.Center)
.Add([Ô]Y[Ô], 60, HorizontalAlignment.Center)
.Add([Ô]Bosque[Ô], 70, HorizontalAlignment.Center)
.Add([Ô]Poço de Argila[Ô], 100, HorizontalAlignment.Center)
.Add([Ô]Mina de Ferro[Ô], 100, HorizontalAlignment.Center)
.Add([Ô]Armazém[Ô], 100, HorizontalAlignment.Center)
.Add([Ô]Esconderijo[Ô], 100, HorizontalAlignment.Center)
End With
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call LVsettings()
Call ConnToDB()
Call FillListview()
End Sub

End Class
CRACKER 30/04/2014 08:10:51
#437791
Bom dia. Caso seja mais fácil de resolver o problema com um botão de search também serve... desde que ajude a resolver o problema.

Obrigado desde já.
TUNUSAT 30/04/2014 08:53:04
#437795
Resposta escolhida
CRACKER,

Desculpe, está difícil de entender o problema.
Como assim [Ô]manter o foco[Ô]?
O que você pode fazer é grifar (co outra cor) a linha na grade que se aproxima do que você digitou... Por exemplo:
ListView1.SelectedItems(0).BackColor = Color.Red


Anexo o seu código.

[][ô]s,
Tunusat.
Tópico encerrado , respostas não são mais permitidas