How to access an event from another Form ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ital1@boursorama.com

    How to access an event from another Form ?

    Hi

    I have created one form that has an event handler, coded in the
    following lines :


    (ON FORM1 : Picture Boxes, when clicked they fire events)

    this.OnPADClick += new FrmPAD.OnPADCli ckEventHandler
    (this.FrmPAD_On PADClick);

    private void ArrowUp_Click(o bject sender, System.EventArg s e)
    {
    int iNumToPass = 0;
    System.EventArg s p = new System.EventArg s();
    OnPADClick (this, p, iNumToPass);
    return;
    }

    public delegate void OnPADClickEvent Handler (object sender,
    System.EventArg s e, long num);
    public event OnPADClickEvent Handler OnPADClick;

    private void FrmPAD_OnPADCli ck (object sender, System.EventArg s e, long
    theNum)
    {
    switch(theNum)
    {
    case 0:
    MessageBox.Show ("Click on PictureBox !");
    break;
    default:
    break;
    }
    }



    (ON FORM2, What I want to do) :

    I need to get the event from another form (Form2), using the same kind
    of function as "private void FrmPAD_OnPADCli ck(object sender,
    System.EventArg s e, long theNum);"

    Could anyone point me in the right direction... What piece of code to
    add to Form2 to get events fired from Form1 ?

    Thank you very much.

  • Justin

    #2
    Re: How to access an event from another Form ?

    Are they in the same namespace? The same class? If you can reference
    Form2 from Form1 then you can set up a custom event in Form2 and have
    Form1 subscribe to it. Wherever you create Form2 and have a reference
    to it, you can do exactly what you do at the beginning of your code
    block.

    <reference to Form2>.<Form2 event you want to capture> +=
    FrmPAD_OnPADCli ck (etc.)

    Here's an example.

    Form2 code

    public event FrmPAD.OnPADCli ckEventHandler somethingHappen ed;

    public void FunctionThatCau sesEventToFire( )
    {
    somethingHappen ed(this, new EventArgs());
    }

    Form1 code

    Form2 instanceName = new Form2();
    instanceName.so methingHappened += new
    FrmPAD.OnPADCli ckEventHandler( this.FrmPAD_OnP ADClick);

    That should cause FrmPAD_OnPADCli ck to get called when the event
    happens in Form2. Hope that helps.

    Comment

    • ital1@boursorama.com

      #3
      Re: How to access an event from another Form ?

      Great! That works perfectly... I did try the same thing, but without
      using an instantiated version of the form that fires the event...

      What I did is to centralize all the form on an unique class (that
      memorizes handles to the different forms) and to subscribe to the event
      as you describe...

      Thank you VERY VERY VERY much, that helps me a lot ! :)



      Justin a écrit :
      [color=blue]
      > Are they in the same namespace? The same class? If you can reference
      > Form2 from Form1 then you can set up a custom event in Form2 and have
      > Form1 subscribe to it. Wherever you create Form2 and have a reference
      > to it, you can do exactly what you do at the beginning of your code
      > block.
      >
      > <reference to Form2>.<Form2 event you want to capture> +=
      > FrmPAD_OnPADCli ck (etc.)
      >
      > Here's an example.
      >
      > Form2 code
      >
      > public event FrmPAD.OnPADCli ckEventHandler somethingHappen ed;
      >
      > public void FunctionThatCau sesEventToFire( )
      > {
      > somethingHappen ed(this, new EventArgs());
      > }
      >
      > Form1 code
      >
      > Form2 instanceName = new Form2();
      > instanceName.so methingHappened += new
      > FrmPAD.OnPADCli ckEventHandler( this.FrmPAD_OnP ADClick);
      >
      > That should cause FrmPAD_OnPADCli ck to get called when the event
      > happens in Form2. Hope that helps.[/color]

      Comment

      Working...