Free Information Technology Magazines and eBooks

Friday, June 12, 2009

Find an item using text in ListView Control

I was banging my head last night because I cannot remember the method use to find an item in a ListView control. I keep on trying the ListView1.Items.Find function but I can get what I want. My application accepts a search keyword then it will search the text from the items listed on the listview. This morning, I dugg down on my old files and found what I'm looking for -- FindItemWithText.


FindItemWithText is a method that allows you to perform a text search on a ListView in list or details view, given a search string and an optional starting and ending index.

Here are sample source codes that demonstrate the search capability of ListView using text.

In VB.NET


Dim lvItem As ListViewItem = _
ListView1.FindItemWithText(TextBox1.Text, False, 0, True)


If (lvItem IsNot Nothing) Then
ListView1.TopItem = lvItem
End If


In C#


ListViewItem lvItem =
ListView1.FindItemWithText(TextBox1.Text, false, 0, true);

if (lvItem != null)
{
ListView1.TopItem = lvItem;

}


For more Coding Tips & Tricks, subscribe now.

7 comments:

Anonymous said...

Not working with vb.net. can you be more specific?

Fryan Valdez said...

it should work. i'm using it on all of my vb.net codes. you can post the specific error so i can help more

Anonymous said...

your code using textbox to listview for search function is great..

it works properly to my code. my problem now is, how can i populate my listview using textbox as it exactly to my text. sorry for my english..

for example. i type letter "a" in the textbox, then i want the listview to display all starts with letter a. because in your code it display my input text at the top, i want only letter that starts with letter a into the listview. can you help me?

Fryan Valdez said...

Hi I will guess that you will populate your listview from a database. To do this, all you have to do is filter it from your SQL query. For example, you can pass something like this:

"SELECT * FROM MyTable WHERE Firstname Like '" & textbox1.text & "%'"

Anonymous said...

thanks for your suggestion.. but i need to know how to apply it to listview. all i know is for datagrid.. if you are not busy can u make a simple program that connects with MYSQL database.. any way this is my code.

Imports MySql.Data.MySqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Data
Public Class Form1

Private Sub BtnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnView.Click
Dim conn As New MySqlConnection
conn.ConnectionString = "server=" & txtServer.Text & ";" & "user id=" & txtUsername.Text & ";" & "password=" & txtPassword.Text & ";" & "database= spsprofile"
Dim com As New MySqlCommand
Dim rdr As MySqlDataReader
conn.Open()
With com
.Connection = conn
.CommandText = "SELECT ID_Number, First_Name, Last_Name FROM authorizedusers"
.CommandType = CommandType.Text
End With
rdr = com.ExecuteReader
While rdr.Read
LvCustomer.Items.Add(rdr("ID_Number")) ' column for customer code
LvCustomer.Items(LvCustomer.Items.Count - 1).SubItems.Add(rdr("First_Name")) ' column for customer's name
LvCustomer.Items(LvCustomer.Items.Count - 1).SubItems.Add(rdr("Last_Name")) ' column for customer's address
End While
rdr.Close()
conn.Close()
End Sub

Private Sub LvCustomer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LvCustomer.SelectedIndexChanged
TextBox1.Text = LvCustomer.Items.Item(LvCustomer.FocusedItem.Index).SubItems(0).Text
TextBox2.Text = LvCustomer.Items.Item(LvCustomer.FocusedItem.Index).SubItems(1).Text
TextBox3.Text = LvCustomer.Items.Item(LvCustomer.FocusedItem.Index).SubItems(2).Text
End Sub


Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged


/// I need the code here ///


End Sub
End Class

Anonymous said...

Hi, I have a little problem similar to your posts. I simple want to get all the data from a Listview control. any one knows how to do this? tnx.

Anonymous said...

its not working..