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 (814318)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

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 "abcde", string position zero indicates that the string starts from "a". 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 = "abcde"

      ' 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("'start' should be between 0 and 4.")
      Else
         position = RichTextBox1.Find("bc", startParam, RichTextBoxFinds.None)
      End If

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

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

      if (startParam >= RichTextBox1.Text.Length)
      {
         throw new ArgumentException("'start' should be between 0 and 4.");
      }
      else
      {
         position = RichTextBox1.Find("bc", 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.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Windows application by using Visual Basic .NET or Visual C# .NET.

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

    Visual Basic .NET Code
       ' Assign some value
       RichTextBox1.Text = "abcde"
    
       ' Set start parameter as length of the string
       Dim position As Integer = RichTextBox1.Find("cd", 5, RichTextBoxFinds.None)
       MsgBox(position)
    Visual C# .NET Code
       // Assign some value
       richTextBox1.Text = "abcde";
    
       // Set start parameter as length of the string
       int position = richTextBox1.Find("cd", 5, RichTextBoxFinds.None);
       MessageBox.Show(position.ToString());
  5. On the Debug menu, click Start to run the application.
  6. Click Button1. Notice the value that the Find method returns.

REFERENCES

For more information, see your .NET Framework SDK Documentation or visit the following MSDN Web site:

RichTextBox Control (Windows Forms)
http://msdn.microsoft.com/library/en-us/vbcon/html/vboririchtextboxctltasks.asp

Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbBug kbRichEdit kbWindowsForms kbControl kbCtrl KB814318 kbAudDeveloper