events in forms

F

factis

I have Form1() and inside it I instantialize Form2() wich is like tool
window

Form1()
{
Form2() form2 = new Form2();
form2.Show();
}

how can i catch button_click events from form2 in Form1().
 
P

Patrice

A form is a class as any other class so you could expose whatever you want
by adding members to the Form2 class (if I remember you can even make a
member public in the propery window). That said, in this particular case it
might be beneficial to avoid exposing gory details i.e. it could be better
to handle low level events in the Form2 class and expose higher level
events.

For example as this is a tool window, an even that reports about the
selected tool or something similar rather than exposing directly control
events (not sure what is the meaning of this button click). This way you
won't depend on Form2 details.
 
F

factis

thx, did that and it works

Patrice said:
A form is a class as any other class so you could expose whatever you want
by adding members to the Form2 class (if I remember you can even make a
member public in the propery window). That said, in this particular case it
might be beneficial to avoid exposing gory details i.e. it could be better
to handle low level events in the Form2 class and expose higher level
events.

For example as this is a tool window, an even that reports about the
selected tool or something similar rather than exposing directly control
events (not sure what is the meaning of this button click). This way you
won't depend on Form2 details.
 
P

Peter Duniho

factis said:
thx, did that and it works

Did what?

If you made the member field of Form2 public so that the code in Form1
can subscribe to an event in the control that field references, then
that's exactly not what Patrice suggested (even though he pointed out it
could be done).

Most likely, what you want is a new event, declared by you, in the Form2
class that is raised when the button is clicked. The event most likely
doesn't even represent a button click; rather, it's a higher-level thing
that just happens to be caused by a button click in that particular
instance.

Without a specific concise-but-complete code example, it's not possible
to provide specific advice. But you definitely should not be making any
member fields public.

Pete
 
F

factis

Peter Duniho said:
Did what?

If you made the member field of Form2 public so that the code in Form1 can
subscribe to an event in the control that field references, then that's
exactly not what Patrice suggested (even though he pointed out it could be
done).

Most likely, what you want is a new event, declared by you, in the Form2
class that is raised when the button is clicked. The event most likely
doesn't even represent a button click; rather, it's a higher-level thing
that just happens to be caused by a button click in that particular
instance.

did this :)
Without a specific concise-but-complete code example, it's not possible to
provide specific advice. But you definitely should not be making any
member fields public.

Pete

tnx for your concern.
 

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