473,508 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trouble with events

I am new to C# and windows programming. I am trying do create something
fairly simple (at least I think). I have 1 form (Form1). On Form1 I have a
listbox. I also have a Class (Class1). Class1 loops through the local
filesystem. I want Class1 to add filenames to Form1's listbox. I know I
have to use events and delegates, etc but am unsure and I can't find any good
examples. Please provide some help. Examples are great. Thanks.
Oct 24 '07 #1
1 1001
JoshP wrote:
I am new to C# and windows programming. I am trying do create something
fairly simple (at least I think). I have 1 form (Form1). On Form1 I have a
listbox. I also have a Class (Class1). Class1 loops through the local
filesystem. I want Class1 to add filenames to Form1's listbox. I know I
have to use events and delegates, etc but am unsure and I can't find any good
examples. Please provide some help. Examples are great. Thanks.
Why do you know that you have to use events and delegates?

It's not clear from your post how Class1 works, but in the simplest case
all you have to do is make sure that Class1 has access to the reference
to the listbox or the Form1 containing the listbox, and then use that
reference to call a method that will add the filenames to the listbox.

If you get the reference to the listbox instance itself, this is simply
a matter of calling the appropriate add method with the filename. It's
possible you'd prefer to put a public method on the form that handles
the actual interaction with the listbox, so that the listbox isn't
exposed. In that case, your Class1 would need a reference to the form
instance, and would call that public method to do the actual work with
the filename.

If your Class1 does its work in a different thread, then you do run into
a need to use a delegate, but not an event. The reason being that the
act of actually adding the filename to the list box must be done on the
main UI thread, not the thread in which Class1 does its work. In that
case, you'd need to use the Control.Invoke() method, passing it a
delegate that would actually do the work (as above...this works whether
you are calling a public method on the form, or calling a method on the
listbox instance directly). That delegate can be as simple as an
anonymous method, and in fact that's what I'd recommend.

Pete
Oct 24 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3287
by: Bill | last post by:
For my personal use I am accessing a railway timetable page, parsing its contents and then sending brief relevant information as the subject line of an email to be read on a mobile phone. The...
0
1195
by: Manuel D. Jim?nez | last post by:
Hi, we are developing an ActiveX control which plays a video streaming. We have used Visual Studio .net wizard to develop almost all the control interface, however we have trouble with event...
10
1460
by: whmoseley | last post by:
I'm using the Prototype library for ajax calls. (Yes, I'm aware of the discussions about prototype lately). Perhpas it's the library that's causing problems, but I'm more suspct of my own code at...
3
7144
by: weston | last post by:
I'm making a foray into trying to create custom vertical scrollbars and sliders, and thought I had a basic idea how to do it, but seem to be having some trouble with the implementation. My...
0
1788
by: exits funnel | last post by:
Hello, I apologize if this question is a bit vague and slightly off topic but I couldn't find an Analysis Services and/or ODBO specific newsgroup. In any event, I'm trying to address an issue...
5
2332
by: Josh Nikle | last post by:
I have a webform containing a GridView control "GridView1" and usercontrol which is also a GridView "GridView2." I have a button column in the usercontrol. The code behind that button adds an...
5
6743
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created...
3
7872
by: Luc The Perverse | last post by:
Hey - I am making my first C# form application! (More a teach myself app than any pratical application) I have a listbox that always has the control so I made an event handler for KeyDown. I...
1
1654
by: Tableshavturned | last post by:
Hi this is my first post on the forums. I haven't really developed before with Access 2003 so trouble shooting with this application is not my forte. The issue at hand is, created a star schema with...
5
13304
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
7333
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.