如何在ListView上下移动项目? [英] How to move items up and down a ListView?

查看:77
本文介绍了如何在ListView上下移动项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图项,当我点击ADD按钮添加一些项目并且名称显示在列表框中时,我有两个按钮,如UP和DOWN,我在列表框中有更多50个项目名称,默认列表框中选中的任何一项我希望所选项目向上或向下移动(当我点击向上或向下按钮时)



如何编写编码为此?

I have a list view items, when i click the ADD Button some items adding and that the names shows in the list box, i have two buttons like UP and DOWN, I have some more 50 items names in the list box, default any one items selected in the list box i want to selected items moves up or down (when i click the UP or DOWN Button)

How can i write the coding for this?

推荐答案

当您选择列表项时,单击下移

该时间获取所选列表项的当前索引

然后从选定索引中删除项目

并通过Insert with Previous Selected Index + 1添加相同的Item对象



向上移动,减去1即先前选择的指数 - 1



请参考这些链接



http://stackoverflow.com/questions/4796109 /如何对莫ve-item-in-listbox-up-down-down [ ^ ]



http://www.c-sharpcorner.com/uploadfile/dpatra/move-updown-listboxitem-in-listbox- in-wpf / [ ^ ]
When you select a List Item click on Move Down
That time get the Current Index of the Selected List Item
Then remove the item from the Selected Index
And add the same Item object by Insert with Previous Selected Index + 1

For move up, subtract it by 1 i.e. previous selected index - 1

please refer these link

http://stackoverflow.com/questions/4796109/how-to-move-item-in-listbox-up-and-down[^]

http://www.c-sharpcorner.com/uploadfile/dpatra/move-updown-listboxitem-in-listbox-in-wpf/[^]


private void btnUp_Click(object sender, EventArgs e)
{
    this.checkedListBoxAttribute.ItemCheck -= checkedListBoxAttribute_ItemCheck;
    this.checkedListBoxAttribute.SelectedIndexChanged -= checkedListBoxAttribute_SelectedIndexChanged;
    string item = checkedListBoxAttribute.SelectedItem.ToString();
    int selectIndex = checkedListBoxAttribute.SelectedIndex;
    bool state = checkedListBoxAttribute.GetItemChecked(selectIndex);
    checkedListBoxAttribute.Items.RemoveAt(checkedListBoxAttribute.SelectedIndex);
    checkedListBoxAttribute.Items.Insert(selectIndex - 1, item);
    checkedListBoxAttribute.SetItemChecked(selectIndex - 1, state);
    this.checkedListBoxAttribute.ItemCheck +=checkedListBoxAttribute_ItemCheck;
    this.checkedListBoxAttribute.SelectedIndexChanged+=checkedListBoxAttribute_SelectedIndexChanged;
    checkedListBoxAttribute.SelectedIndex = selectIndex - 1;
}










private void btnDown_Click(object sender, EventArgs e)
{
    this.checkedListBoxAttribute.ItemCheck -= checkedListBoxAttribute_ItemCheck;
    this.checkedListBoxAttribute.SelectedIndexChanged -= checkedListBoxAttribute_SelectedIndexChanged;
    string item = checkedListBoxAttribute.SelectedItem.ToString();
    int selectIndex = checkedListBoxAttribute.SelectedIndex;
    bool state = checkedListBoxAttribute.GetItemChecked(selectIndex);
    checkedListBoxAttribute.Items.RemoveAt(checkedListBoxAttribute.SelectedIndex);
    checkedListBoxAttribute.Items.Insert(selectIndex + 1, item);
    checkedListBoxAttribute.SetItemChecked(selectIndex + 1, state);
    this.checkedListBoxAttribute.ItemCheck += checkedListBoxAttribute_ItemCheck;
    this.checkedListBoxAttribute.SelectedIndexChanged += checkedListBoxAttribute_SelectedIndexChanged;
    checkedListBoxAttribute.SelectedIndex = selectIndex + 1;
}


这篇关于如何在ListView上下移动项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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