In general, it's a very bad idea to cast the sender argument of an event and try to use it. I don't know why Microsoft designed their event structure like this, but there you go.
I don't have a C# compiler anywhere around, but it looks like you really want to be passing the subject in the EventArgs. You can do this a couple of different ways. If you're using 2005, you can use the generic EventHandler to send a string:
Code:
public event EventHandler<string> Apply;
Then you can send the string in your Apply.
Code:
if(Apply!=null)
Apply(this,tx1.Text);
Of course, then your handler has to look a bit different.
Code:
private void dlg_Apply(object sender, string e)
{
MessageBox.Show(e);
}
If you're not using 2005, you have to derive a class from EventArgs that includes a string.
Code:
public class StringEventArgs : EventArgs
{
public readonly Str;
public StringEventArgs(string str) : base()
{
Str = str;
}
}
Then when you fire Apply, you should send your new EventArgs class:
Code:
if(Apply!=null)
Apply(this,new StringEventArgs(tx1.Text));
Finally, in your handler code, you should cast the args so you can use it.
Code:
private void dlg_Apply(object sender, EventArgs e)
{
StringEventArgs args = e as StringEventArgs;
if (args != null)
MessageBox.Show(args.Str);
}
That's the general idea anyway. I wouldn't be surprised if it doesn't compile; I might have forgotten something.