请告诉我最好的方式来换用C#两个ListView的项目? [英] Whats the best way to swap two ListView items in C#?

查看:289
本文介绍了请告诉我最好的方式来换用C#两个ListView的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最新最好的办法换用C#两个ListView的项目?我看到,标准的ListView没有实现这样的功能。

Whats the best way to swap two ListView items in C#? I see that the standard ListView doesn't implement such functionality.

- 诚挚的问候, 穆拉特

-- Best Regards, Murat

推荐答案

如果您使用自定义ListViewItem的,或对象无法克隆的对象,或股票字符串:

If you use custom ListViewItem, or object you cannot clone object, or stock in string:

enum Direction { UP = -1, DOWN = +1};

void ListViewMove(ListView lv, Direction direction)
{
    if (lv.SelectedItems.Count > 0)
    {
        int selIdx = lv.SelectedItems[0].Index;
        ListViewItem tmp = lv.Items[selIdx] ;

        if ( ( (selIdx != 0) && direction == Direction.UP ) ||
            ((selIdx!=lv.Items.Count-1) && (direction == Direction.DOWN)) )
        {          
            lv.Items.RemoveAt(selIdx);
            tmp = lv.Items.Insert(selIdx + (int)direction, tmp);
            tmp.Selected = true;
        }
    }
    lv.Focus();
}

这篇关于请告诉我最好的方式来换用C#两个ListView的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆