Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Collection as DataSource in ListBox
Message
From
28/02/2005 15:50:28
 
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00990658
Message ID:
00991353
Views:
17
>>to the listbox.SelectedIndexChanged. Apparently the event fires when I set the DataSource to null<

Sorry, Einar ... I had to leave for lunch, right in the middle of our discussion!! In my last post, I was going to mention something similar along these lines, but didn't have time to post it before getting dragged off to lunch.

In SelectedIndexChanged event handlers, I always bracket my code in an "if (this.MyControl.SelectedIndex > -1)", which would amount to the same thing (in this case anyway), of you checking for a null DataSource.

What's strange though, as you said, is that it didn't throw any kind of exceptions when you tried to access an Item in your collection with a -1 indexer. Weird.

But, anywho ... glad you figured it out!!

~~Bonnie




>Bonnie,
>I think I have found an explanation now. I added the following code:
>
>if (this.lstIndividualDelay.DataSource == null)
>			{
>				MessageBox.Show("null");
>			}
>			else
>			{
>				MessageBox.Show("not null");
>				this.txtIncludeDelay.Text = this.oIndividualDelayCollection[this.lstIndividualDelay.SelectedIndex].IncludeDelay.ToString();
>				this.txtIncludeFile.Text = this.oIndividualDelayCollection[this.lstIndividualDelay.SelectedIndex].IncludeFile.ToString();
>
>			}
>
>to the listbox.SelectedIndexChanged. Apparently the event fires when I set the DataSource to null, and that screws things up without notifying me that things are screwed up. Checking if datasource is null or not before changing the textbox.text property based on the selected index makes a lot of sense.
>After discovering this the problem was very simple but I am surprised that exceptions where not thrown.
>Ohh well.
>
>Einar
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform