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)
SYMPTOMSIn 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. CAUSEWhen 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.WORKAROUNDTo 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- 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 replaceReturnedIndex = listBox1.FindString("One", 3); with the following code:ReturnedIndex = listBox1.FindString("One", 0); - In Visual Basic .NET, replace
ReturnedIndex = ListBox1.FindStringExact("One", 3) with the following code:ReturnedIndex = ListBox1.FindStringExact("One", 0) In Visual C# .NET, replaceReturnedIndex = listBox1.FindStringExact("One", 3); with the following code:ReturnedIndex = listBox1.FindStringExact("One", 0); - In Visual Basic .NET, replace
ReturnedIndex = ComboBox1.FindString("One", 3) with the following code:ReturnedIndex = ComboBox1.FindString("One", 0) In Visual C# .NET, replaceReturnedIndex = comboBox1.FindString("One", 3); with the following code:ReturnedIndex = comboBox1.FindString("One", 0); - In Visual Basic .NET, replace
ReturnedIndex = ComboBox1.FindStringExact("One", 3) with the following code:ReturnedIndex = ComboBox1.FindStringExact("One", 0) In Visual C# .NET, replaceReturnedIndex = comboBox1.FindStringExact("One", 3); with the following code:ReturnedIndex = comboBox1.FindStringExact("One", 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. - 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- 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 replaceReturnedIndex = listBox1.FindString("One", 3); with the following code:ReturnedIndex = listBox1.FindString("One"); - In Visual Basic .NET, replace
ReturnedIndex = ListBox1.FindStringExact("One", 3) with the following code:ReturnedIndex = ListBox1.FindStringExact("One") In Visual C# .NET, replaceReturnedIndex = listBox1.FindStringExact("One", 3); with the following code:ReturnedIndex = listBox1.FindStringExact("One"); - In Visual Basic .NET, replace
ReturnedIndex = ComboBox1.FindString("One", 3) with the following code:ReturnedIndex = ComboBox1.FindString("One") In Visual C# .NET, replaceReturnedIndex = comboBox1.FindString("One", 3); with the following code:ReturnedIndex = comboBox1.FindString("One"); - In Visual Basic .NET, replace
ReturnedIndex = ComboBox1.FindStringExact("One", 3) with the following code:ReturnedIndex = ComboBox1.FindStringExact("One") In Visual C# .NET, replaceReturnedIndex = comboBox1.FindStringExact("One", 3); with the following code:ReturnedIndex = comboBox1.FindStringExact("One"); - 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. - 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.
Modification Type: | Minor | Last Reviewed: | 2/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kbStringEditor kbString kbExceptHandling kbListBox kbCtrl kbControl kbComboBox kberrmsg kbWindowsForms kbForms kbProgramming kbbug KB820634 kbAudDeveloper |
---|
|