实现ListBox1的Item项上移与下移!!!
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
ArrayList al = new ArrayList();
al.Add("1");
al.Add("2");
al.Add("3");
al.Add("4");
al.Add("5");
al.Add("6");
ListBox1.DataSource = al;
ListBox1.DataBind();
}
//向下
private void Button2_Click(object sender, System.EventArgs e)
{
if(ListBox1.SelectedIndex<ListBox1.Items.Count-1)
{
//交换位置
string strTempUp = ListBox1.Items[ListBox1.SelectedIndex].Text;
ListBox1.Items[ListBox1.SelectedIndex].Text = ListBox1.Items[ListBox1.SelectedIndex+1].Text;
ListBox1.Items[ListBox1.SelectedIndex+1].Text = strTempUp;
//选中下一个Item项
ListBox1.SelectedIndex = ListBox1.SelectedIndex+1;
ListBox1.Items[ListBox1.SelectedIndex].Selected = true;
}
}
//向上
private void Button1_Click(object sender, System.EventArgs e)
{
if(ListBox1.SelectedIndex>0)
{
//交换位置
string strTempUp = ListBox1.Items[ListBox1.SelectedIndex].Text;
ListBox1.Items[ListBox1.SelectedIndex].Text = ListBox1.Items[ListBox1.SelectedIndex-1].Text;
ListBox1.Items[ListBox1.SelectedIndex-1].Text = strTempUp;
//选中上一个Item项
ListBox1.SelectedIndex = ListBox1.SelectedIndex - 1;
ListBox1.Items[ListBox1.SelectedIndex].Selected=true;
}
浙公网安备 33010602011771号