Solved

Newbie delegate question - how to declare

Posted on 2006-07-07
2
223 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
[X]
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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

630 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