add listbox item to another listbox (silverlight 4)

razza_b
razza_b used Ask the Experts™
on
Hi

I have 2 listboxes and i just want to select an item from one listbox1 and put it into listbox2...

        private void addItem_Click(object sender, RoutedEventArgs e)
        {
            if(lstbox1.SelectedIndex == -1)
            {
                return;
            }
            else if (lstbox1.SelectedIndex > -1)
            {
                 //get the selected item and add to lstbox2 ??
             }
          }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
List<object> _selecteditems = new List<object>();
foreach(var item in listBox1.SelectedItems)
 {
_selecteditems.Add(item);
}
foreach(var item in _selecteditems)
{
listBox1.Items.Remove(item);
listBox2.Items.Add(item);
}

Commented:
else if (lstbox1.SelectedIndex > -1)
            {
                 listBox2.Items.Add(item);
             }

Commented:
1 down vote
      

A ListBox has a SelectedItems property you can use to copy the items in the click event handler of the button. Like this:

foreach(var item in listBox1.SelectedItems)
{
    listBox2.Items.Add(item);
}
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Commented:
http://code.msdn.microsoft.com/windowsdesktop/Pass-Selected-Item-from-734a24ef

try
foreach (int i in listBox1.SelectedIndices)
            {
                listBox2.Items.Add(listBox1.Items[i].ToString());               
            }

Open in new window

Commented:
i have got it working with this, but i cant seem to do multi-select...any ideas based on the code i have?...

private void btnAssign_Click(object sender, RoutedEventArgs e)
        {
            if(lstUserAvailRole.SelectedIndex == -1)
            {
                return;
            }
            else if (lstUserAvailRole.SelectedIndex > -1)
            {
                ListBoxItem selected = lstUserAvailRole.SelectedItem as ListBoxItem;
                if (selected != null)
                {
                    if (lstUserselectedRole.Items.Contains(selected.Content.ToString()))
                    {
                        return;
                    }
                    else
                    {
                        lstUserselectedRole.Items.Add(selected.Content.ToString());
                        lstUserAvailRole.Items.Remove(lstUserAvailRole.SelectedItem);
                    }
                }
                else if (lstUserAvailRole.SelectedItem != null)
                {
                    if (lstUserselectedRole.Items.Contains(lstUserAvailRole.SelectedItem))
                    {
                        return;
                    }
                    else
                    {
                        lstUserselectedRole.Items.Add(lstUserAvailRole.SelectedItem);
                        lstUserAvailRole.Items.Remove(lstUserAvailRole.SelectedItem);
                    }
                }
            }
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if (lstUserselectedRole.SelectedIndex == -1)
            {
                return;
            }
            else if (lstUserselectedRole.SelectedIndex > -1)
            {
                if (lstUserselectedRole.SelectedItem != null)
                {
                    if (lstUserAvailRole.Items.Contains(lstUserselectedRole.SelectedItem))
                    {
                        return;
                    }
                    else
                    {
                        lstUserAvailRole.Items.Add(lstUserselectedRole.SelectedItem.ToString());
                        lstUserselectedRole.Items.Remove(lstUserselectedRole.SelectedItem);
                    }
                }
            }
        }


Thanks

Author

Commented:
my comment shows that i can get it to work with single select code as ukerandi code can do multi select

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial