On Mon, 14 Apr 2008 13:57:50 -0700, Marc Gravell <ma**********@g mail.com
wrote:
You need to expose via properties:
Alternatively, you could pass the new value in an EventArgs-derived
parameter for the event.
Using Marc's code as a starting point (including the lack of a namespace
declaration :) ):
using System.Windows. Forms;
using System;
class PropertyChanged EventArgs : EventArgs
{
public readonly string Property;
public PropertyChanged EventArgs(strin g Property)
{
this.Property = Property;
}
}
delegate void PropertyChanged EventHandler(ob ject sender,
PropertyChanged EventArgs e);
class Form1 : Form
{
protected override void OnLoad(System.E ventArgs e)
{
base.OnLoad(e);
// load second form
Form2 other = new Form2();
other.SomeEvent += other_SomeEvent ;
other.Show(this );
}
public Form1()
{
Text = "Form1";
}
void other_SomeEvent (object sender, PropertyChanged EventArgs e)
{
this.Text = e.Property;
}
}
class Form2 : Form
{
TextBox tb;
Button send;
public Form2()
{
Text = "Form2";
tb = new TextBox();
send = new Button();
send.Text = "Send";
send.Click += new EventHandler(se nd_Click);
send.Dock = DockStyle.Botto m;
Controls.Add(tb );
Controls.Add(se nd);
}
void send_Click(obje ct sender, EventArgs e)
{
PropertyChanged EventHandler handler = SomeEvent;
if (handler != null)
{
handler(this, new PropertyChanged EventArgs(tb.Te xt));
}
}
public event PropertyChanged EventHandler SomeEvent;
}
static class Program
{
static void Main()
{
Application.Ena bleVisualStyles ();
Application.Run (new Form1());
}
}
Either way would work and either is acceptable IMHO. The trade-off is
mainly whether you want to create a new property, or a whole new EventArgs
and delegate type for the event.
Pete