获得所选图标的索引
方法一:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
int k;
k = listView1.Items.IndexOf(listView1.FocusedItem);
textBox1.Text = k.ToString();
}
方法二:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.SelectedItems)
{
textBox1.Text = item.Index.ToString();
}
}
如何获得ListView控件选中图标的索引号
index= listView1.FocusedItem.ImageIndex;
如何:向 Windows 窗体 ListView 控件中添加列
// Set to details view. listView1.View = View.Details; // Add a column with width 20 and left alignment. listView1.Columns.Add("File type", 20, HorizontalAlignment.Left);
使用 Windows 窗体 ListView 控件添加和移除项
// Adds a new item with ImageIndex 3
listView1.Items.Add("List item text", 3);
// Removes the first item in the list.
listView1.Items.RemoveAt(0);
// Clears all the items.
listView1.Items.Clear();
如何:向 Windows 窗体 ListView 控件中删除列
listView1.Columns.Clear();
如何:显示 Windows 窗体 ListView 控件的图标
listView1.SmallImageList = imageList1;
// Sets the first list item to display the 4th image.
listView1.Items[0].ImageIndex = 3;
如何:使用 Windows 窗体 ListView 控件在列中显示子项
// Adds two subitems to the first list item.
listView1.Items[0].SubItems.Add("John Smith");
listView1.Items[0].SubItems.Add("Accounting");
如何:选择 Windows 窗体 ListView 控件中的项
this.listView1.Focus();
this.listView1.Items[0].Selected = true;
如何:对 Windows 窗体 ListView 控件中的项进行分组
// Adds a new group that has a left-aligned header
listView1.Groups.Add(new ListViewGroup("List item text", _
HorizontalAlignment.Left));
如何:在 Windows 窗体 ListView 控件中启用平铺视图
listView1.View = View.Tiling;
using System;
using System.Drawing;
using System.Windows.Forms;
public class ListViewTilingExample : Form
{
private ImageList myImageList;
public ListViewTilingExample()
{
// Initialize myListView.
ListView myListView = new ListView();
myListView.Dock = DockStyle.Fill;
myListView.View = View.Tile;
// Initialize the tile size.
myListView.TileSize = new Size(400, 45);
// Initialize the item icons.
myImageList = new ImageList();
using (Icon myIcon = new Icon("book.ico"))
{
myImageList.Images.Add(myIcon);
}
myImageList.ImageSize = new Size(32, 32);
myListView.LargeImageList = myImageList;
// Add column headers so the subitems will appear.
myListView.Columns.AddRange(new ColumnHeader[]
{new ColumnHeader(), new ColumnHeader(), new ColumnHeader()});
// Create items and add them to myListView.
ListViewItem item0 = new ListViewItem( new string[]
{"Programming Windows",
"Petzold, Charles",
"1998"}, 0 );
ListViewItem item1 = new ListViewItem( new string[]
{"Code: The Hidden Language of Computer Hardware and Software",
"Petzold, Charles",
"2000"}, 0 );
ListViewItem item2 = new ListViewItem( new string[]
{"Programming Windows with C#",
"Petzold, Charles",
"2001"}, 0 );
ListViewItem item3 = new ListViewItem( new string[]
{"Coding Techniques for Microsoft Visual Basic .NET",
"Connell, John",
"2001"}, 0 );
ListViewItem item4 = new ListViewItem( new string[]
{"C# for Java Developers",
"Jones, Allen & Freeman, Adam",
"2002"}, 0 );
ListViewItem item5 = new ListViewItem( new string[]
{"Microsoft .NET XML Web Services Step by Step",
"Jones, Allen & Freeman, Adam",
"2002"}, 0 );
myListView.Items.AddRange(
new ListViewItem[] {item0, item1, item2, item3, item4, item5});
// Initialize the form.
this.Controls.Add(myListView);
this.Size = new System.Drawing.Size(430, 330);
this.Text = "ListView Tiling Example";
}
// Clean up any resources being used.
protected override void Dispose(bool disposing)
{
if (disposing)
{
myImageList.Dispose();
}
base.Dispose(disposing);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ListViewTilingExample());
}
}
如何:在 Windows 窗体 ListView 控件中显示插入标记
using System;
using System.Drawing;
using System.Windows.Forms;
public class ListViewInsertionMarkExample : Form
{
private ListView myListView;
public ListViewInsertionMarkExample()
{
// Initialize myListView.
myListView = new ListView();
myListView.Dock = DockStyle.Fill;
myListView.View = View.LargeIcon;
myListView.MultiSelect = false;
myListView.ListViewItemSorter = new ListViewIndexComparer();
// Initialize the insertion mark.
myListView.InsertionMark.Color = Color.Green;
// Add items to myListView.
myListView.Items.Add("zero");
myListView.Items.Add("one");
myListView.Items.Add("two");
myListView.Items.Add("three");
myListView.Items.Add("four");
myListView.Items.Add("five");
// Initialize the drag-and-drop operation when running
// under Windows XP or a later operating system.
if (OSFeature.Feature.IsPresent(OSFeature.Themes))
{
myListView.AllowDrop = true;
myListView.ItemDrag += new ItemDragEventHandler(myListView_ItemDrag);
myListView.DragEnter += new DragEventHandler(myListView_DragEnter);
myListView.DragOver += new DragEventHandler(myListView_DragOver);
myListView.DragLeave += new EventHandler(myListView_DragLeave);
myListView.DragDrop += new DragEventHandler(myListView_DragDrop);
}
// Initialize the form.
this.Text = "ListView Insertion Mark Example";
this.Controls.Add(myListView);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ListViewInsertionMarkExample());
}
// Starts the drag-and-drop operation when an item is dragged.
private void myListView_ItemDrag(object sender, ItemDragEventArgs e)
{
myListView.DoDragDrop(e.Item, DragDropEffects.Move);
}
// Sets the target drop effect.
private void myListView_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.AllowedEffect;
}
// Moves the insertion mark as the item is dragged.
private void myListView_DragOver(object sender, DragEventArgs e)
{
// Retrieve the client coordinates of the mouse pointer.
Point targetPoint =
myListView.PointToClient(new Point(e.X, e.Y));
// Retrieve the index of the item closest to the mouse pointer.
int targetIndex = myListView.InsertionMark.NearestIndex(targetPoint);
// Confirm that the mouse pointer is not over the dragged item.
if (targetIndex > -1)
{
// Determine whether the mouse pointer is to the left or
// the right of the midpoint of the closest item and set
// the InsertionMark.AppearsAfterItem property accordingly.
Rectangle itemBounds = myListView.GetItemRect(targetIndex);
if ( targetPoint.X > itemBounds.Left + (itemBounds.Width / 2) )
{
myListView.InsertionMark.AppearsAfterItem = true;
}
else
{
myListView.InsertionMark.AppearsAfterItem = false;
}
}
// Set the location of the insertion mark. If the mouse is
// over the dragged item, the targetIndex value is -1 and
// the insertion mark disappears.
myListView.InsertionMark.Index = targetIndex;
}
// Removes the insertion mark when the mouse leaves the control.
private void myListView_DragLeave(object sender, EventArgs e)
{
myListView.InsertionMark.Index = -1;
}
// Moves the item to the location of the insertion mark.
private void myListView_DragDrop(object sender, DragEventArgs e)
{
// Retrieve the index of the insertion mark;
int targetIndex = myListView.InsertionMark.Index;
// If the insertion mark is not visible, exit the method.
if (targetIndex == -1)
{
return;
}
// If the insertion mark is to the right of the item with
// the corresponding index, increment the target index.
if (myListView.InsertionMark.AppearsAfterItem)
{
targetIndex++;
}
// Retrieve the dragged item.
ListViewItem draggedItem =
(ListViewItem)e.Data.GetData(typeof(ListViewItem));
// Insert a copy of the dragged item at the target index.
// A copy must be inserted before the original item is removed
// to preserve item index values.
myListView.Items.Insert(
targetIndex, (ListViewItem)draggedItem.Clone());
// Remove the original copy of the dragged item.
myListView.Items.Remove(draggedItem);
}
// Sorts ListViewItem objects by index.
private class ListViewIndexComparer : System.Collections.IComparer
{
public int Compare(object x, object y)
{
return ((ListViewItem)x).Index - ((ListViewItem)y).Index;
}
}
}
如何:向 ListView 控件添加搜索功能
private ListView textListView = new ListView();
private TextBox searchBox = new TextBox();
private void InitializeTextSearchListView()
{
searchBox.Location = new Point(10, 60);
textListView.Scrollable = true;
textListView.Width = 80;
textListView.Height = 50;
// Set the View to list to use the FindItemWithText method.
textListView.View = View.List;
// Populate the ListViewWithItems
textListView.Items.AddRange(new ListViewItem[]{
new ListViewItem("Amy Alberts"),
new ListViewItem("Amy Recker"),
new ListViewItem("Erin Hagens"),
new ListViewItem("Barry Johnson"),
new ListViewItem("Jay Hamlin"),
new ListViewItem("Brian Valentine"),
new ListViewItem("Brian Welker"),
new ListViewItem("Daniel Weisman") });
// Handle the TextChanged to get the text for our search.
searchBox.TextChanged += new EventHandler(searchBox_TextChanged);
// Add the controls to the form.
this.Controls.Add(textListView);
this.Controls.Add(searchBox);
}
private void searchBox_TextChanged(object sender, EventArgs e)
{
// Call FindItemWithText with the contents of the textbox.
ListViewItem foundItem =
textListView.FindItemWithText(searchBox.Text, false, 0, true);
if (foundItem != null)
{
textListView.TopItem = foundItem;
}
}