473,785 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1116
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?

"yogeshprab hu" <yo**********@d iscussions.micr osoft.com> wrote in message
news:2E******** *************** ***********@mic rosoft.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 NetworkConnecti on
{
...
public event System.EventHan dler ConnectionEstab lished;
...
private EstablishConnec tion(...)
{
... do some stuff to establish the connection ...
if (this.Connectio nEstablished != null)
{
this.Connection Established(thi s, System.EventArg s.Empty);
}
}
}

Then, inside your Form class:

public class MyForm : Form
{
private NetworkConnecti on _connection;

public MyForm()
{
InitializeCompo nents();
this._connectio n = new NetworkConnecti on(...);
this._connectio n.ConnectionEst ablished += new
System.EventHan dler(HandleNewC onnection);
}
...
private void HandleNewConnec tion(object sender, System.EventArg s e)
{
NetworkConnecti on conn = (NetworkConnect ion)sender;
... now add an item to the ListBox because of the new
connection established by NetworkConnecti on "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 HandleNewConnec tion(object sender, System.EventArg s e)

{
if (this._listBox1 .InvokeRequired )
{
this._listBox1. Invoke(new
NetworkConnecti onEstablishedHa ndler(UpdateLis tBox, new object[] {
sender }));
}
}

private delegate void
NetworkConnecti onEstablishedHa ndler(NetworkCo nnection conn);

private void UpdateListBox(N etworkConnectio n conn)
{
... now add an item to the ListBox because of the new
connection established by NetworkConnecti on "conn" ...
}

.... I think ... :)

This assumes that the HandleNewConnec tion event handler is only ever
called as a result of an event raised by a NetworkConnecti on 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
1855
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 seems like they are not compatible with each other so when i add the text fader code in the <body> tag,the rest of my site is gone :S if someone please can have a look at my code and tell what am i doing wrong. thx allot for whom of you who can =) ...
0
1298
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 all the properties are NULL.
1
973
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 focus is taken away. Thanks in advance for any help.
2
2570
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 in an OCX) and then passes them on using events. The consumer of these VB6 events is a C# dll using .NET 2.0
2
1284
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 occurences and incidents and will be used by security staff logging events that happen at my place of work I was advised by a programmer to use visual basic for this but recently I was told it would be easier to make it using MS Access. Firstly...
3
3573
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 example about using Routed Events, he has a loop that assigns an eventhandler to the PreviewKeyUp, etc., events, like this: //these are elements defined in code above this point UIElement els = { win, grid, btn, text }; foreach (UIElement el in els)
1
2523
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 EventsHelper.FireAsync() different from using EventsHelper.Fire() BUT having the listener's event handler spawn a new thread to do the work? In my program, I have a data feed coming in and I have a separate form that has a DataGridView on it. Each time a message...
8
1259
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 Value(multi digit value) can any body explain me ahy this is giving the same value for different index
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10357
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10101
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.