Raising Events across forms

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.
 
R

Rodger Constandse

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?
 
A

Ajay Kalra

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.
 
M

Michael C

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top