我用下面的语句要将listbox1中的选定项移动到listbox2
for(int i=0;i<this.ListBox1.Items.Count;i++)
{
    if(this.ListBox1.Items[i].Selected==true)
    {
    this.ListBox2.Items.Add(new ListItem(ListBox1.Items[i].Text,this.ListBox1.Items[i].Value));
this.ListBox1.Items.Remove(ListBox1.Items[i]);
    i=i-1;
    }
}
有的页面可以实现,有的页面却出现这样一个怪现象:不管选什么,是不是多选,总是将 listbox1的items[0]这一项移动到listbox2
百思不得其解
绑定数据的语句是放在if(!this.ispostback)里的
还有就是在页面的一个dropdownlist的选中项变化的时候重新绑定数据
代码从这个页面复制到那个页面后也重新注册了事件
到底是哪里疏忽了?大家帮找一下原因

解决方案 »

  1.   

    for (int i = this.ListBox1.Items.Count-1; i>=0 ; i++)
                {
                    if (this.ListBox1.Items[i].Selected == true)
                    {
                        this.ListBox2.Items.Add(new ListItem(ListBox1.Items[i].Text, this.ListBox1.Items[i].Value));
                        this.ListBox1.Items.Remove(ListBox1.Items[i]);
                        i = i - 1;
                    }
                }
      

  2.   

    for (int i = this.ListBox1.Items.Count-1; i>=0 ; i--)
                {
                    if (this.ListBox1.Items[i].Selected == true)
                    {
                        this.ListBox2.Items.Add(new ListItem(ListBox1.Items[i].Text, this.ListBox1.Items[i].Value));
                        this.ListBox1.Items.Remove(ListBox1.Items[i]);
                        i = i - 1;
                    }
                }
      

  3.   

    代码我是复制过来的,因为这几个页面基本相同,控件的ID是一样的,复制过来以后就是listbox里面绑定的数据不一样,事件重新注册一下,断点调试发现:
    ListBox1.Items[0].Selected的值不论我选不选都为true,其他的都是false
    为什么会这样?
      

  4.   

    第一个问题,在操作listbox这类的控件,如果有add或者remove时,是不能使用for循环的。
    假如在remove之前,listbox的item的length = 5,当add过后,就会变成4,当下一次for循环时就会少循环一次,所以,要使用while循环这类的控件
      

  5.   

    上面说的应该是remove,呵呵第二个问题,说的不清楚,没看懂
      

  6.   

    LS的
    我的for循环没问题,已经是可以实现移动的
    for(int i=0;i<this.ListBox1.Items.Count;i++)
    {
        if(this.ListBox1.Items[i].Selected==true)
        {
        this.ListBox2.Items.Add(new ListItem(ListBox1.Items[i].Text,this.ListBox1.Items[i].Value));
    this.ListBox1.Items.Remove(ListBox1.Items[i]);
        i=i-1;//如果移走了,那我这里就自己手动的-1了
        }
    }
      

  7.   

    i=i-1;//如果移走了,那我这里就自己手动的-1了
    --!
    i-1了但是循环次数实际又多了一次!你要的是减少循环次数!所以要i=i+1
      

  8.   

    i-1了但是循环次数实际又多了一次!你要的是减少循环次数!所以要i=i+1
    但这样会不会有item没被遍历到了?
    所以循环次数是不定的,建议你用foreach方法
      

  9.   

    foreach枚举
    当枚举元素发生改变的时候不允许ADD,REMOVE操作好不好
    为什么i-1:
    这个项被移走了,那么他下面的一个项自然就顶替了他原来的位置,移走原来的项以后,我要对原来项的下一个项进行判断,那么i的值应该是不变的,所以我在if里面i-1,for了以后就相当与i-1+1=i了;
      

  10.   

    foreach枚举
    当枚举元素发生改变的时候不允许ADD,REMOVE操作好不好
    呵呵,我以前用的不是你这样的,我用的比较麻烦是把listbox项目统统转移到一个arraylist里面,这样就能用foreach,所以惯性的说用foreach,不过你i-1后循环次数没变啊?
      

  11.   

    如果超出了index,它会报错的!
      

  12.   

    比如listbox本来有6个项目,你移出来一个,就有5个项目了,那么你的循环还是会存在lstbox【5】这样的情况,但是它不存在
      

  13.   

    每次remove掉一项后 ListBox1.Items.Count也会-1的
      

  14.   

    做Remove这件事,循环的索引要从大到小写不容易出错
      

  15.   

    试试这样吧,没有测试过,基本的思路就是从最后一个item向前循环,这样当你删除最后一个item的时候,index不会被打乱--
    for ( int i = ListBox1.Items.Count - 1; i >= 0; i-- )
    {
        if ( ListBox1.Items[i].Selected )
        {
            ListBox2.Items.Add(ListBox1.Items[i]);
            ListBox1.Items.RemoveAt(i);
        }
    }
      

  16.   

    ddl.SelectedIndex = -1;
    前面加上这个
      

  17.   

    ListBox1.SelectedIndex = -1;
    for ( int i = ListBox1.Items.Count - 1; i >= 0; i-- )
    {
        if ( ListBox1.Items[i].Selected )
        {
            ListBox2.Items.Add(ListBox1.Items[i]);
            ListBox1.Items.RemoveAt(i);
        }
    }
      

  18.   

    RemoveAt也一样啊
    用RemoveAt和Remove没什么区别吧
    RemoveAt(索引号)
    Remove(Item)
    作用都是从集合中移走一项。
      

  19.   

    每次remove掉一项后 ListBox1.Items.Count也会-1的
    ----------------------
    以前我记得我试验这个的时候这个不会及时更新的。所以我的一切推论是建立在这个基础上的。
    yusongkun(九道轮回)说的争吵谈不上,讨论而已!
    唉,楼主那么做法是对的。至于他所说的情况,我们不调试,看不出来原因的。----既然你也没调试,你怎么知道它那样作就是对的了?晚上有空我测试下
      

  20.   

    试验过了,这样是正常的
    for (int i = 0; i < ListBox2.Items.Count; i++)
    {
    if (ListBox2.Items[i].Selected)
    {
    this.ListBox1.Items.Add(new ListItem(ListBox2.Items[i].Text, ListBox2.Items[i].Value));
    this.ListBox2.Items.Remove(ListBox2.Items[i]);
    i--;
    }
    }
    ,至于楼主出现的问题,会不会是其他部分的代码影响到listbox的选择了。
      

  21.   

    for ( int i = ListBox1.Items.Count - 1; i >= 0; i-- )
    {
        if ( ListBox1.Items[i].Selected )
        {
            ListBox2.Items.Add(ListBox1.Items[i]);
            ListBox1.Items.RemoveAt(i);
        }
    }
    试了,结果一样
    出现异常的页面里绑定的数据是这样来的:
    根据一个dropdownlist的值来判断listbox1绑定那类数据。 autopostback属性是true;
    private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    SqlConnection con=DB.createcon();
    con.Open();
    SqlDataAdapter sda=new SqlDataAdapter();
    DataSet ds=new DataSet();
    string planfor=this.DropDownList1.SelectedValue.ToString();
    if(planfor=="请选择对象")
    {
    this.ListBox1.Items.Clear();
    this.ListBox2.Items.Clear();
    }
    else
    {
    this.ListBox2.Items.Clear();
    sda.SelectCommand=new SqlCommand("查询语句",con);
    sda.Fill(ds,"item");
    this.ListBox1.DataTextField="Item";
    this.ListBox1.DataValueField="Method";
    this.ListBox1.DataSource=ds.Tables["item"].DefaultView;
    this.ListBox1.DataBind();
    }
    con.Close();
    }异常情况是:后台取不到我选中的项的,总是默认listbox.items[0]是选中的,其他都是没选中的,这样只会移动第1项的内容到listbox2
        郁闷!!!
      

  22.   

    哪种代码可能影响到listbox的选择?
    这个我不清楚,求教
      

  23.   

    要用--的循环
    不然再移掉一个之后count数就变了
    这样的话下一次进入循环后selectindex就找错地方了。
      

  24.   

    给你一段我的代码
    #region 左移一个
        protected void btnLeft_ServerClick(object sender, EventArgs e)
        {
            if (this.lstEmp.SelectedIndex == -1)
            {
                return;
            }
            int index = this.lstEmp.SelectedIndex;
            this.lstManager.Items.Add(
                new ListItem(
                this.lstEmp.SelectedItem.Text,
                this.lstEmp.SelectedItem.Value)
                );
            this.lstEmp.Items.RemoveAt(index);
            if (index != 0 && index < this.lstEmp.Items.Count)
            {
                this.lstEmp.SelectedIndex = index;
            }
            else if (index != 0 && index == this.lstEmp.Items.Count)
            {
                this.lstEmp.SelectedIndex = index - 1;
            }
            else if (index == 0 && this.lstEmp.Items.Count > 0)
            {
                this.lstEmp.SelectedIndex = 0;
            }
            this.lstManager.SelectedIndex = this.lstManager.Items.Count - 1;
        }
        #endregion    #region 右移一个
        protected void btnRight_ServerClick(object sender, EventArgs e)
        {
            if (this.lstManager.SelectedIndex == -1)
            {
                return;
            }
            int index = this.lstManager.SelectedIndex;
            this.lstEmp.Items.Add(
                new ListItem(
                this.lstManager.SelectedItem.Text,
                this.lstManager.SelectedItem.Value)
                );
            this.lstManager.Items.RemoveAt(index);
            if (index != 0 && index < this.lstManager.Items.Count)
            {
                this.lstManager.SelectedIndex = index;
            }
            else if (index != 0 && index == this.lstManager.Items.Count)
            {
                this.lstManager.SelectedIndex = index - 1;
            }
            else if (index == 0 && this.lstManager.Items.Count > 0)
            {
                this.lstManager.SelectedIndex = 0;
            }
            this.lstEmp.SelectedIndex = this.lstEmp.Items.Count - 1;
        }
        #endregion    #region 全部左移
        protected void btnLefts_ServerClick(object sender, EventArgs e)
        {
            if (this.lstEmp.Items.Count == 0)
                return;
            for (int i = this.lstEmp.Items.Count - 1; i >= 0; i--)
            {
                this.lstManager.Items.Add(
                    new ListItem(
                    this.lstEmp.Items[i].Text,
                    this.lstEmp.Items[i].Value)
                    );
            }
            this.lstManager.SelectedIndex = this.lstManager.Items.Count - 1;
            this.lstEmp.Items.Clear();
        }
        #endregion    #region 全部右移
        protected void btnRights_ServerClick(object sender, EventArgs e)
        {
            if (this.lstManager.Items.Count == 0)
                return;
            for (int i = this.lstManager.Items.Count - 1; i >= 0; i--)
            {
                this.lstEmp.Items.Add(
                    new ListItem(
                    this.lstManager.Items[i].Text,
                    this.lstManager.Items[i].Value)
                    );
            }
            this.lstEmp.SelectedIndex = this.lstEmp.Items.Count - 1;
            this.lstManager.Items.Clear();
        }
        #endregion
      

  25.   

    将选中的与未选中的单独存到的缓存(如ArrayList里面),然后分别重新绑定,代码逻辑需要清晰