Communicating with a non-modal child form

  • Thread starter Thread starter Jan Roelof de Pijper
  • Start date Start date
J

Jan Roelof de Pijper

I am fairly new to C# and I want to do the following: I have a main
form and have added a second form, frmScreen, to the project, which
contains a button and a TextBox control. In my main form, I create an
instance of this second form and show it, like so:

Form frmMyScreen = new frmScreen( );
frmMyScreen.Show( );

My question is, how do I communicate between my main form and this
second form? How can I look at the Text property of the TextBox on the
second form, or how do I detect whether the button on the second form
is pressed? Or is this impossible and do I need to work with
user-defined messages or something like that?

Any answers will be much appreciated!

Jan Roelof
 
You could create custom events in the frmScreen class that the main form can
subscribe to before calling "frmMyScreen.Show( )". Then when something
interesting happens in frmScreen the main form can hear about it via the
custom event(s). If you look into the MSDN documentation you should find at
least one really good example of creating custom events.
 
You could create custom events in the frmScreen class that the main form can
subscribe to before calling "frmMyScreen.Show( )". Then when something
interesting happens in frmScreen the main form can hear about it via the
custom event(s). If you look into the MSDN documentation you should find at
least one really good example of creating custom events.

Thanks Tim, I'll look into that - it's new terrain for me. In the
"old" MFC days, I would have worked with user-defined windows messages
to exchange information between the two forms. Do you know if that is
still possible using the .NET framework and C#?

Thanks,
Jan Roelof
 
Yeah, it is. You can still get into the "WndProc" for any control - noting
at this point that a Form is considered a control - but it probably would be
easier for you in the long term to actually write the custom events. Here is
a good, and short, introduction to custom events:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkEventsTutorial.asp

Thanks again Tim! I'll be sure to study this topic thoroughly. One
thing I just can't understand, and I hope you can help me there, too.
Let me repeat part of my original message:

I have a main form and have added a second form, frmScreen, to the
project, which contains a button and a TextBox control. In my main
form, I create an instance of this second form and show it, like so:

Form frmMyScreen = new frmScreen( );
frmMyScreen.Show( );

Why is it that if I add a public method to the frmScreen class, it is
not available to me in the instance I create in my main form? In the
frmScreen class, I added a public method String GetEditText() which
simply returns the contents of the edit control, but it is not made
available to me in my main form, not througn intellisense, and if I
try to call it, the compiler warns that 'System.Windows.Forms.Form'
does not contain a definition for 'GetEditText'.
So, frmMyScreen.GetEditText() appears to be a nono and I just don't
understand why!

Thanks,
Jan Roelof
 
The reason for this is that the compiler cannot see the new method because
all it has to go off of is the declared reference type of "Form".
Form frmMyScreen = new frmScreen( );
So you will need to use a line like this:
frmScreen frmMyScreen = new frmScreen( );
 
The reason for this is that the compiler cannot see the new method because
all it has to go off of is the declared reference type of "Form".
Form frmMyScreen = new frmScreen( );
So you will need to use a line like this:
frmScreen frmMyScreen = new frmScreen( );

OUCH. That really is a capital blunder! Thanks a heap, I now have
direct access to the frmScreen's controls, if I make them public! In
combination with the events that you suggested I should now be able to
achieve what I want :)

Thanks again,
Jan Roelof
 
Back
Top