Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Newbie delegate question - how to declare

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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