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
 
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?
 
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.
 
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:
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.
 
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.
 
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.

OK, I think Skydiver answered my initial question so that makes this thread done right? Any more questions I have about custom events should go in a new thread?

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?

I have read the links, and do understand what I have to do, maybe not exactly how to do it but any questions I have on the specifics of a custom event think will have to go in a new thread.

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.

I now know that is why I'm getting the error.

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.

Thanks for that, I'll bare in mind to read the documentation from now on.

I think this thread is done now, I will make a new thread (probably tomorrow) with my questions about custom events. Those docs provided are clear, but it does leave me with some questions.

Thanks everyone for the help.
 
Back
Top Bottom