Avatar of razza_b
razza_b
 asked on

add listbox item to another listbox (silverlight 4)

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 ??
             }
          }
C#.NET ProgrammingMicrosoft Development

Avatar of undefined
Last Comment
razza_b

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ukerandi

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ukerandi

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

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);
}
ukerandi

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
razza_b

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
razza_b

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