Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Newbie delegate question - how to declare

Posted on 2006-07-07
2
Medium Priority
?
229 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 500 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

Independent Software Vendors: 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

972 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