BUG: Error message when you use the FindString method or the FindStringExact method in Visual Basic .NET or in Visual C# .NET: "System.ArgumentOutOfRangeException" (820634)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

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 %Form Path%\Form Name:line 83
Note Form Name 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, %Form Path% 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 "Symptoms" 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 "More Information" section.

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

    Form1 appears.
  6. 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.
  7. Click OK to close each message box. The next message box automatically appears.
Note These steps are based on the sample from the "More Information" 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
  1. Run Visual Basic .NET. Open Form1.vb, and then replace
    ReturnedIndex = ListBox1.FindString("One", 3)
    with the following code:
    ReturnedIndex = ListBox1.FindString("One")
    Run Visual C# .NET. Open Form1.cs, and then replace
    ReturnedIndex = listBox1.FindString("One", 3);
    with the following code:
    ReturnedIndex = listBox1.FindString("One");
  2. In Visual Basic .NET, replace
    ReturnedIndex = ListBox1.FindStringExact("One", 3)
    with the following code:
    ReturnedIndex = ListBox1.FindStringExact("One")
    In Visual C# .NET, replace
    ReturnedIndex = listBox1.FindStringExact("One", 3);
    with the following code:
    ReturnedIndex = listBox1.FindStringExact("One");
  3. In Visual Basic .NET, replace
    ReturnedIndex = ComboBox1.FindString("One", 3)
    with the following code:
    ReturnedIndex = ComboBox1.FindString("One")
    In Visual C# .NET, replace
    ReturnedIndex = comboBox1.FindString("One", 3);
    with the following code:
    ReturnedIndex = comboBox1.FindString("One");
  4. In Visual Basic .NET, replace
    ReturnedIndex = ComboBox1.FindStringExact("One", 3)
    with the following code:
    ReturnedIndex = ComboBox1.FindStringExact("One")
    In Visual C# .NET, replace
    ReturnedIndex = comboBox1.FindStringExact("One", 3);
    with the following code:
    ReturnedIndex = comboBox1.FindStringExact("One");
  5. On the Debug menu, click Start to run the application.

    Form1 appears.
  6. 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.
  7. Click OK to close each message box. The next message box automatically appears.
Note These steps are based on the sample from the "More Information" section of this article. Therefore, the code and the file names in the steps may differ from your code and your file names.

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 Problem

  1. 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.
  2. In the Toolbox, double-click ListBox.

    In Visual Basic .NET, ListBox1 is added to Form1. In Visual C# .NET, listBox1 is added to Form1.
  3. 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.
  4. 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
  5. In the Toolbox, double-click ComboBox.

    In Visual Basic .NET, ComboBox1 is added to Form1. In Visual C# .NET, comboBox1 is added to Form1.
  6. 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.
  7. 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
  8. In the Toolbox, double-click Button.

    In Visual Basic .NET, Button1 is added to Form1. In Visual C# .NET, button1 is added to Form1.
  9. In Visual Basic .NET, add the following code to the Button1_Click event handler:
    Dim ReturnedIndex As Integer
    Try
       ReturnedIndex = ListBox1.FindString("One", 3)
       MessageBox.Show(ReturnedIndex)
    Catch MyException As Exception
       MessageBox.Show(MyException.ToString())
    End Try
    Try
       ReturnedIndex = ListBox1.FindStringExact("One", 3)
       MessageBox.Show(ReturnedIndex)
    Catch MyException As Exception
       MessageBox.Show(MyException.ToString())
    End Try
    Try
       ReturnedIndex = ComboBox1.FindString("One", 3)
       MessageBox.Show(ReturnedIndex)
    Catch MyException As Exception
       MessageBox.Show(MyException.ToString())
    End Try
    Try
       ReturnedIndex = ComboBox1.FindStringExact("One", 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("One", 3);
       MessageBox.Show(ReturnedIndex.ToString());
    }
    catch(Exception MyException)
    {
       MessageBox.Show(MyException.ToString());
    }
    try
    {
       ReturnedIndex = listBox1.FindStringExact("One", 3);
       MessageBox.Show(ReturnedIndex.ToString());
    }
    catch(Exception MyException)
    {
       MessageBox.Show(MyException.ToString());
    }
    try
    {
       ReturnedIndex = comboBox1.FindString("One", 3);
       MessageBox.Show(ReturnedIndex.ToString());
    }
    catch(Exception MyException)
    {
       MessageBox.Show(MyException.ToString());
    }
    try
    {
       ReturnedIndex = comboBox1.FindStringExact("One", 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.
  10. On the Debug menu, click Start to run the application.

    Form1 appears.
  11. 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 "Symptoms" section of this article.
  12. Click OK to close each message box. The next message box automatically appears.

Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbStringEditor kbString kbExceptHandling kbListBox kbCtrl kbControl kbComboBox kberrmsg kbWindowsForms kbForms kbProgramming kbbug KB820634 kbAudDeveloper