【问题标题】:Listview - AfterLabelEdit event is called again and is causing issues with my code. c# .NETListview - 再次调用 AfterLabelEdit 事件并导致我的代码出现问题。 c# .NET
【发布时间】:2016-03-25 05:25:40
【问题描述】:

在尝试找到解决方案后,我看不到任何方法可以让我的代码完全满足我的需要,由于导致异常,我找到了问题所在,但我不知道如何解决它如果这是我需要的。

我正在使用 ListView 和 AfterLabelEdit 事件,当用户正在编辑标签时,在编辑后调用事件,然后在 AfterLabelEdit 内部,如果 ListView 的项目与项目文本存在,我将再次编辑用户输入。

我遇到的问题是:

如果名称与列表视图中的项目匹配,则调用 lvInterfaces_AfterLableEdit 2 次,这会导致“e.label”为空并且这不适用于条件(e.Label == lvInterfaces.Items[ItemIndex].文字)

知道如何解决这个问题吗?

这是我的代码:

    private void lvInterfaces_AfterLabelEdit(object sender, LabelEditEventArgs e)
    {          
       for (int ItemIndex = 0; ItemIndex < lvInterfaces.Items.Count - 1; ItemIndex++)
       {
           if (e.Label == lvInterfaces.Items[ItemIndex].Text)
           {                   
               MessageBox.Show("Interface already exist, please select another name.", "Interface Duplicated", MessageBoxButtons.OK);
               lvInterfaces.Items[lvInterfaces.Items.Count - 1].BeginEdit();                   
               return;
           }
       }

       if (cAction == 'R')
           file.XmlInterfaceRename(e.Label, sOldInterfaceName);
       else if (cAction == 'N')
           file.XmlNewInterface(e.Label);
       else if (cAction == 'C')
           file.CopyInterface(e.Label, InterfaceSelected);            
    }

【问题讨论】:

  • 也许在 (?) 再次调用 BeginEdit 之前设置了 e.CancelEdit 参数?
  • @TaW 如果 e.label = lvInterfaces.Items[ItemIndex].text 匹配,那么它将使用这一行: lvInterfaces.Items[lvInterfaces.Items.Count - 1].BeginEdit ();所以这条线再次调用了 lvInterfaces_AfterLabelEdit

标签: c# .net listview label


【解决方案1】:

问题解决了!

比我预期的要容易:/

只需添加 e.label == null

如果(e.Label == lvInterfaces.Items[ItemIndex].Text || e.Label == null)

【讨论】:

    猜你喜欢
    • 2017-08-29
    • 2012-03-16
    • 2015-03-18
    • 2014-04-12
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    相关资源
    最近更新 更多