简体   繁体   English

重命名listview c#WinForms中的项目

[英]Rename item in a listview c# WinForms

i would like to know how can i change the name of a ListView item in the traditional select>F2>edit>enter manner. 我想知道如何在传统的选择> F2>编辑>输入方式中更改ListView项目的名称。 How can i open that little editable textbox just over the item? 如何在项目上方打开那个小的可编辑文本框?

Set the LabelEdit property to true. 将LabelEdit属性设置为true。 Add a KeyDown event handler to recognize the F2 keystroke. 添加KeyDown事件处理程序以识别F2键击。 Like this: 像这样:

    private void listView1_KeyDown(object sender, KeyEventArgs e) {
        if (e.KeyData == Keys.F2 && listView1.SelectedItems.Count > 0) {
            listView1.SelectedItems[0].BeginEdit();
        }
    }

The simplest answer is to use a ListView, which has this feature built-in. 最简单的答案是使用内置此功能的ListView。
Set the LabelEdit property to true. LabelEdit属性设置为true。

ListBox doesn't have any property of the item selected which can be used to update the text. ListBox没有所选项目的任何属性,可用于更新文本。 It is an object or any type, not a simple text. 它是一个对象或任何类型,而不是一个简单的文本。 As SLaks mentioned, you can present ListView like a ListBox and use LabelEdit event to modify the text of selected Item. 正如SLaks所提到的,您可以像ListBox一样呈现ListView,并使用LabelEdit事件来修改所选项目的文本。

private void listView1_KeyDown(object sender, KeyEventArgs e) {
    if (e.KeyData == Keys.F2 && listView1.SelectedItems.Count > 0) {
       listView1.LabelEdit = true; 
       listView1.SelectedItems[0].BeginEdit();
    }
}

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

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