简体   繁体   English

C#Winform Listview选择的项目问题

[英]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? 有人可以让我走上正确的路吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM