如图我的删除代码intindex=ListBox1.SelectedIndex;intindex=ListBox1.SelectedIndex;if(index<0){Response.Write("");}else{ListBox1.It...
如图我的删除代码
int index = ListBox1.SelectedIndex;
int index = ListBox1.SelectedIndex;
if (index < 0)
{
Response.Write("");
}
else
{
ListBox1.Items.RemoveAt(index);
}
问题出在当我在里面插入或者添加再循环上下移动后Index的值却没有变,比如最初index的值是2,执行以上操作后我想删除最后一项,实际删除的确实不是最后一项!
帮帮忙。。。。换种想法也行!!
插入:
int i = ListBox1.SelectedIndex;
string item = TextBox1.Text;
ListBox1.Items.Insert(i, item);
循环上移:思路:先把选中项一直下移,当移到最下面的时候与最上面项交换值,然后再继续下移,移动(count-1)次就实现了,for语言用于循环移动,下移同理
int count = ListBox1.Items.Count;
int index = ListBox1.SelectedIndex;
string temp;
for (int i = 0; i < (count - 1); i++)
{
if (index >= (count - 1) || index < 0)
{
//头尾交换
temp = ListBox1.Items[count - 1].Text;
ListBox1.Items[count - 1].Text = ListBox1.Items[0].Text;
ListBox1.Items[0].Text = temp;
index = 0;
}
else
{
&n
展开