Overriding ToString
-
I'm adding objects of a custom class to a check list box and when I call the
Add ()
method of the check list box the string it adds is simply namespace.classname. I don't want it to add this I want it to add a string variable that is declared in the class. I tried overriding theToString ()
method because I thought it might be calling that, but it still added the same thing. Does anyone know what method I can override so that it adds the string I want. Thanks again. - monrobot13Why don't you add that string to the list box instead of adding the object that contains it?
-
I'm adding objects of a custom class to a check list box and when I call the
Add ()
method of the check list box the string it adds is simply namespace.classname. I don't want it to add this I want it to add a string variable that is declared in the class. I tried overriding theToString ()
method because I thought it might be calling that, but it still added the same thing. Does anyone know what method I can override so that it adds the string I want. Thanks again. - monrobot13 -
Why don't you add that string to the list box instead of adding the object that contains it?
At first I didn't want to do that becasue I was going to add the whole oject to the list box. I've decided to go a different route since. Now I'm doing exactly that (simply adding the string I wanted) I'm adding the objects into an ArrayList. Thanks - monrobot13
-
You need to call ToString() before adding it to the Items collection. I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
I've decided to go a different route (see post above), but thanks for the help anyways. - monrobot13
-
At first I didn't want to do that becasue I was going to add the whole oject to the list box. I've decided to go a different route since. Now I'm doing exactly that (simply adding the string I wanted) I'm adding the objects into an ArrayList. Thanks - monrobot13
If you did want to go back to your original idea... derive your object from ListViewItem (im doing it with tree's so you might need to check that), and then set its text (no need to overide) to whatever text you want to appear in the list/tree. Then when you add it to the list it will have the exact text that you want. Hope that helps any one else trying something like this! On a similary note... Does anyone know how i can add the same TreeViewItem to 2 different TreeView's without having to clone it?
-
If you did want to go back to your original idea... derive your object from ListViewItem (im doing it with tree's so you might need to check that), and then set its text (no need to overide) to whatever text you want to appear in the list/tree. Then when you add it to the list it will have the exact text that you want. Hope that helps any one else trying something like this! On a similary note... Does anyone know how i can add the same TreeViewItem to 2 different TreeView's without having to clone it?
se99ts wrote: derive your object from ListViewItem That's good as a solution to the original issue, although that's questionable in terms of object oriented model : if the object is non GUI related, why should a non GUI object be "attached" to a GUI class ?
-
se99ts wrote: derive your object from ListViewItem That's good as a solution to the original issue, although that's questionable in terms of object oriented model : if the object is non GUI related, why should a non GUI object be "attached" to a GUI class ?
i know it is a dodgy area, but i found it easier just to add the objects to a list, instead of having to store the entries in an array and then just a text representation of them in the GUI, it just meant i didnt have to do any lookup's i could just call the events on the object. though at this stage i may well have to go to a text representation as the items cant be added to 2 lists, so i may well be doing lookup's!
-
i know it is a dodgy area, but i found it easier just to add the objects to a list, instead of having to store the entries in an array and then just a text representation of them in the GUI, it just meant i didnt have to do any lookup's i could just call the events on the object. though at this stage i may well have to go to a text representation as the items cant be added to 2 lists, so i may well be doing lookup's!
se99ts wrote: though at this stage i may well have to go to a text representation as the items cant be added to 2 lists Exactly, items are always attached to a unique control parent.
-
I'm adding objects of a custom class to a check list box and when I call the
Add ()
method of the check list box the string it adds is simply namespace.classname. I don't want it to add this I want it to add a string variable that is declared in the class. I tried overriding theToString ()
method because I thought it might be calling that, but it still added the same thing. Does anyone know what method I can override so that it adds the string I want. Thanks again. - monrobot13Coming back to the original problem, you can also fill a listbox or combobox by setting the datasource to any object that implements the interface IList. I had the problem,that I wanted the ValueMember property differently set to the DisplayMember. With the collections Add() method this is not possible. So I defined a simple class: internal class clsName { private string _longname; private int _uid; public string LongName {get {return _longname;} set {_longname = value;}} public int UID {get {return _uid;} set {_uid = value;}} public clsName (string lname, int uid) { _longname = lname; _uid = uid; } } and filled the combobox with an arraylist (the parameters come from a xml file): [...] _arrNames.Add(new clsName(nav2.Value,System.Convert.ToInt32(iterator.Current.GetAttribute("UID",nav.NamespaceURI)))); [...] The combobox datasource can then be set to the arraylist and the members to the appropriate custom class properties. [...] cb.DataSource = _arrNames; cb.DisplayMember = "LongName"; cb.ValueMember = "UID"; [...] That's all and the list is filled with the strings plus the value is set to what I need in the combobox event handler later in the application. Wolfgang
-
I'm adding objects of a custom class to a check list box and when I call the
Add ()
method of the check list box the string it adds is simply namespace.classname. I don't want it to add this I want it to add a string variable that is declared in the class. I tried overriding theToString ()
method because I thought it might be calling that, but it still added the same thing. Does anyone know what method I can override so that it adds the string I want. Thanks again. - monrobot13There are 2 ways to do this, one of which you have already mentioned. Firstly you can use the
DisplayMember
property:this.checkedListBox1.DisplayMember = "Test"; // Where Test is the name of the property you would like to display
Alternatively, as you mentioned, override the ToString() method. Most list type controls use this as the default DisplayMember when it is not set explicitly:public override string ToString() { return this.Test; //Again, Where Test is the name of the property you would like to display }
I'm not sure why this did not work for you before. Both of these methods worked in a quick demo i wrote up.