sara brown
sara brown

Reputation: 1067

double click item in listview c#

i still cannot understand how to make the item in listview to be clickable and appear messagebox.

this is my code.

private void button6_Click(object sender, EventArgs e)
    {
        ListViewItem listviewitem;

        listviewitem = new ListViewItem("John");
        listviewitem.SubItems.Add("Smith");
        listviewitem.SubItems.Add("kaya");
        listviewitem.SubItems.Add("bun");
        this.listView1.Items.Add(listviewitem);
        listView1.FullRowSelect = true;

//show header listView1.View = View.Details;

        // Loop through and size each column header to fit the column header text.
        foreach (ColumnHeader ch in this.listView1.Columns)
        {
            ch.Width = -2;
        }

}

i have read itemActive and i have tried this. im adding listView1.SelectedItems[0].Text

    private void button6_Click(object sender, EventArgs e)
        {
            ListViewItem listviewitem;

            listviewitem = new ListViewItem("John");
            listviewitem.SubItems.Add("Smith");
            listviewitem.SubItems.Add("kaya");
            listviewitem.SubItems.Add("bun");
            this.listView1.Items.Add(listviewitem);
            listView1.FullRowSelect = true;

            MessageBox.Show("You clicked " + listView1.SelectedItems[0].Text);
}

this printout error

argument out of range. '0' is not valid number of index.

please help me with this.

Upvotes: 1

Views: 16718

Answers (3)

ABH
ABH

Reputation: 3439

On button6_Click() only add newly created listviewitem in listView1

private void button6_Click(object sender, EventArgs e)
{
    ListViewItem listviewitem;

    listviewitem = new ListViewItem("John");
    listviewitem.SubItems.Add("Smith");
    listviewitem.SubItems.Add("kaya");
    listviewitem.SubItems.Add("bun");
    this.listView1.Items.Add(listviewitem);
    listView1.FullRowSelect = true;

    //MessageBox.Show("You clicked " + listView1.SelectedItems[0].Text);
}

Register the Click event against listView1

this.listView1.Click += new System.EventHandler(this.listView1_Click);

And in it's event handler check for listView1.SelectedItems[0]

private void listView1_Click(object sender, EventArgs e)
{
     if(listView1.SelectedItems.Count > 0)
             MessageBox.Show("You clicked " + listView1.SelectedItems[0].Text);
}

For documentation, please check MSDN

Upvotes: 6

Jeremy Thompson
Jeremy Thompson

Reputation: 65554

You haven't actually selected an item and thats when you get the error:

InvalidArgument=Value of '0' is not valid for 'index'.

Put some protection to check an item is selected:

if (listView1.SelectedItems.Count > 0)
{
    MessageBox.Show("You clicked " + listView1.SelectedItems[0].Text);
}
else
{
     MessageBox.Show("Please select an item");
}

Or select the item in code:

private void button6_Click(object sender, EventArgs e)
    {
        ListViewItem listviewitem;

        listviewitem = new ListViewItem("John");
        listviewitem.SubItems.Add("Smith");
        listviewitem.SubItems.Add("kaya");
        listviewitem.SubItems.Add("bun");
        this.listView1.Items.Add(listviewitem);
        //listView1.FullRowSelect = true;
        listView1.Items[0].Selected = true;
        MessageBox.Show("You clicked " + listView1.SelectedItems[0].Text);
}

Upvotes: 1

Vinoth
Vinoth

Reputation: 2439

Are you sure there is an active item? Possibly because you have not selected any item in the list. Make a check on the selected items,

 string msg = (listView1.SelectedItems.Count >0) ? "You Clicked " + 
listView1.SelectedItems[0].Text  : "Please select an item.";

MessageBox.Show(msg);

Upvotes: 0

Related Questions