本文整理汇总了C#中System.Windows.Forms.ListView.ColumnClick事件的典型用法代码示例。如果您正苦于以下问题:C# ListView.ColumnClick事件的具体用法?C# ListView.ColumnClick怎么用?C# ListView.ColumnClick使用的例子?那么恭喜您, 这里精选的事件代码示例或许可以为您提供帮助。您也可以进一步了解该事件所在类System.Windows.Forms.ListView
的用法示例。
在下文中一共展示了ListView.ColumnClick事件的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ListViewSortForm
//引入命名空间
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace ListViewSortFormNamespace
{
public class ListViewSortForm : Form
{
private ListView listView1;
public ListViewSortForm()
{
// Create ListView items to add to the control.
ListViewItem listViewItem1 = new ListViewItem(new string[] {"Banana","a","b","c"}, -1, Color.Empty, Color.Yellow, null);
ListViewItem listViewItem2 = new ListViewItem(new string[] {"Cherry","v","g","t"}, -1, Color.Empty, Color.Red, new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((System.Byte)(0))));
ListViewItem listViewItem3 = new ListViewItem(new string[] {"Apple","h","j","n"}, -1, Color.Empty, Color.Lime, null);
ListViewItem listViewItem4 = new ListViewItem(new string[] {"Pear","y","u","i"}, -1, Color.Empty, Color.FromArgb(((System.Byte)(192)), ((System.Byte)(128)), ((System.Byte)(156))), null);
//Initialize the ListView control and add columns to it.
this.listView1 = new ListView();
// Set the initial sorting type for the ListView.
this.listView1.Sorting = SortOrder.None;
// Disable automatic sorting to enable manual sorting.
this.listView1.View = View.Details;
// Add columns and set their text.
this.listView1.Columns.Add(new ColumnHeader());
this.listView1.Columns[0].Text = "Column 1";
this.listView1.Columns[0].Width = 100;
listView1.Columns.Add(new ColumnHeader());
listView1.Columns[1].Text = "Column 2";
listView1.Columns.Add(new ColumnHeader());
listView1.Columns[2].Text = "Column 3";
listView1.Columns.Add(new ColumnHeader());
listView1.Columns[3].Text = "Column 4";
// Suspend control logic until form is done configuring form.
this.SuspendLayout();
// Add Items to the ListView control.
this.listView1.Items.AddRange(new ListViewItem[] {listViewItem1,
listViewItem2,
listViewItem3,
listViewItem4});
// Set the location and size of the ListView control.
this.listView1.Location = new Point(10, 10);
this.listView1.Name = "listView1";
this.listView1.Size = new Size(300, 100);
this.listView1.TabIndex = 0;
// Enable editing of the items in the ListView.
this.listView1.LabelEdit = true;
// Connect the ListView.ColumnClick event to the ColumnClick event handler.
this.listView1.ColumnClick += new ColumnClickEventHandler(ColumnClick);
// Initialize the form.
this.ClientSize = new Size(400, 400);
this.Controls.AddRange(new Control[] {this.listView1});
this.Name = "ListViewSortForm";
this.Text = "Sorted ListView Control";
// Resume layout of the form.
this.ResumeLayout(false);
}
// ColumnClick event handler.
private void ColumnClick(object o, ColumnClickEventArgs e)
{
// Set the ListViewItemSorter property to a new ListViewItemComparer
// object. Setting this property immediately sorts the
// ListView using the ListViewItemComparer object.
this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
}
[System.STAThreadAttribute()]
public static void Main()
{
Application.Run(new ListViewSortForm());
}
}
// Implements the manual sorting of items by columns.
class ListViewItemComparer : IComparer
{
private int col;
public ListViewItemComparer()
{
col = 0;
}
public ListViewItemComparer(int column)
{
col = column;
}
public int Compare(object x, object y)
{
return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
}
}
}
示例2: ImageList
//引入命名空间
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.ListView listView1 = new System.Windows.Forms.ListView();
private System.Windows.Forms.ColumnHeader Country= new System.Windows.Forms.ColumnHeader();
private System.Windows.Forms.ColumnHeader Capital= new System.Windows.Forms.ColumnHeader();
private System.Windows.Forms.ColumnHeader City_1= new System.Windows.Forms.ColumnHeader();
private System.Windows.Forms.ColumnHeader City_2= new System.Windows.Forms.ColumnHeader();
private System.Windows.Forms.Button REPORT= new System.Windows.Forms.Button();
private System.Windows.Forms.Button LIST= new System.Windows.Forms.Button();
private System.Windows.Forms.Button SMALLICON= new System.Windows.Forms.Button();
private System.Windows.Forms.Button LARGEICON= new System.Windows.Forms.Button();
ImageList ig = new ImageList();
private System.Windows.Forms.Button SELECTED= new System.Windows.Forms.Button();
private System.Windows.Forms.Button CHECKED= new System.Windows.Forms.Button();
private System.Windows.Forms.Panel panel1= new System.Windows.Forms.Panel();
public Form1() {
this.panel1.SuspendLayout();
this.SuspendLayout();
//
this.listView1.AllowColumnReorder = true;
this.listView1.CheckBoxes = true;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.Country,
this.Capital,
this.City_1,
this.City_2});
this.listView1.Dock = System.Windows.Forms.DockStyle.Top;
this.listView1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.listView1.ForeColor = System.Drawing.SystemColors.HotTrack;
this.listView1.FullRowSelect = true;
this.listView1.Size = new System.Drawing.Size(464, 152);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView1_ColumnClick);
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
//
// Country
//
this.Country.Text = "Country";
this.Country.Width = 100;
//
// Capital
//
this.Capital.Text = "Capital";
this.Capital.Width = 150;
//
// City_1
//
this.City_1.Text = "City_1";
this.City_1.Width = 100;
//
// City_2
//
this.City_2.Text = "City_2";
this.City_2.Width = 100;
//
// REPORT
//
this.REPORT.Dock = System.Windows.Forms.DockStyle.Left;
this.REPORT.Location = new System.Drawing.Point(75, 0);
this.REPORT.Name = "REPORT";
this.REPORT.Size = new System.Drawing.Size(75, 24);
this.REPORT.TabIndex = 1;
this.REPORT.Text = "Report";
this.REPORT.Click += new System.EventHandler(this.REPORT_Click);
//
// LIST
//
this.LIST.Dock = System.Windows.Forms.DockStyle.Left;
this.LIST.Location = new System.Drawing.Point(300, 0);
this.LIST.Name = "LIST";
this.LIST.Size = new System.Drawing.Size(75, 24);
this.LIST.TabIndex = 1;
this.LIST.Text = "List";
this.LIST.Click += new System.EventHandler(this.LIST_Click);
//
// SMALLICON
//
this.SMALLICON.Dock = System.Windows.Forms.DockStyle.Left;
this.SMALLICON.Location = new System.Drawing.Point(150, 0);
this.SMALLICON.Name = "SMALLICON";
this.SMALLICON.Size = new System.Drawing.Size(75, 24);
this.SMALLICON.TabIndex = 1;
this.SMALLICON.Text = "Small Icon";
this.SMALLICON.Click += new System.EventHandler(this.SMALLICON_Click);
//
// LARGEICON
//
this.LARGEICON.Dock = System.Windows.Forms.DockStyle.Left;
this.LARGEICON.Location = new System.Drawing.Point(375, 0);
this.LARGEICON.Name = "LARGEICON";
this.LARGEICON.Size = new System.Drawing.Size(75, 24);
this.LARGEICON.TabIndex = 1;
this.LARGEICON.Text = "Large Icon";
this.LARGEICON.Click += new System.EventHandler(this.LARGEICON_Click);
//
// SELECTED
//
this.SELECTED.Dock = System.Windows.Forms.DockStyle.Left;
this.SELECTED.Name = "SELECTED";
this.SELECTED.Size = new System.Drawing.Size(75, 24);
this.SELECTED.TabIndex = 2;
this.SELECTED.Text = "Selected";
this.SELECTED.Click += new System.EventHandler(this.SELECTED_Click);
//
// CHECKED
//
this.CHECKED.Dock = System.Windows.Forms.DockStyle.Left;
this.CHECKED.Location = new System.Drawing.Point(225, 0);
this.CHECKED.Name = "CHECKED";
this.CHECKED.Size = new System.Drawing.Size(75, 24);
this.CHECKED.TabIndex = 3;
this.CHECKED.Text = "Checked";
this.CHECKED.Click += new System.EventHandler(this.CHECKED_Click);
//
// panel1
//
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.LARGEICON,
this.LIST,
this.CHECKED,
this.SMALLICON,
this.REPORT,
this.SELECTED});
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 157);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(464, 24);
this.panel1.TabIndex = 4;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(464, 181);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panel1,
this.listView1});
this.Name = "Form1";
this.Text = "ListView";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e) {
ig.Images.Add(new Icon("FLGUSA01.ICO"));
ig.Images.Add(new Icon("CTRITALY.ICO"));
ig.Images.Add(new Icon("FLGCAN.ICO"));
ig.Images.Add(new Icon("FLGSWITZ.ICO"));
ig.Images.Add(new Icon("FLGUK.ICO"));
listView1.SmallImageList = ig;
listView1.LargeImageList = ig;
listView1.Columns[1].TextAlign = HorizontalAlignment.Center;
listView1.Columns[2].TextAlign = HorizontalAlignment.Center;
listView1.Columns[3].TextAlign = HorizontalAlignment.Center;
string[] lv = new String[4];
lv[0] = "A";
lv[1] = "A1";
lv[2] = "A2";
lv[3] = "A3";
listView1.Items.Add(new ListViewItem(lv, 0));
lv[0] = "B";
lv[1] = "B1";
lv[2] = "B2";
lv[3] = "B3";
listView1.Items.Add(new ListViewItem(lv, 1));
lv[0] = "C";
lv[1] = "C1";
lv[2] = "C2";
lv[3] = "C3";
listView1.Items.Add(new ListViewItem(lv, 2));
lv[0] = "D";
lv[1] = "D1";
lv[2] = "D2";
lv[3] = "D3";
listView1.Items.Add(new ListViewItem(lv, 3));
lv[0] = "E";
lv[1] = "E1";
lv[2] = "E2";
lv[3] = "E3";
listView1.Items.Add(new ListViewItem(lv, 4));
for (int j = 0; j < listView1.Items.Count; j++) {
ListViewItem lvi = listView1.Items[j];
for (int i = 0; i < lvi.SubItems.Count; i++) {
Console.WriteLine(lvi.SubItems[i].Text);
}
}
}
private void REPORT_Click(object sender, System.EventArgs e) {
this.Text = "Report View";
listView1.View = View.Details;
}
private void LARGEICON_Click(object sender, System.EventArgs e) {
this.Text = "LargeIcon View";
listView1.View = View.LargeIcon;
}
private void SMALLICON_Click(object sender, System.EventArgs e) {
this.Text = "SmallIcon View";
listView1.View = View.SmallIcon;
}
private void LIST_Click(object sender, System.EventArgs e) {
this.Text = "List View";
listView1.View = View.List;
}
private void SELECTED_Click(object sender, System.EventArgs e) {
for (int k = 0; k < listView1.SelectedIndices.Count; k++) {
ListViewItem lvi = listView1.Items[listView1.SelectedIndices[k]];
for (int i = 0; i < lvi.SubItems.Count; i++) {
Console.WriteLine(lvi.SubItems[i].Text);
}
}
}
private void CHECKED_Click(object sender, System.EventArgs e) {
for (int k = 0; k < listView1.CheckedIndices.Count; k++) {
ListViewItem lvi = listView1.Items[listView1.CheckedIndices[k]];
for (int i = 0; i < lvi.SubItems.Count; i++) {
Console.WriteLine(lvi.SubItems[i].Text);
}
}
}
private void listView1_SelectedIndexChanged(object sender, System.EventArgs e) {
}
private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e) {
if (listView1.Sorting == SortOrder.Descending)
listView1.Sorting = SortOrder.Ascending;
else
listView1.Sorting = SortOrder.Descending;
listView1.Sort();
}
}