Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to open a child window on row command, pass value to child window and return values to parent form in vb.net ?
Posted
Comments
Sergey Alexandrovich Kryukov 15-Nov-11 14:52pm    
What is "row command"?
--SA

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
 
Share this answer
 
Agree With SAKryukov

But if ur program is not so Big, u can Variables to it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900