Search code examples
c#tostringcheckedlistbox

Changing ToString value for checkedListBox


Currently creating a windows form application program based on the decorator pattern in C#. The program is structured to have one main class ("Computer") of which is extended to other classes(as wrappers) which form the options available.

Problem: Using a checkedboxlist where user has the choice to choose between options which makes text specific to each option appear in one label when they are checked no matter if only one(only that checked option text will be shown after the label text) or all are checked (all checked options text will be shown one after the other after the label text). Following code shows the set text for the latest option checked in the label and it does not remove the text were the user to uncheck all options.

foreach (object itemChecked in checkedListBox1.CheckedItems)
        {
            Computer computer = (Computer)itemChecked;
            label1.Text = "" + computer.description();
        } 

That problem was solved on here but the solution replaced "description" with ToString. My problem with that is, I am looking to use what is held in "description" for the text in the label rather than what is being held in ToString which is being used to name each checked option. Below are code examples of them both from the main class (Computer):

public virtual String description()
    {
        return "Currently in basket is a Computer ";
        //return this.ToString();
    }

public override string ToString()
    {
        return "Desktop";
    }

The reason behind that is to keep to the decorator pattern structure, ToString bypasses that as it can be used in the same way without the decorator pattern structure. The solution spoken of before follows:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (e.NewValue == CheckState.Checked && e.CurrentValue == CheckState.Unchecked) //Continue if the new CheckState value of the item is changing to Checked
    {
        label1.Text += "a " + checkedListBox1.Items[e.Index].ToString() +", "; //Append ("a " + the item's value + ", ") to the label1 Text property
    }
        else if (e.NewValue == CheckState.Unchecked && e.CurrentValue == CheckState.Checked) //Continue if the new CheckState value of the item is changing to Unchecked
        {
            label1.Text =  label1.Text.Replace("a " +checkedListBox1.Items[e.Index].ToString() + ", ", ""); //Replace ("a " + the item's value + ", ") with an empty string and assign this value to the label1 Text property
        }
}

A solution, I think found on another topic(can't remember exactly), much closer to what I am looking for, below, uses "description" for the value of the label while the ToString stays as the value for the options. However this code brings an error of there being no definition of "CheckedItem" nor any extension method by the very same name (end of the fourth line):

for (int i = 0; i < checkedListBox1.Items.Count; i++)
            if (checkedListBox1.GetItemChecked(i))
            {
                Computer computer = (Computer)checkedListBox1.CheckedItem;
                label1.Text = "" + computer.description();
            }
            else
            {
                label1.Text = "";
            }

P.S. I am a novice/beginner programmer, excuse any inconsistencies or badly explained parts. Thank you.


Solution

  • If I well understood what you want to do, then you need to create a public function that returns Computer.ToString():

    public string Description(){
        return this.ToString();
    }
    
    protected override ToString(){
        /// and here goes the code you need.
    }