RichTextBox.Find RichTextBox.Find RichTextBox.Find RichTextBox.Find Method

Definition

Searches for text within the contents of the RichTextBox.

Overloads

Find(String, Int32, Int32, RichTextBoxFinds) Find(String, Int32, Int32, RichTextBoxFinds) Find(String, Int32, Int32, RichTextBoxFinds) Find(String, Int32, Int32, RichTextBoxFinds)

Searches the text in a RichTextBox control for a string within a range of text within the control and with specific options applied to the search.

Find(String, Int32, RichTextBoxFinds) Find(String, Int32, RichTextBoxFinds) Find(String, Int32, RichTextBoxFinds) Find(String, Int32, RichTextBoxFinds)

Searches the text in a RichTextBox control for a string at a specific location within the control and with specific options applied to the search.

Find(Char[], Int32, Int32) Find(Char[], Int32, Int32) Find(Char[], Int32, Int32) Find(Char[], Int32, Int32)

Searches a range of text in a RichTextBox control for the first instance of a character from a list of characters.

Find(Char[]) Find(Char[]) Find(Char[]) Find(Char[])

Searches the text of a RichTextBox control for the first instance of a character from a list of characters.

Find(Char[], Int32) Find(Char[], Int32) Find(Char[], Int32) Find(Char[], Int32)

Searches the text of a RichTextBox control, at a specific starting point, for the first instance of a character from a list of characters.

Find(String) Find(String) Find(String) Find(String)

Searches the text in a RichTextBox control for a string.

Find(String, RichTextBoxFinds) Find(String, RichTextBoxFinds) Find(String, RichTextBoxFinds) Find(String, RichTextBoxFinds)

Searches the text in a RichTextBox control for a string with specific options applied to the search.

Find(String, Int32, Int32, RichTextBoxFinds) Find(String, Int32, Int32, RichTextBoxFinds) Find(String, Int32, Int32, RichTextBoxFinds) Find(String, Int32, Int32, RichTextBoxFinds)

Searches the text in a RichTextBox control for a string within a range of text within the control and with specific options applied to the search.

public:
 int Find(System::String ^ str, int start, int end, System::Windows::Forms::RichTextBoxFinds options);
public int Find (string str, int start, int end, System.Windows.Forms.RichTextBoxFinds options);
member this.Find : string * int * int * System.Windows.Forms.RichTextBoxFinds -> int
Public Function Find (str As String, start As Integer, end As Integer, options As RichTextBoxFinds) As Integer

Parameters

str
String String String String

The text to locate in the control.

start
Int32 Int32 Int32 Int32

The location within the control's text at which to begin searching.

end
Int32 Int32 Int32 Int32

The location within the control's text at which to end searching. This value must be equal to negative one (-1) or greater than or equal to the start parameter.

Returns

The location within the control where the search text was found.

Exceptions

The end parameter was less the start parameter.

Examples

The following code example searches a section of text in a RichTextBox for the first instance of a search string passed into the searchText parameter of the method. The range to search for text within the control is specified by the searchStart and searchEnd parameters of the method. If the search string is found in the RichTextBox, the method returns the index location of the first character of the found text and highlights the found text; otherwise, it returns a value of -1. The example also uses the options parameter of the Find method to specify that the found text should match the case of the search string. The example requires that this method is placed in the class of a Form that contains a RichTextBox control named richTextBox1. After the first instance of the search string has been found, you can use this example to find other instances in the text.

public:
   int FindMyText( String^ searchText, int searchStart, int searchEnd )
   {
      // Initialize the return value to false by default.
      int returnValue = -1;

      // Ensure that a search string and a valid starting point are specified.
      if ( searchText->Length > 0 && searchStart >= 0 )
      {
         // Ensure that a valid ending value is provided.
         if ( searchEnd > searchStart || searchEnd == -1 )
         {
            // Obtain the location of the search string in richTextBox1.
            int indexToText = richTextBox1->Find( searchText, searchStart, searchEnd, RichTextBoxFinds::MatchCase );

            // Determine whether the text was found in richTextBox1.
            if ( indexToText >= 0 )
            {
               // Return the index to the specified search text.
               returnValue = indexToText;
            }
         }
      }

      return returnValue;
   }
public int FindMyText(string searchText, int searchStart, int searchEnd)
{
	// Initialize the return value to false by default.
	int returnValue = -1;

	// Ensure that a search string and a valid starting point are specified.
	if (searchText.Length > 0 && searchStart >= 0) 
	{
		// Ensure that a valid ending value is provided.
		if (searchEnd > searchStart || searchEnd == -1)
		{	
			// Obtain the location of the search string in richTextBox1.
			int indexToText = richTextBox1.Find(searchText, searchStart, searchEnd, RichTextBoxFinds.MatchCase);
			// Determine whether the text was found in richTextBox1.
			if(indexToText >= 0)
			{
				// Return the index to the specified search text.
				returnValue = indexToText;
			}
		}
	}

	return returnValue;
}
Public Function FindMyText(ByVal searchText As String, ByVal searchStart As Integer, ByVal searchEnd As Integer) As Integer
    ' Initialize the return value to false by default.
    Dim returnValue As Integer = -1

    ' Ensure that a search string and a valid starting point are specified.
    If searchText.Length > 0 And searchStart >= 0 Then
        ' Ensure that a valid ending value is provided.
        If searchEnd > searchStart Or searchEnd = -1 Then
            ' Obtain the location of the search string in richTextBox1.
        Dim indexToText As Integer = richTextBox1.Find(searchText, searchStart, searchEnd, RichTextBoxFinds.MatchCase)
            ' Determine whether the text was found in richTextBox1.
            If indexToText >= 0 Then
                ' Return the index to the specified search text.
                returnValue = indexToText
            End If
        End If
    End If

    Return returnValue
End Function

Remarks

The Find method searches for the text specified in the str parameter and returns the location of the first character of the search string within the control. If the property returns a negative value, the text string being searched for was not found within the contents of the control. You can use this method to create search functionality that can be provided to the user of the control. You can also use this method to search for text to be replaced with a specific format. For example, if the user entered dates into the control, you can use the Find method to search for all dates in the document and replace them with the appropriate format before using the SaveFile method of the control.

With this version of the Find method, you can specify options that enable you to expand or narrow your search. You can specify options that enable you to match the casing of the search word or to search for entire words instead of partial words. By specifying the RichTextBoxFinds.Reverse enumeration in the options parameter, you can search for text from the bottom of the document to the top instead of the default top to bottom search method. This version of the Find method also enables you to narrow the search for text by selecting a specific start and end position within the control's text. This feature can enable you to limit the search range to a specific section of the control's text. If a value of negative one (-1) is assigned to the end parameter, the method will search until the end of the text in the RichTextBox for normal searches. For reverse searches, a value of negative one (-1) assigned to the end parameter indicates that text will be searched from the end of text (bottom) to the position defined by start parameter. When the start and end parameters are provided the same value the entire control is searched for normal searches. For a reverse search, the entire control is searched but the search begins at the bottom of the document and searches to the top of the document.

Note

The Find methods that accept a string as a parameter cannot find text that is contained on more than one line of text within the RichTextBox. Performing such a search will return a value of negative one (-1).

Find(String, Int32, RichTextBoxFinds) Find(String, Int32, RichTextBoxFinds) Find(String, Int32, RichTextBoxFinds) Find(String, Int32, RichTextBoxFinds)

Searches the text in a RichTextBox control for a string at a specific location within the control and with specific options applied to the search.

public:
 int Find(System::String ^ str, int start, System::Windows::Forms::RichTextBoxFinds options);
public int Find (string str, int start, System.Windows.Forms.RichTextBoxFinds options);
member this.Find : string * int * System.Windows.Forms.RichTextBoxFinds -> int
Public Function Find (str As String, start As Integer, options As RichTextBoxFinds) As Integer

Parameters

str
String String String String

The text to locate in the control.

start
Int32 Int32 Int32 Int32

The location within the control's text at which to begin searching.

Returns

The location within the control where the search text was found.

Examples

The following code example searches the entire contents of a RichTextBox for the first instance of a search string passed into the text parameter of the method. The search starting location is specified by the start parameter of the method. If the search string is found in the RichTextBox, the method returns the index location of the first character of the found text and highlights the found text; otherwise, it returns a value of -1. The example also specifies options in the search to match the case of the specified search string. The example requires that this method is placed in the class of a Form that contains a RichTextBox named richTextBox1. You can use this example to perform a "Find Next" type of operation. Once an instance of the search text has been found, you can find other instances of the text by changing the value of the start parameter to search at a location beyond the position of the current match.

public:
   int FindMyText( String^ text, int start )
   {
      // Initialize the return value to false by default.
      int returnValue = -1;
      
      // Ensure that a search string has been specified and a valid start point.
      if ( text->Length > 0 && start >= 0 )
      {
         // Obtain the location of the search string in richTextBox1.
         int indexToText = richTextBox1->Find( text, start, RichTextBoxFinds::MatchCase );
         // Determine whether the text was found in richTextBox1.
         if ( indexToText >= 0 )
         {
            returnValue = indexToText;
         }
      }

      return returnValue;
   }
public int FindMyText(string text, int start)
{
   // Initialize the return value to false by default.
   int returnValue = -1;

   // Ensure that a search string has been specified and a valid start point.
   if (text.Length > 0 && start >= 0) 
   {
      // Obtain the location of the search string in richTextBox1.
      int indexToText = richTextBox1.Find(text, start, RichTextBoxFinds.MatchCase);
      // Determine whether the text was found in richTextBox1.
      if(indexToText >= 0)
      {
         returnValue = indexToText;
      }
   }

   return returnValue;
}

Public Function FindMyText(text As String, start As Integer) As Integer
    ' Initialize the return value to false by default.
    Dim returnValue As Integer = - 1
    
    ' Ensure that a search string has been specified and a valid start point.
    If text.Length > 0 And start >= 0 Then
        ' Obtain the location of the search string in richTextBox1.
        Dim indexToText As Integer = richTextBox1.Find(text, start, _
            RichTextBoxFinds.MatchCase)
        ' Determine whether the text was found in richTextBox1.
        If indexToText >= 0 Then
            returnValue = indexToText
        End If
    End If
    
    Return returnValue
End Function

Remarks

The Find method searches for the text specified in the str parameter and returns the location of the first character of the search string within the control. If the property returns a negative value, the text string being searched for was not found within the contents of the control. You can use this method to create search functionality that can be provided to the user of the control. You can also use this method to search for text to be replaced with a specific format. For example, if the user entered dates into the control, you could use the Find method to search for all dates in the document and replace them with the appropriate format before using the SaveFile method of the control.

With this version of the Find method, you can specify options that enable you to expand or narrow your search. You can specify options that enable you to match the casing of the search word or to search for entire words instead of partial words. By specifying the RichTextBoxFinds.Reverse enumeration in the options parameter, you can search for text from the bottom of the document to the top instead of the default top to bottom search method. This version of the Find method also enables you to narrow the search for text by selecting a specific starting position within the control's text. This feature can enable you to avoid text that might have already been searched or where the specific text you are searching for is known not to exist. When the RichTextBoxFinds.Reverse value is specified in the options parameter, the value of the start parameter indicates the position where the reverse search will end since the search will start at the bottom of the document when using this version of the Find method.

Note

The Find methods that accept a string as a parameter cannot find text that is contained on more than one line of text within the RichTextBox. Performing such a search will return a value of negative one (-1).

Find(Char[], Int32, Int32) Find(Char[], Int32, Int32) Find(Char[], Int32, Int32) Find(Char[], Int32, Int32)

Searches a range of text in a RichTextBox control for the first instance of a character from a list of characters.

public:
 int Find(cli::array <char> ^ characterSet, int start, int end);
public int Find (char[] characterSet, int start, int end);
member this.Find : char[] * int * int -> int
Public Function Find (characterSet As Char(), start As Integer, end As Integer) As Integer

Parameters

characterSet
Char[]

The array of characters to search for.

start
Int32 Int32 Int32 Int32

The location within the control's text at which to begin searching.

end
Int32 Int32 Int32 Int32

The location within the control's text at which to end searching.

Returns

The location within the control where the search characters are found.

Exceptions

start is less than 0 or greater than the length of the text in the control.

Remarks

This version of the Find method searches for the first instance of a character from a list of characters specified in the characterSet parameter and returns the location of the character. For example, you pass an array of characters containing the character 'Q'. If the control contained the text "The Quick Brown Fox", the Find method would return the value of four. An upper case character and a lower case character are considered different values in the search.

If the property returns a negative value, the characters being searched for were not found within the contents of the control. You can use this method to search for a group of characters within the control. If a character from the character list provided in the method's characterSet parameter is found, the value returned by this method is a zero based index of the character's position in the control. A space is considered a character by the method when determining the location of a character.

This version of the Find method enables you to search for a character set from a range of text in the control by specifying a value for the start and end parameters. A value of zero for the start parameter indicates that the search should start from the beginning of the control's document. A -1 value for the end parameter indicates that the search should end at the end of the text within the control. You can use this version of the Find method to narrow your search to a specific range of text within the control to avoid searching areas of the document that are not important to the needs of your application.

Find(Char[]) Find(Char[]) Find(Char[]) Find(Char[])

Searches the text of a RichTextBox control for the first instance of a character from a list of characters.

public:
 int Find(cli::array <char> ^ characterSet);
public int Find (char[] characterSet);
member this.Find : char[] -> int
Public Function Find (characterSet As Char()) As Integer

Parameters

characterSet
Char[]

The array of characters to search for.

Returns

The location within the control where the search characters were found or -1 if the search characters are not found or an empty search character set is specified in the char parameter.

Examples

The following code example searches the contents of a RichTextBox for the characters that are passed to the method in the text parameter. If the contents of the text array are found in the RichTextBox, the method returns the index of the value that is found; otherwise, it returns -1. The example requires that this method is placed in the class of a Form that contains a RichTextBox control named richTextBox1 and a Button control, named button1, that is connected to the Click event handler defined in the example.

private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      array<Char>^temp1 = {'D','e','l','t','a'};
      MessageBox::Show( FindMyText( temp1 ).ToString() );
   }

public:
   int FindMyText( array<Char>^text )
   {
      // Initialize the return value to false by default.
      int returnValue = -1;

      // Ensure that a search string has been specified and a valid start point.
      if ( text->Length > 0 )
      {
         // Obtain the location of the first character found in the control
         // that matches any of the characters in the char array.
         int indexToText = richTextBox1->Find( text );

         // Determine whether the text was found in richTextBox1.
         if ( indexToText >= 0 )
         {
            // Return the location of the character.
            returnValue = indexToText;
         }
      }

      return returnValue;
   }
private void button1_Click(object sender, System.EventArgs e)
{
	MessageBox.Show(FindMyText(new char[]{'D','e','l','t','a'}).ToString());
}

public int FindMyText(char[] text)
{
	// Initialize the return value to false by default.
	int returnValue = -1;

	// Ensure that a search string has been specified and a valid start point.
	if (text.Length > 0) 
	{
		// Obtain the location of the first character found in the control
		// that matches any of the characters in the char array.
		int indexToText = richTextBox1.Find(text);
		// Determine whether the text was found in richTextBox1.
		if(indexToText >= 0)
		{
			// Return the location of the character.
			returnValue = indexToText;
		}
	}

	return returnValue;
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
    MessageBox.Show(FindMyText(New Char() {"B"c, "r"c, "a"c, "v"c, "o"c}).ToString())
End Sub


Public Function FindMyText(ByVal [text]() As Char) As Integer
    ' Initialize the return value to false by default.
    Dim returnValue As Integer = -1

    ' Ensure that a search string has been specified and a valid start point.
    If [text].Length > 0 Then
        ' Obtain the location of the first character found in the control
        ' that matches any of the characters in the char array.
        Dim indexToText As Integer = richTextBox1.Find([text])
        ' Determine whether the text was found in richTextBox1.
        If indexToText >= 0 Then
            ' Return the location of the character.
            returnValue = indexToText
        End If
    End If

    Return returnValue
End Function

Remarks

This version of the Find method searches for the first instance of a character from a list of characters specified in the characterSet parameter and returns the location of the character. For example, you pass an array of characters containing the character 'Q'. If the control contained the text "The Quick Brown Fox", the Find method would return the value of four. An upper case character and a lower case character are considered different values in the search.

If the property returns a negative value, the characters being searched for were not found within the contents of the control. You can use this method to search for a group of characters within the control. This version of the Find method requires that the entire document contained in the control is searched for the characters. If a character from the character list provided in the method's characterSet parameter is found, the value returned by this method is a zero-based index of the character's position in the control. A space is considered a character by the method when determining the location of a character.

Find(Char[], Int32) Find(Char[], Int32) Find(Char[], Int32) Find(Char[], Int32)

Searches the text of a RichTextBox control, at a specific starting point, for the first instance of a character from a list of characters.

public:
 int Find(cli::array <char> ^ characterSet, int start);
public int Find (char[] characterSet, int start);
member this.Find : char[] * int -> int
Public Function Find (characterSet As Char(), start As Integer) As Integer

Parameters

characterSet
Char[]

The array of characters to search for.

start
Int32 Int32 Int32 Int32

The location within the control's text at which to begin searching.

Returns

The location within the control where the search characters are found.

Examples

The following code example searches the contents of a RichTextBox for the characters that are passed to the method in the text parameter. The search begins from the location within the RichTextBox specified by the start parameter of the FindMyText method. If the contents of the text array are found in the RichTextBox, the method returns the index of the value that is found; otherwise, it returns -1. The example requires that this method is placed in the class of a Form that contains a RichTextBox control named richTextBox1 and a Button control named button1 that is connected to the Click event handler defined in the example.

private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      array<Char>^temp0 = {'B','r','a','v','o'};
      MessageBox::Show( FindMyText( temp0, 5 ).ToString() );
   }

public:
   int FindMyText( array<Char>^text, int start )
   {
      // Initialize the return value to false by default.
      int returnValue = -1;

      // Ensure that a valid char array has been specified and a valid start point.
      if ( text->Length > 0 && start >= 0 )
      {
         // Obtain the location of the first character found in the control
         // that matches any of the characters in the char array.
         int indexToText = richTextBox1->Find( text, start );

         // Determine whether any of the chars are found in richTextBox1.
         if ( indexToText >= 0 )
         {
            // Return the location of the character.
            returnValue = indexToText;
         }
      }

      return returnValue;
   }
private void button1_Click(object sender, System.EventArgs e)
{
	MessageBox.Show(FindMyText(new char[]{'B','r','a','v','o'}, 5).ToString());
}

public int FindMyText(char[] text, int start)
{
	// Initialize the return value to false by default.
	int returnValue = -1;

	// Ensure that a valid char array has been specified and a valid start point.
	if (text.Length > 0 && start >= 0) 
	{
		// Obtain the location of the first character found in the control
		// that matches any of the characters in the char array.
		int indexToText = richTextBox1.Find(text, start);
		// Determine whether any of the chars are found in richTextBox1.
		if(indexToText >= 0)
		{
			// Return the location of the character.
			returnValue = indexToText;
		}
	}

	return returnValue;
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
    MessageBox.Show(FindMyText(New Char() {"B"c, "r"c, "a"c, "v"c, "o"c}, 5).ToString())
End Sub


Public Function FindMyText(ByVal text() As Char, ByVal start As Integer) As Integer
    ' Initialize the return value to false by default.
    Dim returnValue As Integer = -1

    ' Ensure that a valid char array has been specified and a valid start point.
    If [text].Length > 0 And start >= 0 Then
        ' Obtain the location of the first character found in the control
        ' that matches any of the characters in the char array.
        Dim indexToText As Integer = richTextBox1.Find([text], start)
        ' Determine whether any of the chars are found in richTextBox1.
        If indexToText >= 0 Then
            ' Return the location of the character.
            returnValue = indexToText
        End If
    End If

    Return returnValue
End Function

Remarks

This version of the Find method searches for the first instance of a character from a list of characters specified in the characterSet parameter and returns the location the character. For example, you pass an array of characters containing the character 'Q'. If the control contained the text "The Quick Brown Fox", the Find method would return the value of four. An upper case character and a lower case character are considered different values in the search.

If the property returns a negative value, the characters being searched for were not found within the contents of the control. You can use this method to search for a group of characters within the control. If a character from the character list provided in the method's characterSet parameter is found, the value returned by this method is a zero-based index of the character's position in the control. A space is considered a character by the method when determining the location of a character.

This version of the Find method enables you to search for a character set from a specified start position within the text of the control by specifying a value for the start parameter. A value of zero indicates that the search should start from the beginning of the control's document. You can use this version of the Find method to narrow your search to avoid text that you already know does not contain the specified characters you are searching for or are not important in your search.

Find(String) Find(String) Find(String) Find(String)

Searches the text in a RichTextBox control for a string.

public:
 int Find(System::String ^ str);
public int Find (string str);
member this.Find : string -> int
Public Function Find (str As String) As Integer

Parameters

str
String String String String

The text to locate in the control.

Returns

The location within the control where the search text was found or -1 if the search string is not found or an empty search string is specified in the str parameter.

Examples

The following code example searches the entire contents of a RichTextBox for the first instance of a search string passed into the text parameter of the method. If the search string is found in the RichTextBox, the method returns a value of true and highlights the search text, otherwise it returns false. The example requires that this method is placed in the class of a Form that contains a RichTextBox named richTextBox1.

public:
   bool FindMyText( String^ text )
   {
      // Initialize the return value to false by default.
      bool returnValue = false;
      
      // Ensure a search string has been specified.
      if ( text->Length > 0 )
      {
         // Obtain the location of the search string in richTextBox1.
         int indexToText = richTextBox1->Find( text );
         // Determine whether the text was found in richTextBox1.
         if ( indexToText >= 0 )
         {
            returnValue = true;
         }
      }

      return returnValue;
   }
public bool FindMyText(string text)
{
   // Initialize the return value to false by default.
   bool returnValue = false;

   // Ensure a search string has been specified.
   if (text.Length > 0) 
   {
      // Obtain the location of the search string in richTextBox1.
      int indexToText = richTextBox1.Find(text);
      // Determine whether the text was found in richTextBox1.
      if(indexToText >= 0)
      {
         returnValue = true;
      }
   }

   return returnValue;
}

Public Function FindMyText(text As String) As Boolean
    ' Initialize the return value to false by default.
    Dim returnValue As Boolean = False
    
    ' Ensure a search string has been specified.
    If text.Length > 0 Then
        ' Obtain the location of the search string in richTextBox1.
        Dim indexToText As Integer = richTextBox1.Find(text)
        ' Determine whether the text was found in richTextBox1.
        If indexToText >= 0 Then
            returnValue = True
        End If
    End If
    
    Return returnValue
End Function

Remarks

The Find method searches for the text specified in the str parameter and returns the location of the first character within the control. If the property returns a negative value, the text string being searched for was not found within the contents of the control. You can use this method to create search functionality that can be provided to the user of the control. You can also use this method to search for text to be replaced with a specific format. For example, if the user entered dates into the control, you could use the Find method to search for all dates in the document and replace them with the appropriate format before using the SaveFile method of the control.

Note

The Find methods that accept a string as a parameter cannot find text that is contained on more than one line of text within the RichTextBox. Performing such a search will return a value of negative one (-1).

Find(String, RichTextBoxFinds) Find(String, RichTextBoxFinds) Find(String, RichTextBoxFinds) Find(String, RichTextBoxFinds)

Searches the text in a RichTextBox control for a string with specific options applied to the search.

public:
 int Find(System::String ^ str, System::Windows::Forms::RichTextBoxFinds options);
public int Find (string str, System.Windows.Forms.RichTextBoxFinds options);
member this.Find : string * System.Windows.Forms.RichTextBoxFinds -> int
Public Function Find (str As String, options As RichTextBoxFinds) As Integer

Parameters

str
String String String String

The text to locate in the control.

Returns

The location within the control where the search text was found.

Examples

The following code example searches the entire contents of a RichTextBox for the first instance of a search string passed into the text parameter of the method. If the search string is found in the RichTextBox, the method returns a value of true and highlights the text; otherwise, it returns false. The example also specifies options in the search to match the case of the specified search string. The example requires that this method is placed in the class of a Form that contains a RichTextBox named richTextBox1.

public:
   bool FindMyText( String^ text )
   {
      // Initialize the return value to false by default.
      bool returnValue = false;
      
      // Ensure a search string has been specified.
      if ( text->Length > 0 )
      {
         // Obtain the location of the search string in richTextBox1.
         int indexToText = richTextBox1->Find( text, RichTextBoxFinds::MatchCase );
         // Determine if the text was found in richTextBox1.
         if ( indexToText >= 0 )
         {
            returnValue = true;
         }
      }

      return returnValue;
   }
public bool FindMyText(string text)
{
   // Initialize the return value to false by default.
   bool returnValue = false;

   // Ensure a search string has been specified.
   if (text.Length > 0) 
   {
      // Obtain the location of the search string in richTextBox1.
      int indexToText = richTextBox1.Find(text, RichTextBoxFinds.MatchCase);
      // Determine if the text was found in richTextBox1.
      if(indexToText >= 0)
      {
         returnValue = true;
      }
   }

   return returnValue;
}

Public Function FindMyText(text As String) As Boolean
    ' Initialize the return value to false by default.
    Dim returnValue As Boolean = False
    
    ' Ensure a search string has been specified.
    If text.Length > 0 Then
        ' Obtain the location of the search string in richTextBox1.
        Dim indexToText As Integer = richTextBox1.Find(text, RichTextBoxFinds.MatchCase)
        ' Determine if the text was found in richTextBox1.
        If indexToText >= 0 Then
            returnValue = True
        End If
    End If
    
    Return returnValue
End Function

Remarks

The Find method searches for the text specified in the str parameter and returns the location of the first character within the control. If the property returns a negative value, the text string being searched for was not found within the contents of the control. You can use this method to create search functionality that can be provided to the user of the control. You can also use this method to search for text to be replaced with a specific format. For example, if the user entered dates into the control, you can use the Find method to search for all dates in the document and replace them with the appropriate format before using the SaveFile method of the control.

With this version of the Find method, you can specify options that enable you to expand or narrow your search. You can specify options that enable you to match the casing of the search word or to search for entire words instead of partial words. By specifying the RichTextBoxFinds.Reverse enumeration in the options parameter, you can search for text from the bottom of the document to the top instead of the default top to bottom search method.

Note

The Find methods that accept a string as a parameter cannot find text that is contained on more than one line of text within the RichTextBox. Performing such a search will return a value of negative one (-1).

Applies to