【问题标题】:Rename item in a listview c# WinForms重命名列表视图中的项目c#WinForms
【发布时间】:2011-05-31 17:58:41
【问题描述】:

我想知道如何以传统的 select>F2>edit>enter 方式更改 ListView 项的名称。如何打开项目上方的那个可编辑的小文本框?

【问题讨论】:

    标签: c# winforms listview rename


    【解决方案1】:

    将 LabelEdit 属性设置为 true。添加 KeyDown 事件处理程序以识别 F2 击键。像这样:

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

    【讨论】:

    • 谢谢,我也用 AfterLabelEdit 事件在编辑后做一些逻辑,:)
    • 这是给 ListBox 的吗?听起来像 ListView!
    • 当然,ListBox 不支持编辑。下次对问题投反对票,而不是正确答案。
    【解决方案2】:

    最简单的答案是使用内置此功能的 ListView。
    LabelEdit property 设置为 true。

    【讨论】:

      【解决方案3】:

      ListBox 没有可用于更新文本的所选项目的任何属性。它是一个对象或任何类型,而不是简单的文本。正如 SLaks 所提到的,您可以像 ListBox 一样呈现 ListView,并使用 LabelEdit 事件来修改所选 Item 的文本。

      【讨论】:

        【解决方案4】:
        private void listView1_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyData == Keys.F2 && listView1.SelectedItems.Count > 0) {
               listView1.LabelEdit = true; 
               listView1.SelectedItems[0].BeginEdit();
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2011-12-16
          • 2019-04-27
          • 2020-08-30
          • 2015-07-15
          • 1970-01-01
          • 2012-08-07
          • 2018-11-16
          • 1970-01-01
          相关资源
          最近更新 更多