We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,478 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

[C#] Problem with getting ListBoxItem

Hello.
I have some problems with recieving text from ListBoxItem. My code is below:

ListBox list = (ListBox)this.FindName(e.Parameter.ToString());
ListBoxItem item = (ListBoxItem)list.Items.GetItemAt(0); //****here is exception****
T.Text = item.Content.ToString();
list.Items.RemoveAt(0);

The exception throws:

Unable to cast object of type 'System.String' to type 'System.Windows.Controls.ListBoxItem'.

My ListBox contains numbers, but it does not matter. I just wanna get a handle to first element on list.

2
Contributors
2
Replies
15 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
fka
Newbie Poster
19 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You may think it holds numbers, but it is telling you that it holds strings (The System.String part of the error). You are trying to convert this string into a ListBoxItem, which it cannot do.

Based on the next few lines, all you need to do is assign the value to T.Text and skip the attempt to convert it to a ListBoxItem.

Momerath
Senior Poster
3,730 posts since Aug 2010
Reputation Points: 1,336
Solved Threads: 624
Skill Endorsements: 13

This problem is solved. I was adding Strings and reading ListBoxItem types.

fka
Newbie Poster
19 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0624 seconds using 2.69MB