QUOTE(nbarten @ 20 Aug, 2008 - 03:50 PM)

Thanks++

it works fine.
How works the other one? I know how to create events in the same class.
You're welcome.
The other way is a bit more convoluted, but there are benefits...
So, I just made a little sample project called DreamInCodeStuff and added a class called FtpConnection. I commented the code pretty heavilly, but if you have any questions feel free to ask...
First we set up the class...
CODE
using System;
namespace DreamInCodeStuff
{
// First we create an EventArgs class defining the information we'll pass back to the subscribing form...
public class FtpMessageEventArgs : EventArgs
{
// Here we are defining two strings to send, but you could send any data...
// (I added the stamp just to illustrate that you can pass more than just the string for the textbox)
private string dtTmStmp;
private string msg;
// This constructor fills the variables when the instance is created...
public FtpMessageEventArgs(string dateTimeStamp, string message)
{
dtTmStmp = dateTimeStamp;
msg = message;
}
// These readonly properties allow the subscribing form to get the info you are passing...
public string DateTimeStamp
{
get { return dtTmStmp; }
}
public string Message
{
get { return msg; }
}
}
// Next we have to create a delegate. This defines what our event method needs to look like.
public delegate void FtpMessageEventHandler(object sender, FtpMessageEventArgs mArgs);
class FtpConnection
{
// Here we declare our event based on the delegate we defined earlier.
// We make the event public so that anyone using our class can subscribe
// to it with their own handler routine...
// Basically, this is just a pointer to a method on the subsrcibing form.
// (Just like we made a TextBox TB to point to a textbox on a form
// when we did this the *EASY* way)
public event FtpMessageEventHandler MessageReceivedEvent;
// Here is your public method for testing the whole thing...
public void DoThis()
{
string message = "Some FTP Info...";
FtpMessageEventArgs tmArgs = new FtpMessageEventArgs(DateTime.Now.ToLocalTime().ToString(), message);
// Make sure the event has been subscribed to (it actually points to a method.)
if (MessageReceivedEvent != null)
MessageReceivedEvent(this, tmArgs);
}
}
}
Then a form to test it from...
CODE
using System;
using System.Windows.Forms;
namespace DreamInCodeStuff
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Here we define a method with the same return type and parameters as the FtpConnection classes event.
// We will assign this method to the public event on the FtpConnection class.
private void FtpMessageReceived(object sender, FtpMessageEventArgs args)
{
string timeStamp = args.DateTimeStamp.Trim();
string message = args.Message.Trim();
txtFtpInfo.Text = timeStamp + ": " + message;
}
private void Form1_Load(object sender, EventArgs e)
{
FtpConnection con = new FtpConnection();
// Point the event to our local method
con.MessageReceivedEvent += new FtpMessageEventHandler(FtpMessageReceived);
// test it..
con.DoThis();
}
}
}
Hope this helps,
Kitt