Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Bug - Getting the SelectedItem in ListBox
 

Bug - Getting the SelectedItem in ListBox

Hi

I added the items to the ListBox as the following

Code Snippet

lst.BeginUpdate();

lst.DisplayMember = "Name";

lst.ValueMember = "ID";

foreach (MyObject d in MyList)

lst.Items.Add(d);

lst.EndUpdate();

To get the selected item I did the following

Code Snippet

1) MyObject obj = (MyObject )lst.Items[lst.SelectedIndex];

2) string s = obj.Name;

3) txt.Text = s;

and now when it gets to line 3 the textbox gets the content of "s" andthe it jumps back to line 1 and Ithe error"Unable to cast object of type 'System.String' to type 'MyObject'."

Is this a bug or my head is full with bugs?

Itzik Kramer  Thursday, February 28, 2008 9:24 AM
Sorry, I found the problem it's my bug

Itzik Kramer  Thursday, February 28, 2008 9:46 AM

HI,

What is the datasource?Send your full code.

Satya

Satya Here  Thursday, February 28, 2008 9:39 AM

I didn't set the datasource, Look at the first code block

Thanks

Itzik Kramer  Thursday, February 28, 2008 9:42 AM
Sorry, I found the problem it's my bug

Itzik Kramer  Thursday, February 28, 2008 9:46 AM

You can use google to search for other answers

Custom Search

More Threads

• groupbox heading text font changeable or not?
• Design-time error E_UNEXPECTED trying to connect to Access database
• How to: design-time or run-time (again)
• [C++] Moving pictures inside a Form ?!
• Not being able to drag and drop
• Access events of an object from another class which declared on a form
• windows controls limit on a form??
• DefaultValue not recognized by designer
• Faster way to display thumbnails in Windows Forms with C#
• dynamically sizing a datagrid view for scroll bars