Solved

show() and ShowDialog()

Posted on 2011-03-10
5
790 Views
Last Modified: 2012-05-11
I have two forms: Form1 and Form2, I want to Open form2 from form1 and return the value from Form2 to Form1, in addition, I want form1 stay active after form2 is opened.  When I call form2.show() , both forms are active, but form2 can't return any value to form2; While I call form2.showDialog(), I can return result from form2 to form1 but form1 is inactive. I know the differences between show() and ShowDiaglog(). However, what I need is the combination of two.  Is it possible? Just want to get some advise from experts before I say "no" to boss for this request.    

Thank you inadvance for help!
0
Comment
Question by:jrmc-pgmr
5 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35101840
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35102101
When you call ShowDialog(), code stops in the primary form until the secondary form is closed.  When the secondary form closes, you still have a reference to it so you can extract whatever you want:
' In Form1
    Dim f2 As New Form2
    If f2.ShowDialog = DialogResult.OK Then
        ' ...extract something from "f2" ...
        Me.Label1.Text = f2.TextBox1.Text
    End If

Open in new window


Note that we are checking the DialogResult() of Form2 to determine whether "OK" or "Cancel" was pressed.  In Form2, you'd simply do something like this to close the form and allow Form1 to continue and extract a value:
' In Form2
Private Sub btnOK_Click(...) Handles btnOK.Click
    Me.DialogResult = DialogResult.OK ' closes Form2 and allows Form1 to continue
End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:bansidhar
ID: 35103742
you can do it. there is more than one way.

one way just pass form1 to form2 and when it closes update form1 or raise some event in form1. but i don't know why you want to do this, this can be dangerous because when the form2 closes and tries to update frm1, form1 may be in totally different state than expected or don't even exist.
0
 

Author Comment

by:jrmc-pgmr
ID: 35104555
Thanks all who helped!
Bansidhar: I understand the risk.   Could you provide sample code that will achieve this? Thanks
0
 
LVL 11

Accepted Solution

by:
bansidhar earned 500 total points
ID: 35105917
Here is a sample. hope this helps
WindowsApplication1.zip
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question