Solved

show() and ShowDialog()

Posted on 2011-03-10
5
784 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now