Groups | Blog | Home
all groups > c# > august 2003 >

c# : Updating labels from a listview


Greg
8/2/2003 8:23:43 PM
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,
Brian Patterson
8/3/2003 12:00:25 AM
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



[quoted text, click to view]

AddThis Social Bookmark Button