Editing items text an Listbox

D

David Ichilov

I've class derived from "Object" class, with ToString() method overriden
itc. ,
now i add object of this class to Windows.Forms.ListBox.Items
collection, and it works fine, displaying what ToString() should return...

then the program is in the runtime, i chage fields of this object in thet
way ( (MyClass)ListBox.SelectedItem ).SomeProperty = newValue;
so ToString() method now will return new value, but text showed in a listbox
itself not changes, i tried to call ListBox.Update(), adn ListBox.Text = "",
but nothing worked...
What can i do?
 
P

PL Patrick

Hi,

int selectedIndex = ListBox.SelectedIndex;
MyClass selectedItem = (MyClass)ListBox.SelectedItem;
.....
selectedItem.Property = newProperty;
.....
ListBox.Items[selectedIndex] = selectedItem;
 
D

David Ichilov

i've done like you proposed, it's my code:

CheesDrawing.Player.csPlayer Player =
(CheesDrawing.Player.csPlayer)lstPlayers.SelectedItem;
Player.Name = txtName.Text;
lstPlayers.Items[lstPlayers.SelectedIndex] = Player;

third line throws exeption:
"object reference not set to an instance of an object"



PL Patrick said:
Hi,

int selectedIndex = ListBox.SelectedIndex;
MyClass selectedItem = (MyClass)ListBox.SelectedItem;
....
selectedItem.Property = newProperty;
....
ListBox.Items[selectedIndex] = selectedItem;

I've class derived from "Object" class, with ToString() method overriden
itc. ,
now i add object of this class to Windows.Forms.ListBox.Items
collection, and it works fine, displaying what ToString() should return...

then the program is in the runtime, i chage fields of this object in thet
way ( (MyClass)ListBox.SelectedItem ).SomeProperty = newValue;
so ToString() method now will return new value, but text showed in a listbox
itself not changes, i tried to call ListBox.Update(), adn ListBox.Text = "",
but nothing worked...
What can i do?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top