Click here to Skip to main content
15,799,398 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Updated 21-Mar-13 1: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.....

Add the following if statement before your code:
if(listbox1.SelectedIndex >= 0)
... // your normal code here
Share this answer
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 ;)
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900