hattenn
hattenn

Reputation: 4399

Refreshing the listbox item after an element is changed

I have a ParameterItem class for adding some items to a listbox:

class ParameterItem
{
    public string Name { get; set; }
    public string Value { get; set; }

    public ParameterItem(string name, string value)
    {
        Name = name;
        Value = value;
    }

    public override string ToString()
    {
        return Name + " = " + Value;
    }

    public override bool Equals(object obj)
    {
        if (obj is ParameterItem)
            return (Name == ((ParameterItem)obj).Name);

        return false;   
    }

    public override int GetHashCode()
    {
        return Name.ToLowerInvariant().GetHashCode();
    }
}

And you can add items to the listbox using two textboxes (name and value). When you click on an item in the listbox, the textboxes get filled with the name and the value of the ParameterItem. I have the following code to change the contents of the selected ParameterItem in the listbox:

    private void btnSaveParameter_Click(object sender, EventArgs e)
    {
        ParameterItem currentParameter = new ParameterItem(textParameterName.Text,
                                                           textParameterValue.Text);
        // If we already have the parameter set then edit it.
        if (lstbxSetParameters.Items.Contains(currentParameter))
        {
            ((ParameterItem)lstbxSetParameters.SelectedItem).Value = currentParameter.Value;
            lstbxSetParameters.;
        }
        // If it's not set yet then add it to the listbox.
        else
        {
            lstbxSetParameters.Items.Add(currentParameter);
            textParameterName.Text = String.Empty;
            textParameterValue.Text = String.Empty;
        }
    }

The problem is, even though I can change the contents of the selected ParameterItem, in the listbox, it still looks like it is not changed.

For example I have a parameter in the list box:

TestParameter = 10

And I change the ParameterItem to

TestParameter = 5

but in the listbox it still looks like

TestParameter = 10

even though it's been changed.

How can I solve this problem? I think the listbox item should call the ToString() method of the ParameterItem again and refresh itself but how?

Or is there a better way to add key value pairs in the listbox?

Upvotes: 1

Views: 5225

Answers (2)

Caesar
Caesar

Reputation: 480

My solution:

string[] nList = new string[lb.Items.Count];
nList = lb.Items.OfType<string>().ToArray();
nList[lb.SelectedIndex] = newValue;
lb.Items.Clear();
lb.Items.AddRange(nList);

This way instead of changing the selected item (with a lot of problem) I reloaded the Listbox with the item changed in the array.

Upvotes: 0

Charlie
Charlie

Reputation: 1332

You can change the selected item by remove it and insert it again.

// If we already have the parameter set then edit it.
if (lstbxSetParameters.Items.Contains(currentParameter))
{
    var newItem = new ParameterItem((lstbxSetParameters.SelectedItem as ParameterItem).Name, currentParameter.Value);
    var index = lstbxSetParameters.SelectedIndex;
    lstbxSetParameters.Items.RemoveAt(index);
    lstbxSetParameters.Items.Insert(index, newItem);
    lstbxSetParameters.SelectedIndex = index;
}

Upvotes: 2

Related Questions