ListView.ColumnClick : ListView « System.Windows.Forms « C# / C Sharp by API

Home
C# / C Sharp by API
1.Microsoft.Win32
2.System
3.System.Collections
4.System.Collections.Generic
5.System.Collections.Specialized
6.System.ComponentModel
7.System.Configuration
8.System.Data
9.System.Data.Common
10.System.Data.Linq
11.System.Data.Odbc
12.System.Data.OleDb
13.System.Data.Sql
14.System.Data.SqlClient
15.System.Diagnostics
16.System.DirectoryServices
17.System.Drawing
18.System.Drawing.Drawing2D
19.System.Drawing.Imaging
20.System.Drawing.Printing
21.System.Drawing.Text
22.System.EnterpriseServices
23.System.Globalization
24.System.IO
25.System.IO.Compression
26.System.IO.IsolatedStorage
27.System.IO.Ports
28.System.Linq
29.System.Management
30.System.Media
31.System.Messaging
32.System.Net
33.System.Net.Mail
34.System.Net.NetworkInformation
35.System.Net.Sockets
36.System.Reflection
37.System.Resources
38.System.Runtime
39.System.Runtime.CompilerServices
40.System.Runtime.InteropServices
41.System.Runtime.Remoting
42.System.Runtime.Remoting.Channels
43.System.Runtime.Remoting.Channels.Http
44.System.Runtime.Remoting.Messaging
45.System.Runtime.Serialization
46.System.Runtime.Serialization.Formatters.Binary
47.System.Runtime.Serialization.Formatters.Soap
48.System.Security
49.System.Security.AccessControl
50.System.Security.Cryptography
51.System.Security.Cryptography.X509Certificates
52.System.Security.Permissions
53.System.Security.Policy
54.System.Security.Principal
55.System.ServiceProcess
56.System.Text
57.System.Text.RegularExpressions
58.System.Threading
59.System.Timers
60.System.Web.Security
61.System.Web.Services
62.System.Windows.Controls
63.System.Windows.Forms
64.System.Xml
65.System.Xml.Linq
66.System.Xml.Schema
67.System.Xml.Serialization
68.System.Xml.XPath
69.System.Xml.Xsl
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp by API » System.Windows.Forms » ListView 
ListView.ColumnClick
 

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(464152);
        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(750);
        this.REPORT.Name = "REPORT";
        this.REPORT.Size = new System.Drawing.Size(7524);
        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(3000);
        this.LIST.Name = "LIST";
        this.LIST.Size = new System.Drawing.Size(7524);
        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(1500);
        this.SMALLICON.Name = "SMALLICON";
        this.SMALLICON.Size = new System.Drawing.Size(7524);
        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(3750);
        this.LARGEICON.Name = "LARGEICON";
        this.LARGEICON.Size = new System.Drawing.Size(7524);
        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(7524);
        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(2250);
        this.CHECKED.Name = "CHECKED";
        this.CHECKED.Size = new System.Drawing.Size(7524);
        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(0157);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(46424);
        this.panel1.TabIndex = 4;
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(464181);
        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();
    }


}

   
  
Related examples in the same category
1.extends ListView
2.ListView.Click
3.ListView.DragDrop
4.ListView.ItemActivate
5.ListView.Items
6.ListView.LargeImageList
7.ListView.MouseUp
8.ListView.Sorting
9.ListView.View
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.