none
how to move items from listbox to another listbox?

    Question

  •  I have 2 listboxs in webform

    is it possible let items transfer on each listbox  and save final result to database?

    for example:

    listbox1 have 2 items : Joan and ViVi

    listbox2 have 1 item : Kevin

    if I choose Joan from listbox1 and click bottn

    Joan will disapper from listbox1 and appear on listbox2

    so that I have this result after click bottn

    listbox1 have 1 item: ViVi

    listbox2 have 2 item : Kevin and Joan

    =======================================

    also I would like to know could I do a mutiple select and remove all items to another listbox at one time?

    when I select Joan and ViVi from listbox1 and then click bottn , Joan and Vivi will be added to listbox2 and remove from listbox1 . I want the result to be

    listbox1 have 0 item

    listbox2 have 3 items : Kevin, Joan, ViVi

    the important thing is , item can only appear once whatever in listbox1 or listbox2, if you have item Vivi in listbox1 I don't want listbox2 have, item can only in 1 listbox..

     

    for Me, the hardest question is how To save all selection To database? is it possible? ( I am using

    MS SQL Server)

     

    thanks :D

    Thursday, November 23, 2006 8:28 AM

Answers

  • simply get the selected item, add it into listbox2 and remove from listbox1

    this.theListBox2.Items.Add(this.theListBox1.SelectedItem);

    this.theListBox1.Items.Remove(this.theListBox1.SelectedItem);

    same thing applies when selecting multiple items, just go through the selectedItems collection and add each item into the other listbox.

    foreach(object curItem in this.theListBox.SelectedItems)

    {

       this.theListBoxDestination.Items.Add(curItem);

    }

     

    if you only want a listbox to have 1 item and not duplicates, check to see if the other listbox contains the item using the Items.Contains() method.

    if (this.theListbox.Items.Contains(this.theSourceListBox.SelectedItem) == false)

    {

       //this destination listbox does NOT contain the item, so add item

    }

     

    in regards to SQL Server, pretty simple. Just create your insert statement, open the connection, executenonquery and close the connection. Add the System.Data.SqlClient namespace. this small example will go through each item in the listbox and insert that value into SQL

     

    SqlCommand theSQLCommand = new SqlCommand("INSERT INTO [TableName] (FieldName) VALUES (@p1)", new SqlConnection("connectionString"));

    try

    {

    theSQLCommand.Connection.Open();

    foreach(object currentItem in this.theListBox.Items)

    {

       SqlParameter theParameter = new SqlParameter("@p1", currentItem);

       theSQLCommand.Parameters.Add(theParameter);

       theSQLCommand.ExecuteNonQuery();

       theSQLCommand.Parameters.Clear();

    }

    theSQLCommand.Connection.Close();

    }

     

    this will go through each item in the listbox, and get the current item, place it into a parameter and execute the insert statement. Does this work for you?

     

    Thursday, November 23, 2006 8:50 AM
  • you can, simply go through each item in the listbox1 and add into listbox2

    for each currentItem as object in Me.ListBox1.Items

       Me.ListBox2.Items.Add(currentItem)

    next

    Me.ListBox1.Items.Clear()

    Friday, November 24, 2006 11:49 AM

All replies

  • simply get the selected item, add it into listbox2 and remove from listbox1

    this.theListBox2.Items.Add(this.theListBox1.SelectedItem);

    this.theListBox1.Items.Remove(this.theListBox1.SelectedItem);

    same thing applies when selecting multiple items, just go through the selectedItems collection and add each item into the other listbox.

    foreach(object curItem in this.theListBox.SelectedItems)

    {

       this.theListBoxDestination.Items.Add(curItem);

    }

     

    if you only want a listbox to have 1 item and not duplicates, check to see if the other listbox contains the item using the Items.Contains() method.

    if (this.theListbox.Items.Contains(this.theSourceListBox.SelectedItem) == false)

    {

       //this destination listbox does NOT contain the item, so add item

    }

     

    in regards to SQL Server, pretty simple. Just create your insert statement, open the connection, executenonquery and close the connection. Add the System.Data.SqlClient namespace. this small example will go through each item in the listbox and insert that value into SQL

     

    SqlCommand theSQLCommand = new SqlCommand("INSERT INTO [TableName] (FieldName) VALUES (@p1)", new SqlConnection("connectionString"));

    try

    {

    theSQLCommand.Connection.Open();

    foreach(object currentItem in this.theListBox.Items)

    {

       SqlParameter theParameter = new SqlParameter("@p1", currentItem);

       theSQLCommand.Parameters.Add(theParameter);

       theSQLCommand.ExecuteNonQuery();

       theSQLCommand.Parameters.Clear();

    }

    theSQLCommand.Connection.Close();

    }

     

    this will go through each item in the listbox, and get the current item, place it into a parameter and execute the insert statement. Does this work for you?

     

    Thursday, November 23, 2006 8:50 AM
  • Hi

    Check the link below which shows moving items between 2 list box, they have used javascript to do this.

    http://www.dotnetwatch.com/display.aspx?contID=284

     

    http://www.dotnet247.com/247reference/msgs/56/280757.aspx

     

     

    Thursday, November 23, 2006 9:02 AM
  • thank you, but is it possible let all items from listbox move to another listbox at once?

    for example:

    listbox 1 we have items : A、B、C、D

    listbox 2 we don't have any item in this time.

     

    I would like to have a botton and it can let all items move to listbox2 at 1 click.

    so when user click bottn, listbox1's items (A、B、C、D) will move to listbox2

    we will have items A、B、C、D in listbox2 and clear all items in listbox1 after botton clicked

     

    also I'm thinking is it ok about this funcation

    I will add 3 controls in webform

    1 dropdownlist

    2 listbox  (listbox1 and listbox2)

    in start

    dropdownlist will shows memeber's name (I add a Sqldatasorce for this relationship and it's works fine)

    listbox1 don't have to show any information 

     user select member's name from dropdownlist and then appear mamber's information on listbox1. is it possible?

    l

     

     

     

     

    thank you again. very appreciate your help.

    Friday, November 24, 2006 2:28 AM
  • you can, simply go through each item in the listbox1 and add into listbox2

    for each currentItem as object in Me.ListBox1.Items

       Me.ListBox2.Items.Add(currentItem)

    next

    Me.ListBox1.Items.Clear()

    Friday, November 24, 2006 11:49 AM
  • how we can select randomly multiple selected items from listbox1 and move into other listbox.
    Thursday, January 11, 2007 6:02 AM