ASP.NET 中listbox实现上移下移置顶置底,(支持多选)

时间:2021-08-04 21:14:50

/// <summary>
 /// 得到选定项的值
 /// </summary>
 /// <returns></returns>
 protected string[] getSelectedValue()
 {
  ArrayList a = new ArrayList();
  for (int i = 0; i < ListBoxNewPaper.Items.Count; i++)
  {
   if (ListBoxNewPaper.Items[i].Selected)
   {
    a.Add(i);
   }
  }
  int[] indices = new int[a.Count];  //得到选中项的索引集合
  a.CopyTo(indices);

  string[] strs = new string[indices.Length];
  for (int i = 0; i < indices.Length; i++)
  {
   strs[i] = ListBoxNewPaper.Items[indices[i]].Value;
  }

  return strs;
 }

 /// <summary>
 /// 得到选定项的内容
 /// </summary>
 /// <returns></returns>
 protected string[] getSelectedText()
 {
  ArrayList a = new ArrayList();
  for (int i = 0; i < ListBoxNewPaper.Items.Count; i++)
  {
   if (ListBoxNewPaper.Items[i].Selected)
   {
    a.Add(i);
   }
  }
  int[] indices = new int[a.Count];  //得到选中项的索引集合
  a.CopyTo(indices);
  string[] strs = new string[indices.Length];
  for (int i = 0; i < indices.Length; i++)
  {
   strs[i] = ListBoxNewPaper.Items[indices[i]].Text;
  }
  return strs;
 }

 /// <summary>
 /// 上移
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void btnUp_Click(object sender, EventArgs e)
 {
  if (ListBoxNewPaper.SelectedIndex > 0)
  {
   string[] newValue = getSelectedValue();
   string[] newText = getSelectedText();

   ArrayList a = new ArrayList();
   for (int i = 0; i < ListBoxNewPaper.Items.Count; i++)
   {
    if (ListBoxNewPaper.Items[i].Selected)
    {
     a.Add(i);
    }
   }
   int[] indices = new int[a.Count];  //得到选中项的索引集合
   a.CopyTo(indices);

   for (int i = 0; i < indices.Length; i++)
   {
    int newIndex = indices[i];
    //交换位置
    ListBoxNewPaper.Items[newIndex].Value = ListBoxNewPaper.Items[newIndex - 1].Value;
    ListBoxNewPaper.Items[newIndex].Text = ListBoxNewPaper.Items[newIndex - 1].Text;
    ListBoxNewPaper.Items[newIndex - 1].Value = newValue[i];
    ListBoxNewPaper.Items[newIndex - 1].Text = newText[i];

    ListBoxNewPaper.Items[newIndex - 1].Selected = true;
   }
   ListBoxNewPaper.Items[indices[indices.Length - 1]].Selected = false;
  }
 }

 /// <summary>
 /// 下移
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void btnDown_Click(object sender, EventArgs e)
 {
  string[] newValue = getSelectedValue();
  string[] newText = getSelectedText();

  ArrayList a = new ArrayList();
  for (int i = 0; i < ListBoxNewPaper.Items.Count; i++)
  {
   if (ListBoxNewPaper.Items[i].Selected)
   {
    a.Add(i);
   }
  }
  int[] indices = new int[a.Count];  //得到选中项的索引集合
  a.CopyTo(indices);

  if (indices[a.Count - 1] < ListBoxNewPaper.Items.Count - 1)  //选中项的最大索引小于列表总项数
  {
   for (int i = indices.Length - 1; i >= 0; i--)
   {
    int newIndex = indices[i];

    //交换位置
    ListBoxNewPaper.Items[newIndex].Value = ListBoxNewPaper.Items[newIndex + 1].Value;
    ListBoxNewPaper.Items[newIndex].Text = ListBoxNewPaper.Items[newIndex + 1].Text;
    ListBoxNewPaper.Items[newIndex + 1].Value = newValue[i];
    ListBoxNewPaper.Items[newIndex + 1].Text = newText[i];

    ListBoxNewPaper.Items[newIndex + 1].Selected = true;
   }
   ListBoxNewPaper.Items[indices[0]].Selected = false;
  }
 }

 /// <summary>
 /// 置顶
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void btnTop_Click(object sender, EventArgs e)
 {
  string[] newValue = getSelectedValue();
  string[] newText = getSelectedText();

  ArrayList a = new ArrayList();
  for (int i = 0; i < ListBoxNewPaper.Items.Count; i++)
  {
   if (ListBoxNewPaper.Items[i].Selected)
   {
    a.Add(i);
   }
  }
  int[] indices = new int[a.Count];  //得到选中项的索引集合
  a.CopyTo(indices);

  for (int i = 0; i < indices.Length; i++)
  {
   int newIndex = indices[i];

   for (int j = newIndex - 1; j >= 0; j--)
   {
    ListBoxNewPaper.Items[j + 1].Text = ListBoxNewPaper.Items[j].Text;
    ListBoxNewPaper.Items[j + 1].Value = ListBoxNewPaper.Items[j].Value;
   }
   ListBoxNewPaper.Items[0].Value = newValue[indices.Length - 1 - i];
   ListBoxNewPaper.Items[0].Text = newText[indices.Length - 1 - i];

   ListBoxNewPaper.Items[newIndex].Selected = false;
   ListBoxNewPaper.Items[i].Selected = true;
  }
 }

 /// <summary>
 /// 置底
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void btnFoot_Click(object sender, EventArgs e)
 {
  string[] newValue = getSelectedValue();
  string[] newText = getSelectedText();

  ArrayList a = new ArrayList();
  for (int i = 0; i < ListBoxNewPaper.Items.Count; i++)
  {
   if (ListBoxNewPaper.Items[i].Selected)
   {
    a.Add(i);
   }
  }
  int[] indices = new int[a.Count];  //得到选中项的索引集合
  a.CopyTo(indices);

  for (int i = indices.Length - 1; i >= 0; i--)
  {
   int newIndex = indices[i];

   //获取总的数目
   int newCount = ListBoxNewPaper.Items.Count;

   for (int j = newIndex; j < newCount - 1; j++)
   {
    ListBoxNewPaper.Items[j].Text = ListBoxNewPaper.Items[j + 1].Text;
    ListBoxNewPaper.Items[j].Value = ListBoxNewPaper.Items[j + 1].Value;
   }
   ListBoxNewPaper.Items[newCount - 1].Value = newValue[indices.Length - 1 - i];
   ListBoxNewPaper.Items[newCount - 1].Text = newText[indices.Length - 1 - i];

   ListBoxNewPaper.Items[newIndex].Selected = false;
   ListBoxNewPaper.Items[newCount - 1 - (indices.Length - 1 - i)].Selected = true;
  }
 }