Raising Events across forms

  • Thread starter Thread starter L
  • Start date Start date
L

L

Hi,

I have a windows form say "Form1" which has a menuitem "SetUp".
When I click this item, a new form "SetUpForm" opens up. When I change
the fields in the "SetUpForm" and click the "OK" button, SetUpForm
should validate the fields and if all the field values are right, it
should close and raise an event in the "Form1". I tried defining an
event using a delegate in "Form1" and then raise the event from
"SetUpForm", but the compiler is throwing an error. How do I write code
to achieve this requirement?

Thanks,
Lalasa.
 
Is the SetUpForm being opened as a dialog (with ShowDialog()) or as a regular
form (with Show()). If you are doing it as a dialog, you could raise the event
after the ShowDialog call returns in Form1.

Otherwise, you could hook the Closed event for SetUpForm from Form1 and use it
to trigger the raising of the event in Form1. Alternatively, you could call a
public method of Form1 to raise the event after you do the validation.

Maybe if you provide some small code snippets of what you are trying to do?
 
Hello L,
I have a windows form say "Form1" which has a menuitem "SetUp".
When I click this item, a new form "SetUpForm" opens up. When I change
the fields in the "SetUpForm" and click the "OK" button, SetUpForm
should validate the fields and if all the field values are right, it
should close and raise an event in the "Form1". I tried defining an
event using a delegate in "Form1" and then raise the event from
"SetUpForm", but the compiler is throwing an error. How do I write
code to achieve this requirement?

Events can only be raised by a class that declares it. See here: http://blog.monstuff.com/archives/000040.html

What you want to do is simply a callback from SetUpForm. Pass Form1 to SetUpForm
in the constructor or with some property. Whenever it is appropriate, SetUpForm
object will call a public method in Form1 object. Another option is to have
the event declared in SetUpForm itself and make Form1 subscribe to this event.
SetUpForm will fire the event and Form1 object will then get it. Yet another
option is to use delegates, but that will be overkill.
 
L said:
Hi,

I have a windows form say "Form1" which has a menuitem "SetUp".
When I click this item, a new form "SetUpForm" opens up. When I change
the fields in the "SetUpForm" and click the "OK" button, SetUpForm
should validate the fields and if all the field values are right, it
should close and raise an event in the "Form1". I tried defining an
event using a delegate in "Form1" and then raise the event from
"SetUpForm", but the compiler is throwing an error. How do I write code
to achieve this requirement?

You don't need an event. When Form1 shows SetupForm I presume you show it
modally, just put the code you need after the ShowDialog()

eg

SetupForm.ShowDialog()
x = SetupForm.SomeValue;
etc

Michael
 
Back
Top