【问题标题】:c# ListView item selection issuec# ListView项目选择问题
【发布时间】:2010-04-16 14:11:34
【问题描述】:

我在表单上有一个 ListView 和删除按钮

我可以选择任何项目并按删除按钮删除该项目 (我已禁用多选)

要求:当我删除一个项目时,应该选择下一个项目 如果底部项目被删除,那么它应该选择上一个项目

我如何实现它

【问题讨论】:

    标签: c# listview


    【解决方案1】:

    也许您可以使用 SelectedIndices 集合来实现它:

    if (lviList.SelectedIndices.Count == 0) return;
    var ind = lviList.SelectedIndices[0];
    int nextIndex;
    if (ind == lviList.Count) {
        nextIndex = ind - 1;
    } else {
        // when you remove, current index will be next item
        nextIndex = ind;    
    }
    
    DeleteItem(ind);
    lviList.SelectedIndex = nextIndex;
    

    【讨论】:

    • 很高兴为您提供帮助...如果对您有帮助,请接受答案
    猜你喜欢
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多