向 ListView 控件添加搜索功能

来源:百度文库 编辑:神马文学网 时间:2024/04/27 18:52:23
向 ListView 控件添加搜索功能

在 ListView 控件中使用大型的项列表时,经常会希望向用户提供搜索功能。ListView 控件以两种不同的方式提供此功能:文本匹配和位置搜索。

FindItemWithText 方法允许在处于列表或详细信息视图的 ListView 上执行文本搜索,要求给定搜索字符串和可选的起始和结束索引。而 FindNearestItem 方法允许在处于图标或平铺视图的 ListView 中查找项,要求给定一组 x 坐标和 y 坐标以及一个搜索方向。

使用文本查找项

  1. 创建一个 ListView,View 属性设置为 Details 或 List,然后用项填充该 ListView

  2. 调用 FindItemWithText 方法,向其传递要查找的项的文本。

  3. 下面的代码示例演示如何创建基本 ListView,用项进行填充并使用由用户输入的文本来在列表中查找项。

Visual Basic 复制代码
Private textListView As New ListView()Private WithEvents searchBox As New TextBox()Private Sub InitializeTextSearchListView()searchBox.Location = New Point(150, 20)textListView.Scrollable = TruetextListView.Width = 80textListView.Height = 50' Set the View to list to use the FindItemWithText method.textListView.View = View.List' Populate the ListView with items.textListView.Items.AddRange(New ListViewItem() { _New ListViewItem("Amy Alberts"), _New ListViewItem("Amy Recker"), _New ListViewItem("Erin Hagens"), _New ListViewItem("Barry Johnson"), _New ListViewItem("Jay Hamlin"), _New ListViewItem("Brian Valentine"), _New ListViewItem("Brian Welker"), _New ListViewItem("Daniel Weisman")})' Add the controls to the form.Me.Controls.Add(textListView)Me.Controls.Add(searchBox)End SubPrivate Sub searchBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs) _Handles searchBox.TextChanged' Call FindItemWithText with the contents of the textbox.Dim foundItem As ListViewItem = _textListView.FindItemWithText(searchBox.Text, False, 0, True)If (foundItem IsNot Nothing) ThentextListView.TopItem = foundItemEnd IfEnd Sub
C# 复制代码
private ListView textListView = new ListView();private TextBox searchBox = new TextBox();private void InitializeTextSearchListView(){searchBox.Location = new Point(10, 60);textListView.Scrollable = true;textListView.Width = 80;textListView.Height = 50;// Set the View to list to use the FindItemWithText method.textListView.View = View.List;// Populate the ListViewWithItemstextListView.Items.AddRange(new ListViewItem[]{new ListViewItem("Amy Alberts"),new ListViewItem("Amy Recker"),new ListViewItem("Erin Hagens"),new ListViewItem("Barry Johnson"),new ListViewItem("Jay Hamlin"),new ListViewItem("Brian Valentine"),new ListViewItem("Brian Welker"),new ListViewItem("Daniel Weisman") });// Handle the TextChanged to get the text for our search.searchBox.TextChanged += new EventHandler(searchBox_TextChanged);// Add the controls to the form.this.Controls.Add(textListView);this.Controls.Add(searchBox);}private void searchBox_TextChanged(object sender, EventArgs e){// Call FindItemWithText with the contents of the textbox.ListViewItem foundItem =textListView.FindItemWithText(searchBox.Text, false, 0, true);if (foundItem != null){textListView.TopItem = foundItem;}}
C++ 复制代码
private:ListView^ textListView;TextBox^ searchBox;private:void InitializeTextSearchListView(){textListView = gcnew ListView();searchBox = gcnew TextBox();searchBox->Location = Point(150, 20);textListView->Scrollable = true;textListView->Width = 100;// Set the View to list to use the FindItemWithText method.textListView->View = View::List;// Populate the ListViewWithItemstextListView->Items->AddRange(gcnew array{gcnew ListViewItem("Amy Alberts"),gcnew ListViewItem("Amy Recker"),gcnew ListViewItem("Erin Hagens"),gcnew ListViewItem("Barry Johnson"),gcnew ListViewItem("Jay Hamlin"),gcnew ListViewItem("Brian Valentine"),gcnew ListViewItem("Brian Welker"),gcnew ListViewItem("Daniel Weisman") });// Handle the TextChanged to get the text for our search.searchBox->TextChanged += gcnew EventHandler(this,&Form1::searchBox_TextChanged);// Add the controls to the form.this->Controls->Add(textListView);this->Controls->Add(searchBox);}private:void searchBox_TextChanged(Object^ sender, EventArgs^ e){// Call FindItemWithText with the contents of the textbox.ListViewItem^ foundItem =textListView->FindItemWithText(searchBox->Text, false, 0, true);if (foundItem != nullptr){textListView->TopItem = foundItem;}}

使用 x 坐标和 y 坐标查找项

  1. 创建一个 ListView,View 属性设置为 SmallIcon 或 LargeIcon,然后用项填充该 ListView

  2. 调用 FindNearestItem 方法,向其传递所需的 x 坐标和 y 坐标以及要查找的方向。

  3. 下面的代码示例演示如何创建基本图标 ListView,用项进行填充和捕获 MouseDown 事件来在向上方向查找最近的项。

Visual Basic 复制代码
Private WithEvents iconListView As New ListView()Private previousItemBox As New TextBox()Private Sub InitializeLocationSearchListView()previousItemBox.Location = New Point(150, 20)' Create an image list for the icon ListView.iconListView.LargeImageList = New ImageList()' Add an image to the ListView large icon list.iconListView.LargeImageList.Images.Add(New Bitmap(GetType(Control), "Edit.bmp"))' Set the view to large icon and add some items with the image' in the image list.iconListView.View = View.SmallIconiconListView.Items.AddRange(New ListViewItem() { _New ListViewItem("Amy Alberts", 0), _New ListViewItem("Amy Recker", 0), _New ListViewItem("Erin Hagens", 0), _New ListViewItem("Barry Johnson", 0), _New ListViewItem("Jay Hamlin", 0), _New ListViewItem("Brian Valentine", 0), _New ListViewItem("Brian Welker", 0), _New ListViewItem("Daniel Weisman", 0)})Me.Controls.Add(iconListView)Me.Controls.Add(previousItemBox)End SubSub iconListView_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) _Handles iconListView.MouseDown' Find the next item up from where the user clicked.Dim foundItem As ListViewItem = _iconListView.FindNearestItem(SearchDirectionHint.Up, e.X, e.Y)' Display the results in a textbox.If (foundItem IsNot Nothing) ThenpreviousItemBox.Text = foundItem.TextElsepreviousItemBox.Text = "No item found"End IfEnd Sub
C# 复制代码
ListView iconListView = new ListView();TextBox previousItemBox = new TextBox();private void InitializeLocationSearchListView(){previousItemBox.Location = new Point(150, 20);// Create an image list for the icon ListView.iconListView.LargeImageList = new ImageList();iconListView.Height = 400;// Add an image to the ListView large icon list.iconListView.LargeImageList.Images.Add(new Bitmap(typeof(Control), "Edit.bmp"));// Set the view to large icon and add some items with the image// in the image list.iconListView.View = View.LargeIcon;iconListView.Items.AddRange(new ListViewItem[]{new ListViewItem("Amy Alberts", 0),new ListViewItem("Amy Recker", 0),new ListViewItem("Erin Hagens", 0),new ListViewItem("Barry Johnson", 0),new ListViewItem("Jay Hamlin", 0),new ListViewItem("Brian Valentine", 0),new ListViewItem("Brian Welker", 0),new ListViewItem("Daniel Weisman", 0) });this.Controls.Add(iconListView);this.Controls.Add(previousItemBox);// Handle the MouseDown event to capture user input.iconListView.MouseDown +=new MouseEventHandler(iconListView_MouseDown);//iconListView.MouseWheel += new MouseEventHandler(iconListView_MouseWheel);   }void iconListView_MouseDown(object sender, MouseEventArgs e){// Find the an item above where the user clicked.ListViewItem foundItem =iconListView.FindNearestItem(SearchDirectionHint.Up, e.X, e.Y);// Display the results in a textbox..if (foundItem != null)previousItemBox.Text = foundItem.Text;elsepreviousItemBox.Text = "No item found";}
C++ 复制代码
    ListView^ iconListView;TextBox^ previousItemBox;private:void InitializeLocationSearchListView(){previousItemBox = gcnew TextBox();iconListView = gcnew ListView();previousItemBox->Location = Point(150, 20);// Create an image list for the icon ListView.iconListView->SmallImageList = gcnew ImageList();// Add an image to the ListView small icon list.iconListView->SmallImageList->Images->Add(gcnew Bitmap(Control::typeid, "Edit.bmp"));// Set the view to small icon and add some items with the image// in the image list.iconListView->View = View::SmallIcon;iconListView->Items->AddRange(gcnew array{gcnew ListViewItem("Amy Alberts", 0),gcnew ListViewItem("Amy Recker", 0),gcnew ListViewItem("Erin Hagens", 0),gcnew ListViewItem("Barry Johnson", 0),gcnew ListViewItem("Jay Hamlin", 0),gcnew ListViewItem("Brian Valentine", 0),gcnew ListViewItem("Brian Welker", 0),gcnew ListViewItem("Daniel Weisman", 0) });this->Controls->Add(iconListView);this->Controls->Add(previousItemBox);// Handle the MouseDown event to capture user input.iconListView->MouseDown += gcnew MouseEventHandler(this, &Form1::iconListView_MouseDown);}void iconListView_MouseDown(Object^ sender, MouseEventArgs^ e){// Find the next item up from where the user clicked.ListViewItem^ foundItem = iconListView->FindNearestItem(SearchDirectionHint::Up, e->X, e->Y);// Display the results in a textbox..if (foundItem != nullptr){previousItemBox->Text = foundItem->Text;}else{previousItemBox->Text = "No item found";}}

请参见

任务

如何:使用 Windows 窗体 ListView 控件添加和移除项

参考

ListView 控件概述(Windows 窗体)
ListView
FindItemWithText
FindNearestItem

其他资源

ListView 控件(Windows 窗体)