Solved

Newbie delegate question - how to declare

Posted on 2006-07-07
2
220 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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