Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Loop through ListBox

asp.net loop

  • Please log in to reply
5 replies to this topic

#1 LiLMsNinja

LiLMsNinja

    CC Regular

  • Just Joined
  • PipPipPip
  • 42 posts

Posted 27 March 2010 - 09:03 AM

Ok, this should be the simplest thing in the world to do. I have done endless googling and can't get any of it to work or keep finding ** that has to do with ASP.NET. Which i would think would be the same... but none of it is working.

I have a ListBox in my win forms app that will have generally fewer than 5 selections. The user can select 1 or more of these items; which i will use the selected items as part of an update query. I need 1 new record for each selection made. However, i am having extreme difficulty finding all of the selections the user has made.

I believe it to be related to the way i have used LINQ to fill the listbox with items. Here is how i fill it:

List<Accessor.SwitchPages> sites = Accessor.getSites(dataGridView1.Rows[i].Cells[3].Value.ToString());
            listProjects.DataSource = sites;
            
            
            listProjects.SelectedIndex = -1;

            listProjects.ValueMember = "_projectid";
            listProjects.DisplayMember = "_projectid";

Later after the user has made a selection and clicked a button will i loop through to find the selections and make the update. But, I am having difficulty getting the actual string value of the item selected.

for (int x = 0; x <= listProjects.SelectedItems.Count; x++)
            {
                if(listProjects.GetSelected(x) == true)
                {
                    string d = listProjects.SelectedValue.ToString();
                    string s = listProjects.SelectedItems[x].ToString();

                    //listProjects.DisplayMember[x].ToString();
                    //listProjects.Items[x].ToString();
        
                }

            }


Inside the IF above, i am trying different ways to test the return of the string value of the selections made.
string d will return the first item(out of many) selected, every loop iteration; OR the ONLY item selected no matter it's position in the array.

string s will return the value: "ForcePassword.Accessor+SwitchPages"

Available example options in the listbox are:
STAR_CSI
STAR_LIC
STAR_CCC

Here is a shot of my locals window. Basically, I want to get to the _projectid value.

Posted Image

Questions:
1) How do i get string s to return the actual text value of the item selected?
2) Is there a better/preferred way to populate the listbox in order to call the selected items later?
  • 0

#2 LiLMsNinja

LiLMsNinja

    CC Regular

  • Just Joined
  • PipPipPip
  • 42 posts

Posted 27 March 2010 - 03:08 PM

Anyone?! Anyone at all?

Does something need clarification?
  • 0

#3 Deadlock

Deadlock

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 64 posts

Posted 27 March 2010 - 04:36 PM

foreach(ListBoxItem lbi in ListProjects.SelectedItems){
     string s = lbi.Text.toString();
     console.WriteLine(s);
}

I don't have C# at my computer atm, So i hope it works =).
  • 0

#4 LiLMsNinja

LiLMsNinja

    CC Regular

  • Just Joined
  • PipPipPip
  • 42 posts

Posted 27 March 2010 - 04:50 PM

foreach(ListBoxItem lbi in ListProjects.SelectedItems){
     string s = lbi.Text.toString();
     console.WriteLine(s);
}

I don't have C# at my computer atm, So i hope it works =).



Yep, you would think that would work. But, it doesn't. For some reason, when i begin typing the 'ListBoxItem' in the foreach the code dropdown thing doesn't drop down. Which means, it's not an available option... am i missing something in the 'Using' area? Even the 'LINQ' option to loop through the selecteditems is not working:

var selItems = from ListItem li in ListBox1.Items
                   where li.Selected == true
                   select li.Text;


    foreach (var item in selItems)
    {
        string s = item.ToString();
    }


I don't have a 'ListItem' no matter which way i toss it or flip it. The way the small snippet below kicks back a value of: "ForcePassword.Accessor+SwitchPages" makes me feel like it's the way i have chosen to populate the ListBox with data in the first place.


string s = listProjects.Items[x].ToString();

  • 0

#5 LiLMsNinja

LiLMsNinja

    CC Regular

  • Just Joined
  • PipPipPip
  • 42 posts

Posted 27 March 2010 - 05:09 PM

Nor does this work:

There is not an available option for 'ListItem' inside 'TypeOf<ListItem>'

var query = from p in ListBox1.Items.OfType<ListItem>()
                .Concat(ListBox2.Items.OfType<ListItem>())
                .Where(o => o.Selected)
            select new
            {
                Text = p.Text
            };

foreach (var item in query)
{
    // print item
}

*Variable Names differ
Example was copied from a website I googled and does not contain the actual variables and/or copy from my application. Because i stopped typing at TypeOf<ListI

Is this because of a 'public' or 'private' method issue. This is driving me absolutely nutso! :mad: :D

Let me know if anyone may require more of my code as an example, i'll post it up. Thanks!
  • 0

#6 LiLMsNinja

LiLMsNinja

    CC Regular

  • Just Joined
  • PipPipPip
  • 42 posts

Posted 29 March 2010 - 05:00 PM

SOLUTION:


List<string> projects = listProjects.SelectedItems.Cast<Accessor.SwitchPages>().Select(x => x._projectid).ToList();
 
            foreach (string project in projects)
            {
                MessageBox.Show(project + " SURPRISE!");
            }

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download