Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I'm having some issues adding items to a listbox in a separate form/class. The data, which I want to add to the listbox, is coming from a network via an EventHandler.

I'm having issues with actually adding the items to the listbox; I'm able to successfully add the items to a string list List<string> and when I loop through the list in the EvenHandler method, the items are there; however, when I I loop through the string list in the form, no items are returned.

Here's the main form class:

C#
public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();

        foreach(string s in Utils.stringList)
        {
            listBox.Items.Add(s);

            // if I loop through the items here, nothing is returned
            foreach(string s in stringList)
            {
                Console.WriteLine(s);
            }
        }
    }
}



And here's the class that I have the EventHandler in which adds the items to the string list:

C#
public class Utils
{
    public List<string> stringList = new List<string>();
    void ItemsReceived(object sender, DataReceivedEventArgs<RetrieveUsers> e)
    {
        stringList.Add(e.Item);

        // if I loop through the list here, the items are returned
        foreach(string s in stringList)
        {
            Console.WriteLine(s);
        }
    }
}


Any ideas on how to fix this?
Posted
Updated 11-Oct-14 18:43pm
v2
Comments
Sergey Alexandrovich Kryukov 12-Oct-14 1:26am    
What is the declaration of Utils in first fragment?
It cannot be the class Utils shown below, as it would not compile, because stringList is an instance member.
—SA
dutchcoffee 12-Oct-14 1:33am    
They both have the same Namespace, if that's what you're referring to.
Sergey Alexandrovich Kryukov 12-Oct-14 1:40am    
No. Are they the same or not? If they are the same, your first fragment could not compile.
You use the call SomeClass.SomeField. It would be possible only if SomeField is static, but Utils.stringList is not static. (Bad class anyway; it's bad to have public instance fields.)
—SA
dutchcoffee 12-Oct-14 1:50am    
Sorry for the confusion and misunderstanding. I created an instance of the Utils have attempted to loop through the items in the string list, however, no items are being returned. If I, however, loop through the items inside the EventHandler method, the items are returned as they should. For some reason, the items aren't being transferred/accessible to the main form.
BillWoodruff 12-Oct-14 2:06am    
If the Main Form has a valid reference to the instance of the Utils class, and Items are, in fact, being added to 'stringList, then this should work. Put a break-point in whatever method that you are accessing 'stringList in the Main Form, and examine the contents of 'stringList: I predict it will be 'null.

We are just guessing until you show code that will compile.

1 solution

'Utils is a Class, but in your code you never create an instance of that class.

Like this: Utils currentUtils = new Utils();

Once you have an instance of the Class, you can access the Public List<string>stringList

Like this (in your Main Form):
C#
foreach(string receivableItem in currentUtils.stringList)
{
    listBox.Items.Add(receivableItem);
}
Note: your EventHandler has no access modifier, and, so, is, by default, private access: unless you make it 'public, or 'internal, no object outside Utils can use/subscribe to it. So, what you show is not working code.
 
Share this answer
 
v2
Comments
dutchcoffee 12-Oct-14 1:40am    
Just tried, but unfortunately nothing is returned still.
Sergey Alexandrovich Kryukov 12-Oct-14 1:44am    
Returned what? Does your code compile? Please see my comments?
—SA
BillWoodruff 12-Oct-14 1:49am    
Until you show code that is complete enough for us to know it potentially works, we'll remain confused. Please answer Sergey's question about the use of 'Util.
Sergey Alexandrovich Kryukov 12-Oct-14 1:43am    
5, but in my comment I tried to be more strict. I asked what is "Util", because the code would compile only if "Util" is something else, some object. If "Util" in both samples are the same, the code would not compile. OP says "no items is returned" which imply that the code compiles. It looks like OP also misrepresents the observations, set aside understanding of the basics of programming you mentioned.
—SA
BillWoodruff 12-Oct-14 1:47am    
Hi Sergey, there's a delay in my seeing other solutions, and comments, here, that can range up to half-an-hour, or longer. I did not see your comment to the OP before I posted my response. Wonder if the 12 hours difference in time-zones between Toronto and where I am has anything to do with it; there are some expats in Thailand that do believe even the basic laws of physics are suspended here :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900