Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DoubleClick on a row in ListView

Tags:

c#

.net

winforms

Is there any possibility to get a value of doubleclicked row in ListView? I registered an event:

    private void lvLista_DoubleClick(object sender, EventArgs e)
    {
        MessageBox.Show(lvLista.SelectedItems.ToString());
    }

But on message, when i doubleclick some row in listview i get:

System.Windows.Forms.ListView+SelectedListViewItemCollection

What is more, I have got 2 columns in listview:

    lvLista.Columns.Add("ID");
    lvLista.Columns.Add("Tilte");

And i want to show in messagebox the "ID" of doubleclicked row.
How to do it? How to get a values from this event?

like image 817
whoah Avatar asked Oct 13 '12 11:10

whoah


1 Answers

If you handle the MouseDown and/or MouseDoubleClick events of the ListView control, and use the HitTest method to determine the target of the mouse action, you will know which item has been double clicked. This is also a good means to determine if NO item was clicked (for example, clicking on the empty area in a partially filled list.

The following code will display the clicked item in a textbox if a single click occurs, and will pop up a message box with the name of the double-clicked item if a double click occurs.

If the click or double click occur in an area of the list view not populated by an item, the text box or message box inform yopu of that fact.

This is a trivial example, and depending on your needs, you will have to mess with it a little.

UPDATE: I added some code which clears the SelectedItems property of the Listview control when an empty area of the list is clicked or double-clicked.

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();

        listView1.MouseDown += new MouseEventHandler(listView1_MouseDown);
        listView1.MouseDoubleClick += new MouseEventHandler(listView1_MouseDoubleClick);
        this.Load += new EventHandler(Form1_Load);
    }


    void Form1_Load(object sender, EventArgs e)
    {
        this.SetupListview();
    }


    private void SetupListview()
    {
        ListView lv = this.listView1;
        lv.View = View.List;


        lv.Items.Add("John Lennon");
        lv.Items.Add("Paul McCartney");
        lv.Items.Add("George Harrison");
        lv.Items.Add("Richard Starkey");
    }


    void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
        ListViewItem item = info.Item;

        if (item != null)
        {
            MessageBox.Show("The selected Item Name is: " + item.Text);
        }
        else
        {
            this.listView1.SelectedItems.Clear();
            MessageBox.Show("No Item is selected");
        }
    }



    void listView1_MouseDown(object sender, MouseEventArgs e)
    {
        ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
        ListViewItem item = info.Item;

        if (item != null)
        {
            this.textBox1.Text = item.Text;
        }
        else
        {
            this.listView1.SelectedItems.Clear();
            this.textBox1.Text = "No Item is Selected";
        }
    }
}
like image 141
XIVSolutions Avatar answered Sep 21 '22 14:09

XIVSolutions