Add items to listbox from Class

G

Guest

I have a single form C# application called MainUI. On MainUI I have a
listbox that will log certain activity occuring in a seperate class file.
For example, the Class has a function that loops through a directory and
determines the filenames within the directory. Within this function I would
like the filenames to be sent to the listbox on MainUI. I can't seem to
figure this out. I have tried creating a public update method in MainUI to
update the listbox and call it from the seperate class (by creating a new
MainUI object in the seperate class). The code runs but the listbox doesn't
get updated. Please help...
 
N

Nicholas Paldino [.NET/C# MVP]

JoshP,

The reason for this is that you create a new MainUI, which is a separate
instance from the one that created your class.

Rather, you should have Class fire an event after (or while) it is
looping through the directory. Then, have the MainUI form attach to the
event, and update the list box when the event is fired.
 
G

Guest

Thanks Nicholas-- could you provide a really quick generic example?

Nicholas Paldino said:
JoshP,

The reason for this is that you create a new MainUI, which is a separate
instance from the one that created your class.

Rather, you should have Class fire an event after (or while) it is
looping through the directory. Then, have the MainUI form attach to the
event, and update the list box when the event is fired.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

JoshP said:
I have a single form C# application called MainUI. On MainUI I have a
listbox that will log certain activity occuring in a seperate class file.
For example, the Class has a function that loops through a directory and
determines the filenames within the directory. Within this function I
would
like the filenames to be sent to the listbox on MainUI. I can't seem to
figure this out. I have tried creating a public update method in MainUI
to
update the listbox and call it from the seperate class (by creating a new
MainUI object in the seperate class). The code runs but the listbox
doesn't
get updated. Please help...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top