Beak
Beak

Reputation: 93

C# Winform Listview selected item issue

I'm trying to copy the selected item's filename and its path to the clipboard and then a textbox from a listview. I can't seem to get this one to work how i want. Here's the code I've been playing around with.

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (listView1.Items.Count > 0)
    {
        listView1.Items[0].Selected = true;
        Clipboard.SetDataObject(this.listView1.SelectedItems[0]);
        textBox1.Paste();
    }
}

Can someone get me on the right track?

Upvotes: 0

Views: 4582

Answers (2)

Hans Passant
Hans Passant

Reputation: 941217

    private void listView1_SelectedIndexChanged(object sender, EventArgs e) {
        if (listView1.SelectedItems.Count > 0) {
            textBox1.Text = listView1.SelectedItems[0].Text;
        }
        else {
            textBox1.Text = string.Empty;
        }
    }

Upvotes: 1

Austin Salonen
Austin Salonen

Reputation: 50215

I'm not sure why you're using the Clipboard here. You can do just fine without it.

listView1.Items[0].Selected = true;
textBox1.Text = this.listView1.SelectedItems[0].ToString();

Upvotes: 3

Related Questions