ListBox Problem

Discussion in 'ASP .Net Web Controls' started by Jack Lewis, Sep 1, 2005.

  1. Jack Lewis

    Jack Lewis Guest

    Dear All.

    I have a Web Form which has 2 ListBoxes and an Add Button. I have 4 items in
    my First List Box. What I want to do is shift the selected Items from first
    List Box to 2nd List Box.The selection mode of both of my ListBoxes is
    "Multiple". I am able to add the selected Items from the 1st List Box to 2nd
    List Box. But The problem is that when I have added the selected items from
    1st List Box to 2nd List Box. the added Items still seem to appear in the 1st
    List Box. This doesn't happen when the Selection Mode of my ListBox is
    "Single". I want the selected Items to be deleted once the they have been
    copied into 2nd List Box.
    Below is my Code:


    private void Page_Load(object sender, System.EventArgs e)
    { // Put user code to initialize the page here

    if(!IsPostBack)

    {

    ListBox1.Items.Add("UK");
    ListBox1.Items.Add("USA"); ListBox1.Items.Add("Ireland");
    ListBox1.Items.Add("Wales");
    }

    }

    private void btnAdd_Click(object sender, System.EventArgs e)
    {
    if (ListBox1.SelectedItem != null)
    {
    foreach(ListItem i in ListBox1.Items)
    {
    if(i.Selected==true)
    {
    strSelection = i.Text;

    if (ListBox2.Items.FindByText(strSelection)== null )
    {
    ListBox2.Items.Add(strSelection);
    ListBox2.SelectedIndex =0;

    }

    //*** Here is the Code where I am removing the Items from ListBox1**//


    for(int counter = 0; counter<=ListBox1.Items.Count-1; counter++)
    {
    if (ListBox1.Items[counter].Selected == true)
    {
    ListBox1.Items.RemoveAt(counter);
    }
    }

    I don't know whats wrong with the above code.But it doesn't seem to remove
    the selected items from the 1st List Box properly. I mean it does remove
    Items frm the 1st list Box. But not the all which I have selected.

    Any help would be greatly appreciated.

    cheers,
    Jack
     
    Jack Lewis, Sep 1, 2005
    #1
    1. Advertising

  2. Jack Lewis

    Microsoft Guest

    Change your for loop to count down instead of up:
    for (int counter = ListBox1.Items.Count-1; counter >= 0; counter--)




    "Jack Lewis" <Jack > wrote in message
    news:...
    > Dear All.
    >
    > I have a Web Form which has 2 ListBoxes and an Add Button. I have 4 items
    > in
    > my First List Box. What I want to do is shift the selected Items from
    > first
    > List Box to 2nd List Box.The selection mode of both of my ListBoxes is
    > "Multiple". I am able to add the selected Items from the 1st List Box to
    > 2nd
    > List Box. But The problem is that when I have added the selected items
    > from
    > 1st List Box to 2nd List Box. the added Items still seem to appear in the
    > 1st
    > List Box. This doesn't happen when the Selection Mode of my ListBox is
    > "Single". I want the selected Items to be deleted once the they have been
    > copied into 2nd List Box.
    > Below is my Code:
    >
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > { // Put user code to initialize the page here
    >
    > if(!IsPostBack)
    >
    > {
    >
    > ListBox1.Items.Add("UK");
    > ListBox1.Items.Add("USA"); ListBox1.Items.Add("Ireland");
    > ListBox1.Items.Add("Wales");
    > }
    >
    > }
    >
    > private void btnAdd_Click(object sender, System.EventArgs e)
    > {
    > if (ListBox1.SelectedItem != null)
    > {
    > foreach(ListItem i in ListBox1.Items)
    > {
    > if(i.Selected==true)
    > {
    > strSelection = i.Text;
    >
    > if (ListBox2.Items.FindByText(strSelection)== null )
    > {
    > ListBox2.Items.Add(strSelection);
    > ListBox2.SelectedIndex =0;
    >
    > }
    >
    > //*** Here is the Code where I am removing the Items from ListBox1**//
    >
    >
    > for(int counter = 0; counter<=ListBox1.Items.Count-1; counter++)
    > {
    > if (ListBox1.Items[counter].Selected == true)
    > {
    > ListBox1.Items.RemoveAt(counter);
    > }
    > }
    >
    > I don't know whats wrong with the above code.But it doesn't seem to remove
    > the selected items from the 1st List Box properly. I mean it does remove
    > Items frm the 1st list Box. But not the all which I have selected.
    >
    > Any help would be greatly appreciated.
    >
    > cheers,
    > Jack
    >
     
    Microsoft, Sep 2, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. blenderdude
    Replies:
    0
    Views:
    1,126
    blenderdude
    Aug 3, 2003
  2. DC Gringo
    Replies:
    0
    Views:
    418
    DC Gringo
    Apr 6, 2004
  3. kent
    Replies:
    1
    Views:
    900
    Andrew de la Harpe
    May 3, 2004
  4. Simon Prince
    Replies:
    2
    Views:
    3,371
    =?Utf-8?B?TWljaGFlbCBUYXlsb3I=?=
    Oct 19, 2004
  5. Chris Kettenbach

    Listbox selection to populate another listbox?

    Chris Kettenbach, Jun 15, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,677
    Chris Kettenbach
    Jun 16, 2005
Loading...

Share This Page