Microsoft KB Archive/814317

= The Find method searches the whole string of the RichTextBox control if the start parameter is equal to the end parameter =

Article ID: 814317

Article Last Modified on 11/13/2007

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



SYMPTOMS
The Find method of the RichTextBox control searches for the text that is specified in the str parameter, and then returns the first character location of the search string in the control. When the Find method searches for a string that is not in the text of the control, the Find property returns -1. When you use an overloaded RichTextBox control Find method as RichTextBox1.Find (string, start, end, options), and you specify the same value for the start parameter and the end parameter, the Find method searches all the text starting from the zero position. Therefore, the Find method returns the position of the search string that starts at the zero position. Similar behavior occurs when you set the start position of the RichTextBox.Find method to RichTextBox.Text.Length.



CAUSE
When you set the start parameter as RichTextBox.Text.Length, or you set the start parameter and end parameter to the same value, the common language runtime internally calls the Find method (string, RichTextBox.Text.Length, RichTextBox.Text.Length, options). When you set the start and end parameters to the length of the text, the Find method searches all the text in the RichTextBox control. When you call an overloaded Find method (string, start, options), the common language runtime internally calls the Find method (string, start, -1, options). Similarly, when you call an overloaded Find method (charset, start), the common language runtime calls Find (charset, start, -1). Therefore, all the text is searched.



WORKAROUND
To work around this problem, set the end parameter to a value that is one greater than the start parameter while you call the Find method. For example, do not use the following call to the Find method: Find(String, RichTextBox.Text.Length, options) Instead, use this Find method code: Find(String, RichTextBox.Text.Length, RichTextBox.Text.Length+1, options)



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Create a new Windows application project by using Visual Basic 2005, Visual Basic .NET, Visual C# 2005, or Visual C# .NET.

By default, Form1 is created. Insert a RichTextBox control and a Button control on Form1.  Double-click Button1, and then add the following code in the Button1_Click event:

Visual Basic .NET or Visual Basic 2005 Dim iFound As Integer RichTextBox1.Text = &quot;Where there is a will there is way&quot; iFound = RichTextBox1.Find(&quot;is&quot;, RichTextBox1.Text.Length, RichTextBox1.Text.Length, RichTextBoxFinds.None) MessageBox.Show(iFound.ToString) Visual C# .NET or Visual C# 2005 int iFound; richTextBox1.Text = &quot;Where there is a will there is way&quot;; iFound = richTextBox1.Find(&quot;is&quot;, richTextBox1.Text.Length, richTextBox1.Text.Length, RichTextBoxFinds.None); MessageBox.Show(iFound.ToString);  On the Debug menu, click Start to run the application. Click Button1. Verify that the Find method searches all the text in the RichTextBox control.</ol>

<div class="references_section">