ListBox中Item项的位置的变动(转天轰穿的代码)


向上移一位按钮的commandName为up  向下移一位按钮的commandName为down
代码如下:
protected void Button2_Click(object sender, EventArgs e)
    {//ListBox中Item项的位置的变动
        if (((Button)sender).CommandName == "up" && ListBox3.SelectedIndex > 0 || ((Button)sender).CommandName == "down" && ListBox3.SelectedIndex < ListBox3.Items.Count - 1)
        {
            int index;
            if (((Button)sender).CommandName == "up")
            { index = -1; }
            else
            {
                index = 1;
            }
            ListItem lt = new ListItem(ListBox3.SelectedItem.Text,ListBox3.SelectedValue);
            ListBox3.Items[ListBox3.SelectedIndex].Text = ListBox3.Items[ListBox3.SelectedIndex + index].Text;
            ListBox3.Items[ListBox3.SelectedIndex].Value = ListBox3.Items[ListBox3.SelectedIndex + index].Value;
            ListBox3.Items[ListBox3.SelectedIndex + index].Text = lt.Text;
            ListBox3.Items[ListBox3.SelectedIndex + index].Value = lt.Value;
            ListBox3.SelectedIndex = ListBox3.SelectedIndex + index;

        }
    }

你可能感兴趣的:(listbox)