C# 向 ListView 控件添加搜索功能
2011-07-08 10:48
351 查看
在 ListView 控件中使用大型的项列表时,经常会希望向用户提供搜索功能。ListView 控件以两种不同的方式提供此功能:文本匹配和位置搜索。 FindItemWithText 方法允许在处于列表或详细信息视图的 ListView 上执行文本搜索,要求给定搜索字符串和可选的起始和结束索引。而 FindNearestItem 方法允许在处于图标或平铺视图的 ListView 中查找项,要求给定一组 x 坐标和 y 坐标以及一个搜索方向。
调用 FindItemWithText 方法,向其传递要查找的项的文本。
下面的代码示例演示如何创建基本 ListView,用项进行填充并使用由用户输入的文本来在列表中查找项。
使用文本查找项
创建一个 ListView,View 属性设置为 Details 或 List,然后用项填充该 ListView。调用 FindItemWithText 方法,向其传递要查找的项的文本。
下面的代码示例演示如何创建基本 ListView,用项进行填充并使用由用户输入的文本来在列表中查找项。
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 ListViewWithItems 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") }); // 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; } }
相关文章推荐
- 向 ListView 控件添加搜索功能
- [代码问题解决录A1]c#Listview控件动态添加数据并阻止重复添加
- c#中listview控件如何添加信息
- C#为ListView添加自动列宽调整和点击列表头自动排序功能
- c#中listview控件添加信息例程
- Android开发-UI控件:为ListView,GirdView,etc...添加系统自带的下拉刷新功能
- C# ListView 简单的搜索功能
- C#-ListView控件中列添加控件ComboBox,控件TextBox,添加时间选择列DateTimePicker
- Android高级控件(一)——ListView绑定CheckBox实现全选,添加和删除等功能
- C# toolstrip 上添加 DateTimePicker 控件
- 百度搜索结果出现“添加至首页”功能
- C#中 ListView控件行高的巧妙设置方法
- c#开发的一个在winfrom下可编辑的table控件,可代替datagridview部分功能
- C#中使用ListView动态添加数据不闪烁(转)
- c#GroupBox控件添加右键菜单
- c# 给窗体添加MouseEnter事件,可鼠标移到任意控件均会产生MouseEnter事件,如何识别是哪个产生的呢?
- 重写ListView控件,实现点击列头排序的功能
- C# WinForm实现窗体上控件自由拖动功能示例
- C# 遍历文件夹图片并用ListView控件展示
- C#添加Tao控件来调用opengl