How do I use a foreach loop with a listbox?

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
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
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+
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:

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
Programming Experience
Beginner
The error is something like "the name doesn't exist in the current context". I am definitely having scope issues currently.
 

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
Programming Experience
Beginner
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
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+
See my updated post #2 that I was writing while you were replying...
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+
This looks related to your other thread. Hopefully the moderators can merge threads...
 

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
Programming Experience
Beginner
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.
 

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
Programming Experience
Beginner
Yes it seems both of the threads lead to the same issue.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+
Anyway, to answer the easy part of the problem, assuming you have access the lstbHistory, you would iterate over the Items collection.
 

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
Programming Experience
Beginner
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.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
Programming Experience
Beginner
OK, thanks, I'll get reading, hopefully I can handle it :)
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+
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.
 

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
Programming Experience
Beginner
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; }
}
 

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
Programming Experience
Beginner
Never mind, I now know what the get set thing means, but will the foreach loop work?
 

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
Programming Experience
Beginner
So I understand what I need to do and roughly how to do it. Here is my situation, Form1 has a TextBox, Form2 has a ListBox, the TextBox will send raise event when it is updated and the ListBox will be updated with the text as a new item. That's what I'm trying to achieve.

So I need to first make an EventArgs that contains a string property like so:

C#:
public class TextBoxUpdatedEventArgs : EventArgs
{
    public string NewText { get; set; }
}
So my question at this point is where would this go? My guess would be at the bottom of the "public partial class Form1 : Form" block?
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
Please do not trail your topics with new questions. This confuses people, and it makes it harder to understand which of your problems you've resolved.

Instead, please open a new topic for new problems.
 

glasswizzard

Active member
Joined
Nov 22, 2019
Messages
30
Programming Experience
Beginner
Please do not trail your topics with new questions. This confuses people, and it makes it harder to understand which of your problems you've resolved.

Instead, please open a new topic for new problems.
I don't mind starting a new thread for each individual step but I think that would get annoying for everyone, I think this thread has become about custom event args and their implementation, and I'll need advice probably the whole way. It just seems to me that one thread would be the best thing to keep it all together but if you want it separated I'll do that.

Edit: To clear up any confusion the only questions I have at this point are:
1) Will a foreach loop as described in post #1 avoid the problem described in post #10?
2) Given the situation described in post #13 where should I declare TextBoxUpdatedEventArgs?
 
Last edited:

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
Well its not the best for anyone to keep it all together or in one place, which is why I am asking you to separate your separate issues since they're non-related. If one of the problems you are experiencing is related to the topic question you started out with, by all means keep your topic updated regarding only that issue. But when you ask something else like you have done in post 13, then that becomes another question which isn't related to the question you started out with. Generally the moderators will split your topic upon doing such things anyway, so you're best not to add to their already busy duties by doing the opposite of what I just asked you, because I will just flag it for the mods to split the topic into two anyway.

Form1 has a TextBox, Form2 has a ListBox, the TextBox will send raise event when it is updated and the ListBox will be updated with the text as a new item. That's what I'm trying to achieve.
Have you read over the links you have already been given?
Have you also searched MSDN for documentation on what you're trying to do?
but it seems that's wrong because the "lstbHistory" part is underlined in red. How do I do this?
That's likely because it belongs to another form other than the one you are trying to access it from, but I'm only summarising. You should know why that is marked in red squiggle.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+
Stop and read the documentation instead of relying on Intellisense. If you did read the documentation for the list box, you would find that there are both FindString() and FindStringExact(). The latter would fix your issue from post #10.
 
Top Bottom