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
ASKER CERTIFIED SOLUTION
Avatar of ukerandi
ukerandi
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ukerandi
ukerandi
Flag of United Kingdom of Great Britain and Northern Ireland image

else if (lstbox1.SelectedIndex > -1)
            {
                 listBox2.Items.Add(item);
             }
Avatar of ukerandi
ukerandi
Flag of United Kingdom of Great Britain and Northern Ireland image

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);
}
Avatar of ukerandi
ukerandi
Flag of United Kingdom of Great Britain and Northern Ireland image

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

SOLUTION
Avatar of razza_b
razza_b

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of razza_b
razza_b

ASKER

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

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo