Jump to content

Loop through ListBox

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
LiLMsNinja

LiLMsNinja

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
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 crap 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?
CodeMonkey +15 | CommunicationSkills -34 | ClarityUsingEnglish -55 | Clarity while pointing at the monitor and making vigorous facial expressions, occasional grunts, and mouth clicks +150

#2
LiLMsNinja

LiLMsNinja

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Anyone?! Anyone at all?

Does something need clarification?
CodeMonkey +15 | CommunicationSkills -34 | ClarityUsingEnglish -55 | Clarity while pointing at the monitor and making vigorous facial expressions, occasional grunts, and mouth clicks +150

#3
Deadlock

Deadlock

    Learning Programmer

  • Members
  • PipPipPip
  • 81 posts
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 =).

#4
LiLMsNinja

LiLMsNinja

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

Deadlock said:

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();


CodeMonkey +15 | CommunicationSkills -34 | ClarityUsingEnglish -55 | Clarity while pointing at the monitor and making vigorous facial expressions, occasional grunts, and mouth clicks +150

#5
LiLMsNinja

LiLMsNinja

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
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!
CodeMonkey +15 | CommunicationSkills -34 | ClarityUsingEnglish -55 | Clarity while pointing at the monitor and making vigorous facial expressions, occasional grunts, and mouth clicks +150

#6
LiLMsNinja

LiLMsNinja

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
SOLUTION:


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

CodeMonkey +15 | CommunicationSkills -34 | ClarityUsingEnglish -55 | Clarity while pointing at the monitor and making vigorous facial expressions, occasional grunts, and mouth clicks +150