1

I have two files. One file with a class called Wood. I have created some objects with this class. These Objects include the attribute WoodType and a few others. I filled the objects into an array called woodObjects. The second file is a Form where I have a ListView.

So my goal was, to add all objects with their attributes to the ListView. I did it like that:

String[] row = { (listView1.Items.Count + 1).ToString(), WoodType, condition, isDry};
ListViewItem item = new ListViewItem(row);
listView1.Items.Add(item);

Success.

Now I have to update the attribute WoodType of one object in the ListView. I created a button "change Type" for that. I thought of the user clicking the row where the object ist he wants to change, and then clicking the button "change Type" to change WoodType it. To realize this, I wanted to take the index of the row, and than changing the value of "WoodType" with its set Accessor with something like the following:

woodObjects[indexOfRowFromListView].WoodType = "Oak Wood";

Yes, I know, this would never work like that. So my questions are: How can I select the correct index and how do I change the value of the attribute?

*I also thought of deleting the objects, but saving its values so I can create a new objects with the same values except WoodType. I appreciate every help!

9
  • @MehdiDehghani The problem is, that I dont know how to get the correct index. After i got the index, i have to know, how to change the value of the attribute. I updated my question to clarify. Aug 10, 2018 at 13:15
  • ListView should have a property called SelectedIndex.
    – welrocken
    Aug 10, 2018 at 13:20
  • @MehdiDehghani Yes I do. I can also get the row with Ecordero 's solution. The main problem really is, how I can change the value of the attribute. Aug 10, 2018 at 13:24
  • @welrocken Thanks. And how can I change the value of the attribute now when i have the index? Aug 10, 2018 at 13:24
  • woodObjects[listView.SelectedIndex].WoodType = "New Value";
    – welrocken
    Aug 10, 2018 at 13:27

1 Answer 1

1

I think the cleanest solution to your problem would involve writing a new ListViewItem implementation. Something like this:

public class WoodObjectListItem : System.Windows.Forms.ListViewItem
{
    public Wood WoodObject { get; }

    public WoodObjectListItem(int rowNumber, Wood woodObject) 
        : base(new string[] { rowNumber.ToString(), woodObject.WoodType, woodObject.Condition, woodObject.IsDry })
    {
        WoodObject = woodObject;
    }

    public void ChangeType()
    {
        WoodObject.WoodType = "Oak Wood";
    }
}

Then you could add your woodObjects to the list view like:

listView1.Items.Add(new WoodObjectListItem(listView1.Items.Count + 1, woodObject));

And when the button is clicked you could do:

if (listView1.SelectedItem is WoodObjectListItem woodObjectListItem)
    woodObjectListItem.ChangeType();
1
  • 1
    Thank you very, very much. Solved my problem! Aug 10, 2018 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.