Microsoft KB Archive/814318

= BUG: The Find method of the RichTextBox control searches text from the zero position when the Start parameter is equal to the length of the string =

Article ID: 814318

Article Last Modified on 5/11/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 .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



SYMPTOMS
When the Start parameter is equal to the length of the string, the Find method of the RichTextBox control does not raise an exception, even though the string positional count starts with zero. For example, if the string is, string position zero indicates that the string starts from. When the Start parameter is set to the string length, RichTextBox searches the string from the zero position, and then returns the first instance position of the search string.



CAUSE
This bug occurs because the Find method of the RichTextBox control does not handle the upper boundary condition (RichTextBox.Text.Length).



RESOLUTION
Before you run the Find method, determine if the Start parameter is greater than the length of the string. If the Start parameter is greater, raise an exception or else you call the Find method. To do this, use the following code:

Visual Basic .NET Code ' Assign some value RichTextBox1.Text = &quot;abcde&quot;

' Set start parameter as length of the string Dim position As Integer Dim startParam As Integer startParam = 6

If startParam >= RichTextBox1.Text.Length Then Throw New ArgumentException(&quot;'start' should be between 0 and 4.&quot;) Else position = RichTextBox1.Find(&quot;bc&quot;, startParam, RichTextBoxFinds.None) End If

MsgBox(position) Visual C# .NET Code // Assign some value RichTextBox1.Text = &quot;abcde&quot;;

// Set start parameter as length of the string int position; int startParam; startParam = 6;

if (startParam >= RichTextBox1.Text.Length) {        throw new ArgumentException(&quot;'start' should be between 0 and 4.&quot;); }     else {        position = RichTextBox1.Find(&quot;bc&quot;, startParam, RichTextBoxFinds.None); }     MessageBox.Show(position.ToString);



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



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

By default, Form1 is created. From the toolbox, drag a RichTextBox control onto Form1. Drag a Button control to Form1.  Add the following code to the Button1_Click event:

Visual Basic .NET Code ' Assign some value RichTextBox1.Text = &quot;abcde&quot;

' Set start parameter as length of the string Dim position As Integer = RichTextBox1.Find(&quot;cd&quot;, 5, RichTextBoxFinds.None) MsgBox(position) Visual C# .NET Code // Assign some value richTextBox1.Text = &quot;abcde&quot;;

// Set start parameter as length of the string int position = richTextBox1.Find(&quot;cd&quot;, 5, RichTextBoxFinds.None); MessageBox.Show(position.ToString);  On the Debug menu, click Start to run the application. Click Button1. Notice the value that the Find method returns.</li></ol>

<div class="references_section">