// First you must make a public EventHandler: public event EventHandler MyFooBar; // Then, when you need to fire the event in your UserControl, do this: if (this.MyTextBox.Text == "FOO") this.OnMyFooBar(new EventArgs()); // Lastly, this raises the MyFooBar event: protected virtual void OnMyFooBar(EventArgs e) { if MyFooBar != null) MyFooBar(this, e); }Then, in your form, you just set up the usual delegates and EventHandlers:
this.oMyControl.MyFooBar += new System.EventHandler(this.oMyControl_MyFooBarHandler); private void oMyControl_MyFooBarHandler(object sender, System.EventArgs e) { // whatever your code needs to be }You can even get more fancy, creating a custom delegate and custom EventArgs, but the above code is sufficient for simple things, when just the built-in System.EventArgs is all you need. For fancier, custom stuff, try this:
public class DoneEventArgs : EventArgs { private int m_MyEventArg; public int MyEventArg { get {return this.m_MyEventArg;} set {this.m_MyEventArg = value;} } }Next, a custom delegate:
public delegate void MyCustomEventHandler(object sender, DoneEventArgs e);Everything else is pretty much the same, substituting stuff in the appropriate places:
public event MyCustomEventHandler MyFooBar;
And in your Form, you'd have this instead:this.oMyControl.MyFooBar += new MyCustomEventHandler(this.oMyControl_MyFooBarHandler); private void oMyControl_MyFooBarHandler(object sender, DoneEventArgs e) { // whatever your code needs to be }So, basically what is happening is that you're telling the UserControl that it should run the Form's oMyControl_MyFooBarHandler() method.
>using System; >using System.Collections.Generic; >using System.Text; > >public delegate string mydel(int number); > >class Program >{ > static void Main(string[] args) > { > MyClass obj1 = new MyClass(10); > MyClass obj2 = new MyClass(20); > > mydel del1 = obj1.myfunc; // I don't understand these 2 lines > mydel del2 = obj2.myfunc; > > Console.WriteLine(del1(50)); > Console.WriteLine(del2(20)); > } >} > >public class MyClass >{ > int x; > public MyClass(int x1) > { > x = x1; > } > public string myfunc(int n) > { > return (n * x).ToString(); > } >} > > >