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

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"); } } }

你可能感兴趣的:(ListView控件演示04:查找列表中包含指定字符串的项)