How do I use a foreach loop with a listbox?

glasswizzard

Well-known member
Joined
Nov 22, 2019
Messages
126
Programming Experience
Beginner
My thinking was like this:

C#:
foreach (var HistoryItem in lstbHistory)

but it seems that's wrong because the "lstbHistory" part is underlined in red. How do I do this?

Thank you
 
When you hover over the lstHistory with the red squiggly lines, what is the error shown?

Just guessing, chances are that you are trying to accessing something outside of its scope.

I'm assuming that this thread is the new thread you were referring to in your other post. If so, then this is generally a scoping issue. The expedient solution is to let the two forms have references to each other, and to expose various controls as public. This expedient solution goes against modern programming principles -- in this case loose coupling and proper encapsulation. You could have gotten away with this back in the 80's and 90's when this was the most efficient way to do things due to limitations on amount of RAM and CPU power, but with that not being a limiting factor anymore, you should strive to write more maintainable code that follows modern OOP principles.
 
Last edited:
The error is something like "the name doesn't exist in the current context". I am definitely having scope issues currently.
 
Hello, I have two forms, each form has one control, a text box and a listbox respectively.

My main form contains the textbox, the second form is the History form which contains the listbox (the history listbox). Text from the textbox in form1 is added to the listbox in form2 via AddToHistory(string). Where am I supposed to create this method? In my second forms cs file the textbox in the first forms cs file can't be seen and vice versa, the textbox in form1's cs file can't see the listbox in the other.

What am I doing wrong?
Thanks
 
See my updated post #2 that I was writing while you were replying...
 
This looks related to your other thread. Hopefully the moderators can merge threads...
 
Thank you, could you tell me how to "let the two forms have references to each other"?

This isn't the post I mentioned creating, when I made this post I didn't know it would be a scope issue, so maybe I made a redundant thread, if so I'll delete it.
 
Anyway, to answer the easy part of the problem, assuming you have access the lstbHistory, you would iterate over the Items collection.
 
Thanks for that it's good to know but I changed the way I was doing it to this:

C#:
if (lstbHistory.FindString(txtInput.Text) == ListBox.NoMatches)

so the code is run only if the text is not already there.
 
C#:
if (lstbHistory.FindString(txtInput.Text) == ListBox.NoMatches)

so the code is run only if the text is not already there.
Alas, that will find "Thanksgiving" when you search for "Thanks". Probably not what you want to happen.
 
Alas, that will find "Thanksgiving" when you search for "Thanks". Probably not what you want to happen.

Damn, will my original plan of using the foreach loop work?

Also, could you tell me what the "{ get; set; }" part of the below code means please?

C#:
public class NameUpdatedEventArgs : EventArgs
{
    public string Name { get; set; }
}
 
Back
Top Bottom