On Mon, 14 Apr 2008 13:57:50 -0700, Marc Gravell <ma**********@gmail.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 PropertyChangedEventArgs : EventArgs
{
public readonly string Property;
public PropertyChangedEventArgs(string Property)
{
this.Property = Property;
}
}
delegate void PropertyChangedEventHandler(object sender,
PropertyChangedEventArgs e);
class Form1 : Form
{
protected override void OnLoad(System.EventArgs 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, PropertyChangedEventArgs 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(send_Click);
send.Dock = DockStyle.Bottom;
Controls.Add(tb);
Controls.Add(send);
}
void send_Click(object sender, EventArgs e)
{
PropertyChangedEventHandler handler = SomeEvent;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(tb.Text));
}
}
public event PropertyChangedEventHandler SomeEvent;
}
static class Program
{
static void Main()
{
Application.EnableVisualStyles();
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