ListView.FindItemWithText Method (String) (System.Windows.Forms)

Switch View :
ScriptFree
.NET Framework Class Library
ListView.FindItemWithText Method (String)

Finds the first ListViewItem that begins with the specified text value.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Syntax

Visual Basic
Public Function FindItemWithText ( _
	text As String _
) As ListViewItem
C#
public ListViewItem FindItemWithText(
	string text
)
Visual C++
public:
ListViewItem^ FindItemWithText(
	String^ text
)
F#
member FindItemWithText : 
        text:string -> ListViewItem 

Parameters

text
Type: System.String
The text to search for.

Return Value

Type: System.Windows.Forms.ListViewItem
The first ListViewItem that begins with the specified text value.
Remarks

The search is case-insensitive.

The text parameter can specify a substring of the desired matching text. In addition, this method will return the first item that starts with the specified text. For example, if a ListView contains two list items—the first item's text set to "angle bracket" and the second item's text set to "bracket"—a call to FindItemWithText passing brack as the parameter will return the item whose text is "bracket".

The FindItemWithText method returns null if the list is empty or there is no matching item.

Examples

The following code example demonstrates the FindItemWithText method. To run this example, paste the following code into a Windows Form and call the InitializeFindListView method from the form's constructor or Load event handler. Click the button to see the results of the method call.

Visual Basic

' Declare the ListView and Button for the example.
Private findListView As New ListView()
Private WithEvents findButton As New Button()


Private Sub InitializeFindListView()

    ' Set up the location and event handling for the button.
    findButton.Location = New Point(10, 10)

    ' Set up the location of the ListView and add some items.
    findListView.Location = New Point(10, 30)
    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.
    Me.Controls.Add(findButton)
    Me.Controls.Add(findListView)

End Sub

Private Sub findButton_Click(ByVal sender As Object, ByVal e As EventArgs) _
    Handles findButton.Click

    ' Call FindItemWithText, sending output to MessageBox.
    Dim item1 As ListViewItem = findListView.FindItemWithText("brack")
    If (item1 IsNot Nothing) Then
        MessageBox.Show("Calling FindItemWithText passing 'brack': " _
            & item1.ToString())
    Else
        MessageBox.Show("Calling FindItemWithText passing 'brack': null")
    End If

End Sub


C#

		// Declare the ListView and Button for the example.
        ListView findListView = new ListView();
        Button findButton = new Button();

        private void InitializeFindListView()
        {
	    // Set up the location and event handling for the button.
            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, 30);
            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");
		 }


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also

Reference