Microsoft KB Archive/820634

= BUG: Error message when you use the FindString method or the FindStringExact method in Visual Basic .NET or in Visual C# .NET: &quot;System.ArgumentOutOfRangeException&quot; =

Article ID: 820634

Article Last Modified on 5/12/2007

-

APPLIES TO


 * 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
In Visual Basic .NET or in Visual C# .NET, you can invoke the overloaded two-argument FindString method or the overloaded two-argument FindStringExact method of the ComboBox class or the ListBox class. However, when the startIndex parameter is equal to the index value of the last item of the associated list, you may receive the following error message:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll

Additional information: Specified argument was out of the range of valid values.

If you handle this exception in a Try/Catch block, you may receive an error message similar to the following:

Unhandled Exception: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Parameter name: startIndex

at System.Windows.Forms.ListBox.FindStringExact(String s, Int32 startIndex)

at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in \ :line 83

Note is a placeholder for the file name of the Form that contains the ComboBox object or the ListBox object that invokes the FindString method or the FindStringExact method. Additionally,  is a placeholder for the path of the previously mentioned Form.



CAUSE
When you invoke either the two-argument FindString method or the two-argument FindStringExact method, a startIndex parameter is used. This startIndex parameter may be equal to the index value of the last item of the associated list for a ComboBox object or a ListBox object. In such cases, the invoked method is supposed to start searching from the beginning of the list for the string that is passed as the first parameter. However, the invoked method incorrectly begins the search after the last item of the list. Therefore, the list boundary is crossed during the search, and you receive the error message in the &quot;Symptoms&quot; section.



WORKAROUND
To work around this bug, pass 0 as the startIndex parameter for either the two-argument FindString method or the two-argument FindStringExact method. You may also use either the one-argument FindString method or the one-argument FindStringExact method instead of the corresponding two-argument methods.

To work around this problem, follow these steps:

Note These steps are for the sample that is in the &quot;More Information&quot; section.

Passing 0 as the StartIndex Parameter   Run Visual Basic .NET. Open Form1.vb and then replace ReturnedIndex = ListBox1.FindString(&quot;One&quot;, 3) with the following code: ReturnedIndex = ListBox1.FindString(&quot;One&quot;, 0) Run Visual C# .NET. Open Form1.cs and then replace ReturnedIndex = listBox1.FindString(&quot;One&quot;, 3); with the following code: ReturnedIndex = listBox1.FindString(&quot;One&quot;, 0);   In Visual Basic .NET, replace ReturnedIndex = ListBox1.FindStringExact(&quot;One&quot;, 3) with the following code: ReturnedIndex = ListBox1.FindStringExact(&quot;One&quot;, 0) In Visual C# .NET, replace ReturnedIndex = listBox1.FindStringExact(&quot;One&quot;, 3); with the following code: ReturnedIndex = listBox1.FindStringExact(&quot;One&quot;, 0);   In Visual Basic .NET, replace ReturnedIndex = ComboBox1.FindString(&quot;One&quot;, 3) with the following code: ReturnedIndex = ComboBox1.FindString(&quot;One&quot;, 0) In Visual C# .NET, replace ReturnedIndex = comboBox1.FindString(&quot;One&quot;, 3); with the following code: ReturnedIndex = comboBox1.FindString(&quot;One&quot;, 0);   In Visual Basic .NET, replace ReturnedIndex = ComboBox1.FindStringExact(&quot;One&quot;, 3) with the following code: ReturnedIndex = ComboBox1.FindStringExact(&quot;One&quot;, 0) In Visual C# .NET, replace ReturnedIndex = comboBox1.FindStringExact(&quot;One&quot;, 3); with the following code: ReturnedIndex = comboBox1.FindStringExact(&quot;One&quot;, 0);  On the Debug menu, click Start to run the application.

Form1 appears. In Visual Basic .NET, click Button1. In Visual C# .NET, click button1.

You receive a series of four message boxes with the return values of the invoked methods.</li> Click OK to close each message box. The next message box automatically appears.</li></ol>

Note These steps are based on the sample from the &quot;More Information&quot; section of this article. Therefore, the code and the file names in the steps may differ from your code and your file names.

Using the Corresponding One-Argument Methods <ol>  Run Visual Basic .NET. Open Form1.vb, and then replace ReturnedIndex = ListBox1.FindString(&quot;One&quot;, 3) with the following code: ReturnedIndex = ListBox1.FindString(&quot;One&quot;) Run Visual C# .NET. Open Form1.cs, and then replace ReturnedIndex = listBox1.FindString(&quot;One&quot;, 3); with the following code: ReturnedIndex = listBox1.FindString(&quot;One&quot;); </li>  In Visual Basic .NET, replace ReturnedIndex = ListBox1.FindStringExact(&quot;One&quot;, 3) with the following code: ReturnedIndex = ListBox1.FindStringExact(&quot;One&quot;) In Visual C# .NET, replace ReturnedIndex = listBox1.FindStringExact(&quot;One&quot;, 3); with the following code: ReturnedIndex = listBox1.FindStringExact(&quot;One&quot;); </li>  In Visual Basic .NET, replace ReturnedIndex = ComboBox1.FindString(&quot;One&quot;, 3) with the following code: ReturnedIndex = ComboBox1.FindString(&quot;One&quot;) In Visual C# .NET, replace ReturnedIndex = comboBox1.FindString(&quot;One&quot;, 3); with the following code: ReturnedIndex = comboBox1.FindString(&quot;One&quot;); </li>  In Visual Basic .NET, replace ReturnedIndex = ComboBox1.FindStringExact(&quot;One&quot;, 3) with the following code: ReturnedIndex = ComboBox1.FindStringExact(&quot;One&quot;) In Visual C# .NET, replace ReturnedIndex = comboBox1.FindStringExact(&quot;One&quot;, 3); with the following code: ReturnedIndex = comboBox1.FindStringExact(&quot;One&quot;); </li> On the Debug menu, click Start to run the application.

Form1 appears.</li> In Visual Basic .NET, click Button1. In Visual C# .NET, click button1.

You receive a series of four message boxes with the return values of the invoked methods.</li> Click OK to close each message box. The next message box automatically appears.</li></ol>

Note These steps are based on the sample from the &quot;More Information&quot; section of this article. Therefore, the code and the file names in the steps may differ from your code and your file names.

<div class="status_section">

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

<div class="moreinformation_section">

Steps to Reproduce the Problem
<ol> Run Microsoft Visual Studio .NET. Create a Windows application project that is named FindStringDemo.

You can use either Visual Basic .NET or Visual C# .NET.

By default, Form1 is created.</li> In the Toolbox, double-click ListBox.

In Visual Basic .NET, ListBox1 is added to Form1. In Visual C# .NET, listBox1 is added to Form1.</li> In Visual Basic .NET, select ListBox1. In Visual C# .NET, select listBox1. In the Properties window, select the ellipsis (...) for the Items property to view the Strings Collection Editor dialog box.</li>  Add the following text in the Enter the strings in the collection (one per line) text box and then click OK: One Two Three Four </li> In the Toolbox, double-click ComboBox.

In Visual Basic .NET, ComboBox1 is added to Form1. In Visual C# .NET, comboBox1 is added to Form1.</li> In Visual Basic .NET, select ComboBox1. In Visual C# .NET, select comboBox1. In the Properties window, select the ellipsis (...) for the Items property to view the Strings Collection Editor dialog box.</li>  Add the following text in the Enter the strings in the collection (one per line) text box and then click OK: One Two Three Four </li> In the Toolbox, double-click Button.

In Visual Basic .NET, Button1 is added to Form1. In Visual C# .NET, button1 is added to Form1.</li> <li> In Visual Basic .NET, add the following code to the Button1_Click event handler: Dim ReturnedIndex As Integer Try ReturnedIndex = ListBox1.FindString(&quot;One&quot;, 3) MessageBox.Show(ReturnedIndex) Catch MyException As Exception MessageBox.Show(MyException.ToString) End Try Try ReturnedIndex = ListBox1.FindStringExact(&quot;One&quot;, 3) MessageBox.Show(ReturnedIndex) Catch MyException As Exception MessageBox.Show(MyException.ToString) End Try Try ReturnedIndex = ComboBox1.FindString(&quot;One&quot;, 3) MessageBox.Show(ReturnedIndex) Catch MyException As Exception MessageBox.Show(MyException.ToString) End Try Try ReturnedIndex = ComboBox1.FindStringExact(&quot;One&quot;, 3) MessageBox.Show(ReturnedIndex) Catch MyException As Exception MessageBox.Show(MyException.ToString) End Try In Visual C# .NET, add the following code to the button1_Click event handler. int ReturnedIndex; try {  ReturnedIndex = listBox1.FindString(&quot;One&quot;, 3); MessageBox.Show(ReturnedIndex.ToString); } catch(Exception MyException) {  MessageBox.Show(MyException.ToString); } try {  ReturnedIndex = listBox1.FindStringExact(&quot;One&quot;, 3); MessageBox.Show(ReturnedIndex.ToString); } catch(Exception MyException) {  MessageBox.Show(MyException.ToString); } try {  ReturnedIndex = comboBox1.FindString(&quot;One&quot;, 3); MessageBox.Show(ReturnedIndex.ToString); } catch(Exception MyException) {  MessageBox.Show(MyException.ToString); } try {  ReturnedIndex = comboBox1.FindStringExact(&quot;One&quot;, 3); MessageBox.Show(ReturnedIndex.ToString); }  catch(Exception MyException) {  MessageBox.Show(MyException.ToString); } Note You may replace One with any of the other strings in the ListBox or in the ComboBox without affecting this step. </li> <li>On the Debug menu, click Start to run the application.

Form1 appears.</li> <li>In Visual Basic .NET, click Button1. In Visual C# .NET, click button1. You receive a series of four message boxes with information similar to the information mentioned in the &quot;Symptoms&quot; section of this article.</li> <li>Click OK to close each message box. The next message box automatically appears.</li></ol>

<div class="references_section">