简体   繁体   中英

Use Reference EventHandler between Forms

I have a question about EventHandler (in this case i want to create a very simple Form with only progressBar).

I have Two Forms:

public partial class Form1 : Form {

  public Form1() {
     InitializeComponent();
  }
  /// <summary>
  /// Event to notify change
  /// </summary>
  private EventHandler progress;

  private void button1_Click(object sender, EventArgs e) {

     Form2 BarForm = new Form2() { SomeEvent = progress};// <- When i assign handler in Form2 class progress in Form1 is always null
     BarForm.Show();

     BarForm.sethandler(ref progress);// <- If i don't insert this function with "ref" progress is null.

     for (int i = 0; i < 1000000; i++) {
        if (progress != null)
           progress(this, new EventArgs());
     }
  }

}

And the 2° :

public partial class Form2 : Form {

  public EventHandler SomeEvent{ get; set; }

  public Form2() {
     InitializeComponent();
  }

  private void Form2_Load(object sender, EventArgs e) {

     this.progressBar1.Maximum = 1000000;
     this.progressBar1.Minimum = 0;

     SomeEvent += new EventHandler(new Action<object, EventArgs>((s, ee) => {
        this.progressBar1.PerformStep();
     }));// <- Here i assign handler but ONLY in this Form i have a SomeEvent != null

  }
  /// <summary>
  /// without this function the Event Handler in other form remain always null
  /// </summary>
  /// <param name="h"></param>
  public void sethandler( ref EventHandler h){           
        h += new EventHandler(new Action<object, EventArgs>((s, ee) => {
        this.progressBar1.PerformStep();
     }));

  }

}

Well, if i remove the line "DarForm.sethandler(ref progress)", the handler in this class is null although in the Form2_Load i set handler passed from Form1. I thought that in command line

 Form2 BarForm = new Form2() { SomeEvent = progress};// <- When i assign handler in Form2 class progress in Form1 is always null

I had assigned the reference of "progress" in Form1 to "SomeEvent" in Form2.

Now,to set an EventHandler of one class in other i must use ref KeyWord?

Thank you.

You need to listen to the events of the main form in the child form rather than passing the reference of the event handler. Dont pass reference of the event handler to the other forms. Instead pass Form1 reference as explained in the below link - Listening to Events in Main Form from Another Form in C# . Then subscribe to Form1.Progress in your form2 and handle the event.

Edit You shouldn't pass the reference of EventHandler like normal objects, as they are not meant to be passed as references but used as publisher subscribers. If you are keen on not passing Form1 object as it is, you can abstract just the event handler part into an interface and pass only that.

Eg

interface IProgressHandler
{
  EventHandler Progress{get;set;}
}

Form1 : IProgressHandler
{
Form2 BarForm = new Form2(this) ;
}

Form2
{
   IProgressHandler  _progressHandler
   public Form2(IProgressHandler progressHandler) 
   {
     _progressHandler = progressHandler;
     _progressHandler.Progress+=(s, ee) => { this.progressBar1.PerformStep() };
     InitializeComponent();
   }
}

This works cleanly

Form1:

public EventHandler SomeEvent { get; set; }
public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    Form2 f = new Form2();
    this.SomeEvent += new EventHandler(f.IncProgress);
    f.Show();
    for (int i = 0; i < 1000000; i++)
    {
        if (SomeEvent != null)
            SomeEvent(this, new EventArgs());
    }
    f.Close();

}

Form2:

   public Form2()
    {
        InitializeComponent();
        this.progressBar1.Maximum = 1000000;
        this.progressBar1.Minimum = 0;
    }

    public void IncProgress(object Sender, EventArgs e)
    {
        this.progressBar1.PerformStep();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM