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



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 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

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.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. 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.
  2. Insert a RichTextBox control and a Button control on Form1.
  3. 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 = "Where there is a will there is way"
       iFound = RichTextBox1.Find("is", RichTextBox1.Text.Length, RichTextBox1.Text.Length, RichTextBoxFinds.None)
       MessageBox.Show(iFound.ToString())
    Visual C# .NET or Visual C# 2005
       int iFound;
       richTextBox1.Text = "Where there is a will there is way";
       iFound = richTextBox1.Find("is", richTextBox1.Text.Length, richTextBox1.Text.Length, RichTextBoxFinds.None);
       MessageBox.Show(iFound.ToString());
  4. On the Debug menu, click Start to run the application.
  5. Click Button1. Verify that the Find method searches all the text in the RichTextBox control.

REFERENCES

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

RichTextBox.Find Method
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformsrichtextboxclassfindtopic.asp

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbWindowsForms kbControl kbCtrl kbprb KB814317 kbAudDeveloper