Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
if (lbxFecilities.Items.Count > 0)
            {
                for (int i = 0; i < lbxFecilities.Items.Count; i++)
                {
                    if (lbxFecilities.Items[i].Selected)
                    {
                        if (facilities == "")
                            facilities = lbxFecilities.Items[i].Text;
                        else
                            facilities += "," + lbxFecilities.Items[i].Text;
                    }
                }
            }


Here lsbFecility is the listBox Controller. My problem is that I cannot access the Items[i].Selected Property. It shows some error like Missing assembly reference. Someone please help me to select multiple items from listBox.
Posted
Updated 8-Mar-12 17:43pm
v2
Comments
walterhevedeich 9-Mar-12 0:20am    
Please post the exact error message.
arun.m.mr 9-Mar-12 0:21am    
Error 2 'object' does not contain a definition for 'Selected' and no extension method 'Selected' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Sergey Alexandrovich Kryukov 9-Mar-12 1:08am    
The answer depends on UI library and application type. WPF? Forms? Silverlight? ASP.NET? What? Tag it.
--SA
Member 11116893 12-Nov-14 5:54am    
how to select multiple item from listbox and save into sql server database??
in wpf

1 solution

Please chk if you have give the following NAMESPACE

1. For Windows Application
C#
using System.Windows.Controls.ListBox

2. For ASP.Net Application
C#
using System.Web.UI.WebControls;

3. For WPF Application
C#
using System.Windows.Documents;


And Instead of "FOR Loop" use the following

C#
private void button1_Click(object sender, System.EventArgs e)
 {
     String strItem;
     foreach(Object selecteditem in listBox1.SelectedItems)
     {
         strItem = selecteditem as String;
         MessageBox.Show(strItem);
     }
 }


Hope this will give you an idea
 
Share this answer
 
Comments
arun.m.mr 9-Mar-12 1:02am    
Thank you aniketyadav7. Thank you so much.
Sergey Alexandrovich Kryukov 9-Mar-12 1:12am    
You literally forced aniketyadav7 to make triple work for you. This is not very nice. You should always specify exact type of ListBox, as there are different types under this name.
--SA
Sergey Alexandrovich Kryukov 9-Mar-12 1:09am    
Sorry, this is not exactly so. SelectedItems does not exist in ASP.NET.
Will you please check it up and fix?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900