-4

I have a listbox where the items are a struct. This struct contains two strings. One for the title, and the other one for plain text. Now i want to change the text in a existing item in the listbox. Is there a way to do this without removing the existing and adding the updated version to the list?

This is what i want to do,

((TextItemRecord) listbox1.Items[myListbox1.BeforeIndex]).text = "Blablabla";

The compiler says, "Cannot modify the result of an unboxing conversion" when i try to do this. Any solutions?

Struct,

struct TextItemRecord
{
    public UInt64 address;
    public string name, text;

    public TextItemRecord(UInt64 address, string name)
    {
        this.address = address;
        this.name = name;
        this.text = "";
    }
    public override string ToString()
    {
        return name;
    }
}

I'm sorry folks, I have no idea how this site is supposed to work

Bas
  • 3
  • 5

2 Answers2

0

Refer to the following link rename an item in listbox. I believe this will clear up a few things . Since there was no mention of the Event trigger that changes the text, I won't assume it. You can iterate through all items and call the SelectedIndex attribute to change each Text like so:

   foreach(var item in listbox1)
        listbox1.Items[item.SelectedIndex] = "Blablabla";
Community
  • 1
  • 1
Zukunft
  • 17
  • 1
  • 5
0

First off structs are value type not reference type. Which means that the only way to change a field's value is to create a copy with the change and replace the one you copied from. So, I would suggest changing it to a class. Also, since ListBox uses the ToString() method to display items, I would suggest changing that to allow the text field to be shown:

class TextItemRecord
{
    public UInt64 address;
    public string name;
    public string text;

    public TextItemRecord(UInt64 address, string name)
    {
        this.address = address;
        this.name = name;
        this.text = "";
    }
    public override string ToString()
    {
        return $"{name} - {text}";
    }
}

Now to show a list of items in the Listbox assign the DataSource property to the list:

List<TextItemRecord> test;
public Form1()
{
    InitializeComponent();

    test = new List<TextItemRecord>()
    {
        new TextItemRecord(1234, "AAA"),
        new TextItemRecord(5678, "BBB"),
        new TextItemRecord(9012, "CCC")
    };
    listBox1.DataSource = test;
}

Modifying the items in the ListBox and having the changes show is a a little more complicated. Here's one method that works:

private void AddText(List<TextItemRecord> tirList, int index, string text)
{
    BindingManagerBase bmb = listBox1.BindingContext[test];
    bmb.SuspendBinding();
    test[index].text = text;
    bmb.ResumeBinding();
}
tinstaafl
  • 6,908
  • 2
  • 15
  • 22
  • If i understand you correctly, The values are stored in the "List test;", and the listbox is only a visual representation. – Bas Dec 30 '16 at 12:59
  • Sorry, I wasn't done typing and i can't edit for some reason. If i understand you correctly, The values are stored in the "List test;", and the listbox is only a visual representation. This is not really the way I wanted to do that. I wanted to save the items in the listbox itself. – Bas Dec 30 '16 at 13:01
  • With the list as a datasource the listbox will store the items from the list as objects. It will represent the objects by using the ToString() method. By binding the list you can make changes to the name and/or text and the changes will be reflected in the listbox. – tinstaafl Dec 31 '16 at 04:51