Expand Minimize

ListView::OnColumnClick Method

Raises the ColumnClick event.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

virtual void OnColumnClick(
	ColumnClickEventArgs^ e


Type: System.Windows.Forms::ColumnClickEventArgs

A ColumnClickEventArgs that contains the event data.

Raising an event invokes the event handler through a delegate. For more information, see Handling and Raising Events.

The OnColumnClick method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors

When overriding OnColumnClick in a derived class, be sure to call the base class's OnColumnClick method so that registered delegates receive the event.

The following code example creates a form that contains a ListView control that manually sorts items when a column in the ListView control is clicked. The example defines a class called ListViewItemComparer that implements the System.Collections::IComparer interface that performs the ListViewItem comparison. The example creates an instance of ListViewItemComparer and uses it to set the ListViewItemSorter property of the ListView control. The Sort method call in the ColumnClick event handler uses the methods defined in ListViewItemComparer to perform the sort of items, based on the column that is clicked.

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::Collections;

// Implements the manual sorting of items by columns. 
ref class ListViewItemComparer: public IComparer
   int col;

      col = 0;

   ListViewItemComparer( int column )
      col = column;

   virtual int Compare( Object^ x, Object^ y )
      return String::Compare( (dynamic_cast<ListViewItem^>(x))->SubItems[ col ]->Text,
                              (dynamic_cast<ListViewItem^>(y))->SubItems[ col ]->Text );

public ref class ListViewSortForm: public Form
   ListView^ listView1;

      // Create ListView items to add to the control. 
      array<String^>^temp0 = {"Banana","a","b","c"};
      ListViewItem^ listViewItem1 = gcnew ListViewItem( temp0,-1,Color::Empty,Color::Yellow,nullptr );
      array<String^>^temp1 = {"Cherry","v","g","t"};
      ListViewItem^ listViewItem2 = gcnew ListViewItem( temp1,-1,Color::Empty,Color::Red,
                 gcnew System::Drawing::Font( "Microsoft Sans Serif",8.25F,FontStyle::Regular,GraphicsUnit::Point,0 ) );
      array<String^>^temp2 = {"Apple","h","j","n"};
      ListViewItem^ listViewItem3 = gcnew ListViewItem( temp2,-1,Color::Empty,Color::Lime,nullptr );
      array<String^>^temp3 = {"Pear","y","u","i"};
      ListViewItem^ listViewItem4 = gcnew ListViewItem( temp3,-1,Color::Empty,Color::FromArgb( 192, 128, 156 ),nullptr );

      //Initialize the ListView control and add columns to it. 
      this->listView1 = gcnew 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( gcnew ColumnHeader );
      this->listView1->Columns[ 0 ]->Text = "Column 1";
      this->listView1->Columns[ 0 ]->Width = 100;
      listView1->Columns->Add( gcnew ColumnHeader );
      listView1->Columns[ 1 ]->Text = "Column 2";
      listView1->Columns->Add( gcnew ColumnHeader );
      listView1->Columns[ 2 ]->Text = "Column 3";
      listView1->Columns->Add( gcnew ColumnHeader );
      listView1->Columns[ 3 ]->Text = "Column 4";

      // Suspend control logic until form is done configuring form. 

      // Add Items to the ListView control. 
      array<ListViewItem^>^temp4 = {listViewItem1,listViewItem2,listViewItem3,listViewItem4};
      this->listView1->Items->AddRange( temp4 );

      // Set the location and size of the ListView control. 
      this->listView1->Location = Point(10,10);
      this->listView1->Name = "listView1";
      this->listView1->Size = System::Drawing::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 += gcnew ColumnClickEventHandler( this, &ListViewSortForm::ColumnClick );

      // Initialize the form. 
      this->ClientSize = System::Drawing::Size( 400, 400 );
      array<Control^>^temp5 = {this->listView1};
      this->Controls->AddRange( temp5 );
      this->Name = "ListViewSortForm";
      this->Text = "Sorted ListView Control";

      // Resume lay[Out] of* the form. 
      this->ResumeLayout( false );


   // ColumnClick event handler. 
   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 = gcnew ListViewItemComparer( e->Column );

int main()
   Application::Run( gcnew ListViewSortForm );

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft