Click here to Skip to main content
13,055,047 members (54,327 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello everyone,

I am working with listbox and image controls in silverlight 5. Trying to display a image in image control by selecting a image name in the listbox. Below is the code using in listbox_selection changed event. What could be the reason for this exception.

ImgesList.Source = 
new BitmapImage(new Uri(App.Current.Host.Source.OriginalString.Substring(0, App.Current.Host.Source.OriginalString.IndexOf("ClientBin")) + "Images/" + listBox1.SelectedItem));


Length cannot be less than zero. Parameter name: length

Thanks in advance.
Posted 20-Mar-13 22:28pm
Updated 21-Mar-13 0:31am
Richard MacCutchan 21-Mar-13 5:08am
The parameter name length does not appear in those two lines of code. Go back and check exactly where the exception is occurring.
bapu_reddy 21-Mar-13 7:18am
This is the only statement using in "Listbox_selectionchanged" event, where do I go back and check.....
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Add the following if statement before your code:
if(listbox1.SelectedIndex >= 0)
... // your normal code here
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

This error occurs if the lenght of string in the method "Substring" is negative.
You can catch this exception like this:

try {
   string str = App.Current.Host.Source.OriginalString.Substring(0, App.Current.Host.Source.OriginalString.IndexOf("ClientBin");
catch (ArgumentOutOfRangeException e) {

That means, check whether your string have "ClientBin" at all:

int n = App.Current.Host.Source.OriginalString.IndexOf("ClientBin");
if (n > -1)

or something like this ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100