Order of events when clicked in C# listvew

When I created a function to process a list view when it is selected by mouse click, I checked the order of events.

Create a list view like this.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Sample01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.Items.Add("test01");
            listView1.Items.Add("test02");
            listView1.Items.Add("test03");
        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            Console.WriteLine("MouseClick");
        }

        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            Console.WriteLine("MouseUp");
        }

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            Console.WriteLine("MouseDown");
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            Console.WriteLine("ItemSelectionChanged; selected=" + e.IsSelected);
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Console.WriteLine("SelectedIndexChanged" );
        }

        private void listView1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Click");
        }

        private void listView1_ItemActivate(object sender, EventArgs e)
        {
            Console.WriteLine("ItemActivate");
        }

    }
}

Click on “test01” at the top.

Execution Result
————————————————–
MouseDown
ItemSelectionChanged; selected=True
SelectedIndexChanged
Click
MouseClick
MouseUpMouseUp
————————————————–

I imagine that the selection is changed after Click, but the Click event and MouseClick come after ItemSelectionChanged and SelectedIndexChanged.

Next, after selecting “test01”, select “test02”.

Execution Result
————————————————–
MouseDown
ItemSelectionChanged; selected=False
SelectedIndexChanged
ItemSelectionChanged; selected=True
SelectedIndexChanged
Click
MouseClick
MouseUp
————————————————–

After “test01” is deselected, “test02” is selected.

Now deselect “test02” by clicking on the white area where there are no items.


Execution Result
————————————————–
MouseDown
ItemSelectionChanged; selected=False
SelectedIndexChanged
MouseUp
————————————————–

Click only runs when an item is selected.

Double-click “test01” with nothing selected.


Execution Result
————————————————–
MouseDown
ItemSelectionChanged; selected=True
SelectedIndexChanged
Click
MouseClick
MouseUp
MouseDown
ItemActivate
MouseUp
————————————————–

Double-click to run ItemActivate.

コメント

Copied title and URL