views:

99

answers:

1

I have some Events I created on my own and was wondering on how to raise them when I want.

Probably my application design is also messed up, might take a look at that if you like.

This is the Structure

ProgramContext
 - Form MainWindow
  + Control TextBox
  + Control Button
  + ...

In this case, the MainWindow.TextBox holds some information that is updated quite often at runtime. So, I somehow need it to refresh itself when I want to (so it can reload its data from the database, where the it's stored)

I tried hooking an EventHandler to its Validating-Event, but that didn't seem to do the trick.

So, basically I have a method that reloads the data in ProgramContext

DataTable table = _adapter.GetData();
foreach (DataRow row in table.Rows)
{
  MainWindow.TextBox.Text += table.Text.ToString();
}

That needs to be done whenever another method (that writes new data into table) is executed.

Any ideas?

+1  A: 

Edit : It seems your question is more about hooking into a specific event, but FWIW below is how to fire custom events in general.

From what I understand, you want an external party to monitor events raised from a textbox on a Form and then to reload data on another form?

A quick and dirty would be to make the Form TextBox public and then others could subscribe to this event

MainForm.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

You might also look at the Ent Lib Composite Application Block and Smart Client Software Factory - this has a very flexible event broking / pub sub mechanism for synchronsing across UI "SmartParts" (controls, forms dialogs etc) in a loose coupled fashion.

// Assuming you need a custom signature for your event. If not, use an existing standard event delegate
public delegate void myDelegate(object sender, YourCustomArgsHere args);

// Expose the event off your component
public event myDelegate MyEvent;

And to raise it
if (MyEvent != null)
{
MyEvent(this, myCustomArgsHere);
}
nonnb
@nonnb: In your example, instead of declaring a new delegate, it'd be clearer to just use the generic `EventHandler` as in `public event EventHandler<YourCustomArgsHere> MyEvent`
Johann Gerell
I also tried hooking to `TextChanged`, but that will end in an infinite loop, as I want to change the text within the EventHandler
ApoY2k