Click here to Skip to main content
11,790,613 members (59,489 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Silverlight
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
Edited 21-Mar-13 0:31am
Richard MacCutchan at 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 at 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 Wink | ;)

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

  Print Answers RSS
0 OriginalGriff 1,154
1 Maciej Los 825
2 KrunalRohit 686
3 ppolymorphe 660
4 CPallini 621

Advertise | Privacy | Mobile
Web01 | 2.8.1509028.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2015
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