Solved

Newbie delegate question - how to declare

Posted on 2006-07-07
2
216 Views
Last Modified: 2010-05-18
Hi

I have an windows form app with a textbox. In a second class I have a socket server and client connection. The idea is when a connection is made to the server port the network stream is sent on through the client to a destination port. I would like to display this data (an xml message) along with any responses in the text box in my form. After searching it seems I need to use a delegate pointing to a method which will update the textbox. In my form I have created the method:

class Form1
{
....
public void update_txtBox(string data)
        {
            richTextBox1.AppendText(data);
        }
}

In the socket class I get the network stream and send it to the destination port but am struggling to write the data to the textbox.

class socket
{
public delegate void AddText(string data);
......
AddText updateTextBox = new AddText(Form1.update_txtBox);
.....
}

My questions:
Am I on the right track?
Do I declare the delegate in the form class or the socket class or either?
In which class do I declare the delegate variable and is my attempt above correct? (it doesn't work so probably not :)

Any help or a pointer to a good tutorial/explanation will be appreciated.

Matt
0
Comment
Question by:matt_d_p
2 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 125 total points
ID: 17057854
Hi,

A good approach would be to use an event... the event is fired in the socket class, the main form can consume the event and update the text box appropriatly... the use of events requires delegates anyway. In your socket class define a delegate and an event like this:

     public delegate void TextReceivedDelegate(string text);
     public event TextReceivedDelegate OnTextReceived;

In your form, when you create an instance of the Socket class you can subscribe to the event like this:

MySocketClass msc = new MySocketClass();
msc.OnTextReceived += new TextReceivedDelegate(SocketTextReceivedEventHandler);

Note that when you type "+=" and press the space bar in Visual Studio.NET you can then press the Tab key to insert the event handling method automatically. If you type it in manually you'll have  to define the method (SocketTextReceivedEventHandler) in your form:

public void SocketTextReceivedEventHandler(string text)
{
      myTextBox.Append(text);
}

The only thing left now is to fire the event in the socket class, to do that just call the event and send the text in a parameter, like this:

this.OnTextReceived("the text here");

More on events in C# here: http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=380
0
 

Author Comment

by:matt_d_p
ID: 17067469
Thanks Zephyr__

This gives me a much clearer idea of what I need to do. Will read up a bit first and them have a go at implemeting it.
Matt
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now