Scrat
Scrat

Reputation: 106

How to get selected object in a listview and winforms

I have a list of my class "Company"

private List<Company> company = new List<Company>();

Then i have added a listview into my form. The listview is populated with objects from the list above. I have several textboxes in the form that shows information about the company. The first time an object is selected in the listview, it works fine. The second time an object is selected, I got this error:

ERROR: object reference not set to an instance of an object    

Code for selectedIndexChanged event:

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListView item = ((ListView)sender);
        int index = item.FocusedItem.Index;
        Company company = ReturnCompany(index);
        DoStuff(company);
    }

Code that returns the object:

private Company ReturnCompany(int index)
    {
        return company[index];
    }

Code that fills the textboxes:

private void DoStuff(Company i)
    {
        txtCompanyName.Text = i.CompanyName;
        txtBusiness.Text = i.Business;
        txtCity.Text = i.City;
        txtCountry.Text = i.Country;
    }

I would be very happy if someone could help me with this problem.

Upvotes: 1

Views: 2441

Answers (3)

arvee
arvee

Reputation: 21

I think your code should do what you want maybe there is something wrong with filling the listview, try to fill it like this then try I hope this will help you :)

        private List<Company> company = new List<Company>();
        //maybe you have 100 name of companies 
        Company[] companyNames = new Company[100];


        int i = 0;
        while(i<100)
        {
        foreach (Company cmpny in company)
        {
            companyNames[i] = cmpny;

            i++;
        }
        }
         var listViewItem = new ListViewItem(companyNames); 
         listView1.Items.Add(listViewItem);

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66501

I've never actually used FocusedItem before.

The FocusedItem property returns the ListViewItem that represents the item currently displaying the focus rectangle for the ListView control.

It sounds a little unreliable, in that it only reports an item that has a focus rectangle around it. Not sure what happens if an item is selected, but a different control on the form happens to have focus.

In your case, FocusedItem is null, so you're getting an exception. Try using SelectedIndices instead, which returns a collection of the indexes for each item you've selected in the list.

Make sure at least one item is selected (since this event could fire when you're deselecting an item as well), then use the selected index to find your company using your existing code.

ListView compListView = ((ListView)sender);

if (compListView.SelectedIndices.Count > 0)
{
    var selectedCompany = companies[compListView.SelectedIndices[0]];

    DoStuff(selectedCompany);
}

Upvotes: 1

Jose M.
Jose M.

Reputation: 1316

Which line throws the exception ? Try this:

 private void listView1_SelectedIndexChanged(object sender, EventArgs e)
 {
    ListView item = ((ListView)sender);
    int index = item.FocusedItem.Index;
    if (company.Any() && (0 <= index || index < company.Count))
    {
        Company company = ReturnCompany(index);
        DoStuff(company);
    }
}

Upvotes: 0

Related Questions