whoah
whoah

Reputation: 4443

DoubleClick on a row in ListView

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?

Upvotes: 21

Views: 68868

Answers (6)

hgjdfhjfhjj
hgjdfhjfhjj

Reputation: 91

I know this thread is old but nobody here answered the question properly in my opinion. For those in the future, try this, from MSDN:

// User must double-click to activate item
myListView.Activation = System.Windows.Forms.ItemActivation.Standard;

// Add event handler
myListView.ItemActivate += new 
System.EventHandler(this.myListView_ItemClick);

Upvotes: 9

Martin Riddar
Martin Riddar

Reputation: 311

Since the accepted answer didn't help me i thought that I would share my solution to the same problem: getting data from a specific column in a listview in the double click event.

The following line returns the data of the second column in the row that I've double clicked on as a string:

private void listViewOutput_DoubleClick(object sender, EventArgs e)
{
   string content = listViewOutput.Items[listViewOutput.SelectedIndices[0]].SubItems[1].Text
}

Upvotes: 3

Shawn
Shawn

Reputation: 228

Try this

private void listView1_MouseClick(object sender, MouseEventArgs e)
{
    ListViewHitTestInfo hit = listView1.HitTest(e.Location);

    Rectangle rowBounds = hit.SubItem.Bounds;
    Rectangle labelBounds = hit.Item.GetBounds(ItemBoundsPortion.Label);
    int leftMargin = labelBounds.Left - 1;

    string x = hit.Item.Text;

}

Upvotes: 2

Gary Huckabone
Gary Huckabone

Reputation: 412

Thanks; this is what I needed. I thought I'd also mention one could set up the local info variable more generally as:

  ListViewHitTestInfo info = ((ListView)sender).HitTest(e.X, e.Y);

Upvotes: 2

XIVSolutions
XIVSolutions

Reputation: 4502

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";
        }
    }
}

Upvotes: 32

Daniil Grankin
Daniil Grankin

Reputation: 3933

Try this:

private void lvLista_DoubleClick(object sender, EventArgs e)
{

    MessageBox.Show(lvLista.SelectedItems[0].SubItems[0].Text);
}

Upvotes: 23

Related Questions