Winfrom 中 ListBox如何实现上移下移,置顶 置底的功能

private void button1_Click(object sender, EventArgs e)
{
   
// 上移
    if (this.listBox1.SelectedIndices.Count > 0 &&
       
this.listBox1.SelectedIndices[0] > 0)
    {
       
int[] newIndices =
           
this.listBox1.SelectedIndices.Cast<int>()
            .Select(index
=> index - 1).ToArray();

       
this.listBox1.SelectedItems.Clear();

       
for (int i = 0; i < newIndices.Length; i++)
        {
           
object obj = this.listBox1.Items[newIndices[i]];
           
this.listBox1.Items[newIndices[i]] = this.listBox1.Items[newIndices[i] + 1];
           
this.listBox1.Items[newIndices[i] + 1] = obj;
           
this.listBox1.SelectedItems.Add(this.listBox1.Items[newIndices[i]]);
        }
    }
}

private void button2_Click(object sender, EventArgs e)
{
   
// 下移
    if (this.listBox1.SelectedIndices.Count > 0 &&
       
this.listBox1.SelectedIndices[this.listBox1.SelectedIndices.Count - 1] <
       
this.listBox1.Items.Count - 1)
    {
       
int[] newIndices =
           
this.listBox1.SelectedIndices.Cast<int>()
            .Select(index
=> index + 1).ToArray();

       
this.listBox1.SelectedItems.Clear();

       
for (int i = newIndices.Length; i > 0; i--)
        {
           
object obj = this.listBox1.Items[newIndices[i - 1]];
           
this.listBox1.Items[newIndices[i - 1]] = this.listBox1.Items[newIndices[i - 1] - 1];
           
this.listBox1.Items[newIndices[i - 1] - 1] = obj;
           
this.listBox1.SelectedItems.Add(this.listBox1.Items[newIndices[i - 1]]);
        }
    }
}

private void button3_Click(object sender, EventArgs e)
{
   
// 置顶
    if (this.listBox1.SelectedIndices.Count > 0 &&
       
this.listBox1.SelectedIndices[0] > 0)
    {
       
int[] oldIndices =
           
this.listBox1.SelectedIndices.Cast<int>().ToArray();

       
int[] newIndices =
           
this.listBox1.SelectedIndices.Cast<int>()
            .Select(index
=> index - 1).ToArray();

       
int offest = oldIndices[0];

       
while (offest-- > 0)
        {
           
this.listBox1.SelectedItems.Clear();

           
for (int i = 0; i < newIndices.Length; i++)
            {
               
object obj = this.listBox1.Items[newIndices[i]];
               
this.listBox1.Items[newIndices[i]] = this.listBox1.Items[newIndices[i] + 1];
               
this.listBox1.Items[newIndices[i] + 1] = obj;
               
this.listBox1.SelectedItems.Add(this.listBox1.Items[newIndices[i]]);
            }

            newIndices
= newIndices.Select(index => index - 1).ToArray();
        }
    }
}

private void button4_Click(object sender, EventArgs e)
{
   
// 置末
    if (this.listBox1.SelectedIndices.Count > 0 &&
       
this.listBox1.SelectedIndices[this.listBox1.SelectedIndices.Count - 1] <
       
this.listBox1.Items.Count - 1)
    {
       
int[] oldIndices =
           
this.listBox1.SelectedIndices.Cast<int>().ToArray();

       
int[] newIndices =
           
this.listBox1.SelectedIndices.Cast<int>()
            .Select(index
=> index + 1).ToArray();

       
int offest = oldIndices[oldIndices.Length - 1];

       
while (offest++ < this.listBox1.Items.Count - 1)
        {
           
this.listBox1.SelectedItems.Clear();

           
for (int i = newIndices.Length; i > 0; i--)
            {
               
object obj = this.listBox1.Items[newIndices[i - 1]];
               
this.listBox1.Items[newIndices[i - 1]] = this.listBox1.Items[newIndices[i - 1] - 1];
               
this.listBox1.Items[newIndices[i - 1] - 1] = obj;
               
this.listBox1.SelectedItems.Add(this.listBox1.Items[newIndices[i - 1]]);
            }

            newIndices
= newIndices.Select(index => index + 1).ToArray();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值