列表框切换项目向上 [英] listbox switch item in up down

查看:58
本文介绍了列表框切换项目向上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按下按钮点击向下按下swtich项目的列表框控件...

i have listbox control to swtich item up down in button click ...

推荐答案

这不是太难 - 我用一个选定的曲目列表我的音乐播放器:

It's not too difficult - I do with with a list of selected tracks in my music player:
/// <summary>
/// Move all tracks at indexes up one row
/// </summary>
/// <param name="indexes"></param>
public void MoveUp(IEnumerable<int> indexes)
    {
    List<Track> tracks = new List<Track>();
    List<Track> insertThese = new List<Track>();
    int[] indexesSorted = indexes.ToArray();
    Array.Sort(indexesSorted);
    ExtractTracksByIndex(_Playlist, tracks, insertThese, indexes);
    int newIndex = indexesSorted.FirstOrDefault() - 1;
    newIndex = Math.Min(newIndex, tracks.Count);
    newIndex = Math.Max(newIndex, 0);
    tracks.InsertRange(newIndex, insertThese);
    _Playlist.Tracks = tracks;
    Selected = insertThese;
    }
/// <summary>
/// Move all tracks at indexes down one row
/// </summary>
/// <param name="indexes"></param>
public void MoveDown(IEnumerable<int> indexes)
    {
    List<Track> tracks = new List<Track>();
    List<Track> insertThese = new List<Track>();
    int[] indexesSorted = indexes.ToArray();
    Array.Sort(indexesSorted);
    ExtractTracksByIndex(_Playlist, tracks, insertThese, indexesSorted);
    int newIndex = indexesSorted.FirstOrDefault() + 1;
    newIndex = Math.Min(newIndex, tracks.Count);
    newIndex = Math.Max(newIndex, 0);
    tracks.InsertRange(newIndex, insertThese);
    _Playlist.Tracks = tracks;
    Selected = insertThese;
    }

我向他们传递要移动的项目索引的列表,并从列表中删除它们(按相反顺序,最高索引优先)然后重新插入新位置。最后一位确保之后重新选择所选曲目。

I pass them a list of the item indexes to move, and they are removed from the list (in reverse order, highest index first) then re-inserted at the new location. This final bit just makes sure the selected tracks are re-selected afterwards.


这篇关于列表框切换项目向上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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