Windows Forms ListView
FAQ Home
1.
How do I add SubItems to a ListView control?
2. When I call ListViewItem.Selected = true;, why doesn't the
item get selected?
3. I am trying to programmatically change the ForeColor and
BackColor properties of subitems in a listview. Why doesn't this work?
4. How do I implement custom column sorting in a listview?
5. How do I programmatically select an item in my listview?
6. How can I sort a column in a ListView?
7. How can I implement an owner drawn listview?
8. How can I tell which column (subitem) has been clicked on
in my listview?
1 How do I add SubItems to a ListView control?
Try code such as:
ListViewItem item = new
ListViewItem("NewItem");
item.SubItems.AddRange(new string[]{"SubItem1", "SubItem2")};
listView1.Items.Add(item);
listView1.Items.Add(new ListViewItem(new string[]{"item1", "item2",
"item3", "item4"});
listView1.View = View.Details;
2 When I call ListViewItem.Selected = true;, why doesn't
the item get selected?
Make sure the ListView has focus when you set the Selected property. For
example, if this code is in the click handler for a button, this button will
have the focus, and the selection in the list will fail. Just set the focus
back to the list before you set the Selected property.
3 I am trying to programmatically change the ForeColor
and BackColor properties of subitems in a listview. Why doesn't this work?
Make sure the item's UseItemStyleForSubItems property is set
to false.
ListViewItem item1 = new
ListViewItem("item1",0);
//this line makes things work
item1.UseItemStyleForSubItems = false;
//set fore & back color of next sub item
item1.SubItems.Add("1", Color.Black, Color.LightGreen, Font);
item1.SubItems.Add("44");
item1.SubItems.Add("3");
//As long as UseItemStyleForSubItems is false, you can later set the colors
with code such as
item1.SubItems[2].BackColor = Color.Pink;
4 How do I implement custom column sorting in a listview?
You create a class the implements the IComparer inteface. This
interface has a single method that accepts two objects, and returns positive
integers if the first object is larger than the second, negative integers if
the first object is less than the second object, and returns zero if they
are the same. Once you have this class, then you handle the listview's
ColumnClick event to set the property ListViewItemSorter to
point to an instance of this class. You can download a sample project. Here
are some snippets.
public class SorterClass :
IComparer
{
private int _colNum = 0;
public SorterClass(int colNum)
{
_colNum = colNum;
}
//this routine should return -1 if xy and 0 if x==y.
// for our sample we'll just use string comparison
public int Compare(object x, object y)
{
System.Windows.Forms.ListViewItem item1 = (System.Windows.Forms.ListViewItem)
x;
System.Windows.Forms.ListViewItem item2 = (System.Windows.Forms.ListViewItem)
y;
if(int.Parse(item1.SubItems[_colNum].Text) <
int.Parse(item2.SubItems[_colNum].Text))
return -1;
else if(int.Parse(item1.SubItems[_colNum].Text) >
int.Parse(item2.SubItems[_colNum].Text))
return 1;
else
return 0;
}
}
//usage...
private void listView1_ColumnClick(object sender,
System.Windows.Forms.ColumnClickEventArgs e)
{
//don't sort col 0
if(e.Column > 0)
{
SorterClass sc = new SorterClass(e.Column);
listView1.ListViewItemSorter = sc;
}
}
5 How do I programmatically select an item in my
listview?
To select the i-th item, you can use code such as
//Make sure the listview has focus
listView1.Focus();
listView1.Items[i].Selected = true;
6 How can I sort a column in a ListView?
This
Microsoft KB article provides a step by step sample.
7 How can I implement an owner drawn listview?
Carlos Perez explains how to do this in an article and a sample on
codeproject.com. In this sample, he does an owner-drawn listview so he
can implement custom sorting with a column header that contains a up/down
icon to indicate the sorted column and order.
8 How can I tell which column (subitem) has been clicked
on in my listview?
Here is a solution offered by Bharat Patel of Microsoft in the
microsoft.public.dotnet.languages.csharp newgroup. The attached solution
contains both C# and VB.NET projects. The solution use Interop with the
LVM_GETSUBITEMRECT window's message to loop through each subitem's bounds
rectangle until it finds the one that contains the specified mouse click
point.
|