First of all, there is no even though a
System.Windows.Forms.Form
is
System.Windows.Forms.Control
and hence has the properties
System.Windows.Forms.Control.Controls
and
System.Windows.Forms.Control.Parent
, the control-parent relationship between forms is
not functional. For further detail, see my past solution
Parent Child Form Relationship[
^].
See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[
^].
When you say "parent form" you might mean just a form, perhaps the main form, which is really irrelevant in the context of the question.
Now, it looks like you simply need to run a second form as a modal dialog, which you can do using the method
System.Windows.Forms.Form.ShowDialog
, see
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx[
^].
Note that after the form shown as a modal dialog is closed, its instance remain valid and accessible, so it can be used to get its status (some value you wanted to determine as a result of form operation). Now, the remaining problem is how to set some value(s) before you show this form and how to get back resulting values.
Well, you simply need to have a couple of field members in this form, one for setting before showing, another for reading after that. You can expose these two fields as a write-only property and a read-only, property, respectively. The fields should be declared
private
, but to make the two properties accessible from the other form, declare them as
internal
.
That would be all for the access to form data, but just for record: there is a more accurate way to do that: implementation of appropriate interface in the form class, see my past solutions:
How to pass the Value one form to another form in windows form[
^],
How to copy all the items between listboxes in two forms[
^].
One bonus advice:
Do you need to preserve the status of the dialog when you show it another time? You might need it, to preserve default value(s), status of the focus, etc. In this case, you need to keep the form accessible in the form of a field of the form which shows the dialog. Leave it
null
at first and use a
lazy evaluation
pattern: create an instance of the form only when it should be shown for the first time. That's it: first check if the instance is
null
; if not, use existing instance, if it is
null
, create an instance.
—SA