Bug 59865 - ListBox throws ArgumentOutOfRangeException on EnsureVisible when height is less than ItemHeight
Summary: ListBox throws ArgumentOutOfRangeException on EnsureVisible when height is le...
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: Windows.Forms ()
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2017-09-29 18:06 UTC by Alex Vallat
Modified: 2017-09-29 18:06 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

Minimal test case (352 bytes, text/plain)
2017-09-29 18:06 UTC, Alex Vallat

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 59865 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Alex Vallat 2017-09-29 18:06:43 UTC
Created attachment 25039 [details]
Minimal test case

Description of Problem:
System.Windows.Forms.ListBox will throw an ArgumentOutOfRangeException in GetItemRectangle if EnsureVisible is called when the client-height of the ListBox is smaller than the ItemHeight and there is a single item in the list. MS .net does not exhibit this behaviour.

Steps to reproduce the problem:
1. See attached minimal test case

Actual Results:
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: GetItemRectangle index out of range.
  at System.Windows.Forms.ListBox.GetItemRectangle

Expected Results:
No exception

How often does this happen? 
Reliably reproducible

Additional Information:
In the EnsureVisible method of ListBox there is a code path for when index is not less than top_index, and the ListBox is not multicolumn (https://github.com/mono/mono/blob/c6d92b9983868483ec4b39b83341ffb413ad74d6/mcs/class/System.Windows.Forms/System.Windows.Forms/ListBox.cs#L2106)

For a single item, index is 0, top_index is 0. If the height is small enough that rows will also be calculated as 0 then the logic sets top_index = 1, which is out of range of the number of items in the list (1).