Groups | Blog | Home
all groups > dotnet windows forms > may 2005 >

dotnet windows forms : Child Windows



JezB
5/27/2005 12:00:00 AM
I'm new to windows forms, and I'm a bit puzzled how to add child windows to
the main window and interact between them. In my case I just want to open up
an Options window which, depending on what the user does in there, affects
things on the main window.

There's no ChildWindow control as far as I can see so I assume I'll have to
create my Options window as a new Form object and create it and show it from
my main form. But I want to apply changes to my main window from the options
window. So as I see it I can either:

(1) pass a reference to the master form to my child form and within code on
my child form make changes directly to the controls on the master form - of
course to do this I have to make those controls public, or

(2) create delegates and events on my child form and subscribe to those
events on my main form, so that I can manipulate the controls via code on
the master form.

Both these approaches seem more complex that it really should be ! Is there
an easier way or am I missing something ?


Tim Wilson
5/27/2005 12:57:35 PM
Do you want the user to be able to still work with the main Form while the
child Form is open, or is it acceptable to open the child Form in a modal
state?

If you're showing the child Form using the ShowDialog method, modal, then
you may consider exposing the appropriate information using custom
properties, in the same way that the common dialogs work. Assuming that you
have an "OK, Cancel, Apply" setup in your dialog, then you may want to
expose an "Apply" event from the dialog that the main Form can hook before
showing the child. Handling the "OK" and "Cancel" of the dialog would work
as usual for a common dialog.

if (child.ShowDialog() == DialogResult.OK)
{
// Then the user wants the changes to be applied/committed.
}

So in the above scenario you'd need to create some custom properties, or
just one if you're exposing all the information through a custom class
instance, and one custom event. This is like option "(2)".

--
Tim Wilson
..Net Compact Framework MVP

[quoted text, click to view]

Tim Wilson
5/31/2005 8:23:03 AM
Every Form is an object that, by default, encapsulates and hides the
controls within it. So short of exposing every control by making it internal
or public, which IMO would be bad practice, then you'd need to create some
special properties and/or events.

--
Tim Wilson
..Net Compact Framework MVP

[quoted text, click to view]

JezB
5/31/2005 11:10:45 AM
OK thanks - that helps a bit.

I was hoping that one form could "own" another so that each has access to
all the other's controls without exposing public variables or delegates,
keeping things nice and simple.

[quoted text, click to view]

AddThis Social Bookmark Button