ListView控件演示04:查找列表中包含指定字符串的项

本文介绍了如何利用ListView.FindItemWithText()方法在列表视图中快速查找以指定文本开头的项,通过示例代码演示了方法的应用。重点在于展示如何在Windows应用程序中实现高效的数据搜索功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ListView.FindItemWithText()
查找以指定文本值开头的第一个 ListViewItem。

代码示例说明了 FindItemWithText 方法。
此方法将返回以指定文本开头的第一个项。例如,如果 ListView 包含两个列表项,第一个项的文本设置为“angle bracket”,而第二个项的文本设置为“bracket”,那么,在调用 FindItemWithText 时将 brack 作为参数传递会返回文本为“bracket”的项。

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Demo04 { public partial class MainForm : Form { // Declare the ListView and Button for the example. ListView findListView = new ListView(); Button findButton = new Button(); public MainForm() { InitializeComponent(); this.InitializeFindListView(); } private void InitializeFindListView() { // Set up the location and event handling for the button. this.findButton.Text = "Find"; this.findButton.AutoSize = true; findButton.Click += new EventHandler(findButton_Click); findButton.Location = new Point(10, 10); // Set up the location of the ListView and add some items. findListView.Location = new Point(10, 10 + this.findButton.Height + 7); findListView.View = View.List; findListView.Items.Add(new ListViewItem("angle bracket")); findListView.Items.Add(new ListViewItem("bracket holder")); findListView.Items.Add(new ListViewItem("bracket")); // Add the button and ListView to the form. this.Controls.Add(findButton); this.Controls.Add(findListView); } void findButton_Click(object sender, EventArgs e) { // Call FindItemWithText, sending output to MessageBox. ListViewItem item1 = findListView.FindItemWithText("brack"); if (item1 != null) MessageBox.Show("Calling FindItemWithText passing 'brack': " + item1.ToString()); else MessageBox.Show("Calling FindItemWithText passing 'brack': null"); } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值