473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Simple prob with events, pls help

I want to do something really simple.

I have a form and a class which handles my network layer

The form has an instance of the network class as a member and initialises
the network layer.

Inside the network class it then starts dealing with connections and
listening (server).

When a connection is made i have a method in the network class that is
called.

On the form i have a listbox. I want the method in the network class to add
an item to the listbox on the form.

I thought the best way to do this was delgates but it seems delegates are
only good for raising events within the classitself and not out onto forms
tha contain it.

Is there anyway to attach an event from within my network class to the
listbox on the form so that whenever that event is fired i can tell the
listbox to catch it and update itself?

Thanks
Mar 28 '06 #1
5 1101
Hi Daniel,

You started on the right path. You define events in your class but you
handle them elsewhere like in your form. Following article on .NET Framework
Developer's guide should help you:

http://msdn.microsoft.com/library/de...ini-sample.asp
Mar 28 '06 #2
Thanks, i got it working but now my problem is that i am using two different
threads.

i wanted thread1 to trigger the event, and then thread2 to handle it.

I keep getting a error saying this cant be done? Can it be done?

"yogeshprabhu" <yo**********@discussions.microsoft.com> wrote in message
news:2E**********************************@microsof t.com...
Hi Daniel,

You started on the right path. You define events in your class but you
handle them elsewhere like in your form. Following article on .NET
Framework
Developer's guide should help you:

http://msdn.microsoft.com/library/de...ini-sample.asp

Mar 28 '06 #3
> Is there anyway to attach an event from within my network class to the
listbox on the form so that whenever that event is fired i can tell the
listbox to catch it and update itself?


You wouldn't have the ListBox react to the event. Instead, you would
have the Form listen for the event and then take some action as a
result, which in your case would be add an item to the ListBox.

Assuming that your network connection class makes only one connection
(so the event itself doesn't need to carry with it information about
_which_ connection was made), your code might look something like this:

In your network connection class:

public class NetworkConnection
{
...
public event System.EventHandler ConnectionEstablished;
...
private EstablishConnection(...)
{
... do some stuff to establish the connection ...
if (this.ConnectionEstablished != null)
{
this.ConnectionEstablished(this, System.EventArgs.Empty);
}
}
}

Then, inside your Form class:

public class MyForm : Form
{
private NetworkConnection _connection;

public MyForm()
{
InitializeComponents();
this._connection = new NetworkConnection(...);
this._connection.ConnectionEstablished += new
System.EventHandler(HandleNewConnection);
}
...
private void HandleNewConnection(object sender, System.EventArgs e)
{
NetworkConnection conn = (NetworkConnection)sender;
... now add an item to the ListBox because of the new
connection established by NetworkConnection "conn" ...
}
}

Mar 28 '06 #4
In Windows Forms, UI must be updated on the main thread, handle your event in
the form code as Bruce has suggested.

Mar 28 '06 #5
Yes, it can be done. You need to look at Invoke / BeginInvoke. See Jon
Skeet's article on threading in Windows Forms:

http://www.yoda.arachsys.com/csharp/...winforms.shtml

I believe that the upshot is that in your Form's event handler, you
need to invoke another form method to do the actual update on the
ListBox:

private void HandleNewConnection(object sender, System.EventArgs e)

{
if (this._listBox1.InvokeRequired)
{
this._listBox1.Invoke(new
NetworkConnectionEstablishedHandler(UpdateListBox, new object[] {
sender }));
}
}

private delegate void
NetworkConnectionEstablishedHandler(NetworkConnect ion conn);

private void UpdateListBox(NetworkConnection conn)
{
... now add an item to the ListBox because of the new
connection established by NetworkConnection "conn" ...
}

.... I think ... :)

This assumes that the HandleNewConnection event handler is only ever
called as a result of an event raised by a NetworkConnection object.

I don't have much experience in this area, so if anyone else knows a
better way to do these, please chime in. (Jon?)

Mar 28 '06 #6

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

Similar topics

4
by: anatushi | last post by:
Hi, im looking for help on how to add a text fader to my website, usually i'm working with css but in this case i need to add java script to make it work, that wasn't the problem but somehow it...
0
by: news.microsoft.com | last post by:
Where can I find some code on custom WMI events? I am trying to create a Windows Service that fires events and a Windows Application to monitor the Service. For some reason, I get the events but...
1
by: Jim | last post by:
Can anyone help me, I'm trying to catch the OnFocus and OnBlur events for an asp.net label. I want to change the forecolor of the label text when it is moused over, and then revert it back when the...
2
by: hakan_cn | last post by:
Hi all! Sorry for the cross-post, I really have a difficult problem and need some help badly. I have a VB6 ActiveX component (dll) that listens to a real-time data feed of stock quotes (built...
2
by: grissom | last post by:
Hi I have been challenged to build a database program for my place of work I have no prior programming experience and all of this is totally new to me. The program or database will store...
3
by: RobinS | last post by:
I'm trying to learn WPF and do it in VB instead of C#. (God forbid I should do *anything* the easy way. ;-) Here's something weird. On p162-3 of this book by Petzold (in C# of course) in an...
1
by: slikrik98 | last post by:
Greetings, I believe I have narrowed down my issues to one simple question, and I'm hoping someone with async events experience can help me out. My question is: how is using...
8
by: sachinv1821 | last post by:
hi all i have simple Problem please tell me the Solution if u know?? main() { int a={1,2,3,4,5,6,7,8,9}; printf("%u %u %u",a,a,a); } when i execute this program i am getting a Fixed address...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.