|
|
22.1 How do I add SubItems to a ListView control?
|
 |
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;
|
22.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. |
22.3 How do I unselect the selected items in a ListView programatically?
|
 |
this.listView1.SelectedItems.Clear();
|
Me.listView1.SelectedItems.Clear()
|
22.4 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");
|
//As long as UseItemStyleForSubItems is false, you can later set the colors with code such as
|
item1.SubItems[2].BackColor = Color.Pink;
|
22.5 How do I implement custom column sorting in a listview?
|
 |
You create a class the implements the IComparer interface. 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
|
public SorterClass(int 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))
|
else if(int.Parse(item1.SubItems[_colNum].Text) > int.Parse(item2.SubItems[_colNum].Text))
|
private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
|
SorterClass sc = new SorterClass(e.Column);
|
listView1.ListViewItemSorter = sc;
|
22.6 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.Items[i].Selected = true;
|
22.7 How can I sort a column in a ListView?
|
 |
This Microsoft KB article provides a step by step sample. |
22.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. |
|
|
|
|