Reading from a ListBox
-
Hi. I have a listbox named lbDat. I want to read values from the listbox by using the line
lbDat.SelectedItem.ToString();
But it just gives me the namespace of my solution and a class name. What am i doing wrong and what can i do to correct this behaviour. Thank you in advance.
Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.
-
Hi. I have a listbox named lbDat. I want to read values from the listbox by using the line
lbDat.SelectedItem.ToString();
But it just gives me the namespace of my solution and a class name. What am i doing wrong and what can i do to correct this behaviour. Thank you in advance.
Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.
ToString on an object will get you the object type unless you override the ToString method. If the item with in the listbox is already a string then there is no need for the ToString. If the item is not a string you should cast it to what object type it is then access the properties you need.
var castedObject = (CustomObject)lbDat.SelectedItem;
var dataInObject = castedObject.SomeProperty;Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
Hi. I have a listbox named lbDat. I want to read values from the listbox by using the line
lbDat.SelectedItem.ToString();
But it just gives me the namespace of my solution and a class name. What am i doing wrong and what can i do to correct this behaviour. Thank you in advance.
Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.
-
Hi. I have a listbox named lbDat. I want to read values from the listbox by using the line
lbDat.SelectedItem.ToString();
But it just gives me the namespace of my solution and a class name. What am i doing wrong and what can i do to correct this behaviour. Thank you in advance.
Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.
As another alternative; override the .ToString function of the class that you're stuffing in there (identified by the namespace and classname showing currently) and have it return a formatted string with the most interesting properties of the "
this
class", somewhat similar to the example below;public override string ToString()
{
return string.Format("Class myClassName, id: {0}, name: {1}", this.someId, this.someName);
}Overriding that method has the bonus-advantage you have a string-representation in the debugger for your object.
Bastard Programmer from Hell :suss: