Updating labels from a listview

G

Greg

I'm trying to display record information in labels, I've
got a listview filled with records and when you click on
one of them I want that record's information shown in
several labels. This is how I'm trying to do it:

private void listView_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
// gets currently selected record
li = listView.GetItemAt(e.X,e.Y);
}

private void listView_SelectedIndexChanged(object sender,
System.EventArgs e)
{
if( li != null )
lblNum.Text = li.SubItems[0].Text;
}

With this code li is always null! li is getting set
because my other functions have been using it fine, it
seems that when it tries to access it at this point it's
always null no matter what. I've also tried putting the
line 'lblNum.Text = li.SubItems[0].Text;' right below 'li
= listView.GetItemAt(e.X,e.Y);' but it never updates the
label. Any suggestions?

Thanks,
Greg
 
B

Brian Patterson

Do away with your mouse down handler. Access the listviewitems like this:

private void listView1_Click(object sender, System.EventArgs e)

{

Console.WriteLine("Item Text: " + listView1.SelectedItems[0].Text);

Console.WriteLine("SubItem 1: " +
listView1.SelectedItems[0].SubItems[1].Text);

}



Brian :patterson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top