次の方法で共有


ListView.OnColumnClick メソッド

ColumnClick イベントを発生させます。

Protected Overridable Sub OnColumnClick( _
   ByVal e As ColumnClickEventArgs _)
[C#]
protected virtual void OnColumnClick(ColumnClickEventArgse);
[C++]
protected: virtual void OnColumnClick(ColumnClickEventArgs* e);
[JScript]
protected function OnColumnClick(
   e : ColumnClickEventArgs);

パラメータ

解説

イベントが発生すると、デリゲートを使用してイベント ハンドラが呼び出されます。詳細については、「 イベントの発生 」を参照してください。

OnColumnClick メソッドを使用すると、デリゲートを結び付けずに、派生クラスでイベントを処理することもできます。派生クラスでイベントを処理する場合は、この手法をお勧めします。

継承時の注意: 派生クラスで OnColumnClick をオーバーライドする場合は、登録されているデリゲートがイベントを受け取ることができるように、基本クラスの OnColumnClick メソッドを呼び出してください。

使用例

[Visual Basic, C#, C++] ListView コントロールの列がクリックされたときに手動で項目を並べ替える ListView コントロールが配置されたフォームを作成する例を次に示します。この例では、 ListViewItem の比較を実行する System.Collections.IComparer インターフェイスを実装するクラスを ListViewItemComparer という名前で定義します。また、 ListViewItemComparer のインスタンスを作成し、これを使用して ListView コントロールの ListViewItemSorter プロパティを設定します。 ColumnClick イベント ハンドラでの Sort メソッド呼び出しでは、 ListViewItemComparer に定義されているメソッドを使用し、クリックされた列を基準に、項目の並べ替えを実行します。

 
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections


Namespace ListViewSortFormNamespace

    Public Class ListViewSortForm
        Inherits Form

        Private listView1 As ListView

        Public Sub New()
            ' Create ListView items to add to the control.
            Dim listViewItem1 As New ListViewItem(New String() {"Banana", "a", "b", "c"}, -1, Color.Empty, Color.Yellow, Nothing)
            Dim listViewItem2 As New ListViewItem(New String() {"Cherry", "v", "g", "t"}, -1, Color.Empty, Color.Red, New Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point, CType(0, System.Byte)))
            Dim listViewItem3 As New ListViewItem(New String() {"Apple", "h", "j", "n"}, -1, Color.Empty, Color.Lime, Nothing)
            Dim listViewItem4 As New ListViewItem(New String() {"Pear", "y", "u", "i"}, -1, Color.Empty, Color.FromArgb(CType(192, System.Byte), CType(128, System.Byte), CType(156, System.Byte)), Nothing)

            'Initialize the ListView control and add columns to it.
            Me.listView1 = New ListView

            ' Set the initial sorting type for the ListView.
            Me.listView1.Sorting = SortOrder.None
            ' Disable automatic sorting to enable manual sorting.
            Me.listView1.View = View.Details
            ' Add columns and set their text.
            Me.listView1.Columns.Add(New ColumnHeader)
            Me.listView1.Columns(0).Text = "Column 1"
            Me.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.
            Me.SuspendLayout()
            ' Add Items to the ListView control.
            Me.listView1.Items.AddRange(New ListViewItem() {listViewItem1, listViewItem2, listViewItem3, listViewItem4})
            ' Set the location and size of the ListView control.
            Me.listView1.Location = New Point(10, 10)
            Me.listView1.Name = "listView1"
            Me.listView1.Size = New Size(300, 100)
            Me.listView1.TabIndex = 0
            ' Enable editing of the items in the ListView.
            Me.listView1.LabelEdit = True
            ' Connect the ListView.ColumnClick event to the ColumnClick event handler.
            AddHandler Me.listView1.ColumnClick, AddressOf ColumnClick

            ' Initialize the form.
            Me.AutoScaleBaseSize = New Size(5, 13)
            Me.ClientSize = New Size(400, 400)
            Me.Controls.AddRange(New Control() {Me.listView1})
            Me.Name = "ListViewSortForm"
            Me.Text = "Sorted ListView Control"
            ' Resume layout of the form.
            Me.ResumeLayout(False)
        End Sub 'New


        ' ColumnClick event handler.
        Private Sub ColumnClick(ByVal o As Object, ByVal e As ColumnClickEventArgs)
            ' Set the ListViewItemSorter property to a new ListViewItemComparer 
            ' object. Setting this property immediately sorts the 
            ' ListView using the ListViewItemComparer object.
            Me.listView1.ListViewItemSorter = New ListViewItemComparer(e.Column)
        End Sub

    End Class

    ' Implements the manual sorting of items by columns.
    Class ListViewItemComparer
        Implements IComparer

        Private col As Integer

        Public Sub New()
            col = 0
        End Sub

        Public Sub New(ByVal column As Integer)
            col = column
        End Sub

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
           Implements IComparer.Compare
            Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
        End Function
    End Class
End Namespace

[C#] 
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.AutoScaleBaseSize = new Size(5, 13);
            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);
        }
    }

}

[C++] 
#using <mscorlib.dll>
#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.
__gc class ListViewItemComparer : public IComparer {
private:
    int  col;
public:
    ListViewItemComparer() {
        col=0;
    }
    ListViewItemComparer(int column) {
        col=column;
    }
    int Compare(Object* x, Object* y) {
        return String::Compare((dynamic_cast<ListViewItem*>(x))->SubItems->Item[col]->Text,
            (dynamic_cast<ListViewItem*>(y))->SubItems->Item[col]->Text);
    }
};

public __gc class ListViewSortForm : public Form {
private:
    ListView*  listView1;

public:
    ListViewSortForm() {
        // Create ListView items to add to the control.

        String* temp0 [] = {S"Banana", S"a", S"b", S"c"};
        ListViewItem* listViewItem1 = new ListViewItem(temp0, -1, Color::Empty, Color::Yellow, 0);

        String* temp1 [] = {S"Cherry", S"v", S"g", S"t"};
        ListViewItem* listViewItem2 = new ListViewItem(temp1, -1, Color::Empty, Color::Red,
            new System::Drawing::Font(S"Microsoft Sans Serif", 8.25F, FontStyle::Regular, GraphicsUnit::Point, 0));

        String* temp2 [] = {S"Apple", S"h", S"j", S"n"};
        ListViewItem* listViewItem3 = new ListViewItem(temp2, -1, Color::Empty, Color::Lime, 0);

        String* temp3 [] = {S"Pear", S"y", S"u", S"i"};
        ListViewItem* listViewItem4 = new ListViewItem(temp3, -1, Color::Empty,
            Color::FromArgb(192, 128, 156), 0);

        //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->Item[0]->Text = S"Column 1";
        this->listView1->Columns->Item[0]->Width = 100;
        listView1->Columns->Add(new ColumnHeader());
        listView1->Columns->Item[1]->Text=S"Column 2";
        listView1->Columns->Add(new ColumnHeader());
        listView1->Columns->Item[2]->Text=S"Column 3";
        listView1->Columns->Add(new ColumnHeader());
        listView1->Columns->Item[3]->Text=S"Column 4";
        // Suspend control logic until form is done configuring form.
        this->SuspendLayout();
        // Add Items to the ListView control.

        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 = S"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 += new ColumnClickEventHandler(this, &ListViewSortForm::ColumnClick);

            // Initialize the form.
            this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
            this->ClientSize = System::Drawing::Size(400, 400);

            Control* temp5 [] = {this->listView1};

            this->Controls->AddRange(temp5);
            this->Name = S"ListViewSortForm";
            this->Text = S"Sorted ListView Control";
            // Resume lay->Item[Out] 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]
int main() {
    Application::Run(new ListViewSortForm());
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

ListView クラス | ListView メンバ | System.Windows.Forms 名前空間 | ColumnClick | ColumnClickEventArgs