By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,989 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,989 IT Pros & Developers. It's quick & easy.

Simple prob with events, pls help

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.