Forms that return values..

wyrd

Senior Contributor
Joined
Aug 23, 2002
Messages
1,405
Location
California
How can I make a form return a value when it closes? That way I can use it much like a MessageBox and how it returns values.

Thanks in advance.
 
Couldnt you just set a value for some variable during form closing event and then read the variable?
 
Declare a public variable on the form you are to pass the returned value too (form2).
Then assign the variable the "returned" value and instantiate the form before closing the form1.

Form2 might have something like this for example
Visual Basic:
Public Class fclsEmployeeSignIn
    Inherits System.Windows.Forms.Form
    Public strdatasource As String
    Public networkinstallation As Boolean


And Form1 could pass those variable values in a block like this:

Visual Basic:
 Dim frmEmployeeSignIn As New fclsEmployeeSignIn()
        frmEmployeeSignIn.strdatasource = strdatasource
        frmEmployeeSignIn.networkinstallation = networkinstallation
        frmEmployeeSignIn.Show()
        me.close()

or, if you want input from a user, use an inputbox...something like this:
Visual Basic:
 password = InputBox("Re-enter your password.  Your Password may not be left blank.", "Password Confirmation")
        If password = txbPassword.Text Then

     'do some stuff
        End If

Just a couple of ways, depends on what your trying to accomplish.

Jon
 
I appreciate the feedback, unfortunately none were what I was looking for. Fortunately I did find what I was looking for (well, not exactly, but it will definitely make do).

This comes to a surprise to me (as I didn't know I could do this), so hopefully you'll also find it informative (for those who are wondering).

You can assign a DialogResult to the actual form;
this.DialogResult = DialogResult.OK;

By doing this, it automatically closes the form and returns that value to the owner form.

C#:
// Somewhere in owner form...
ChildForm frm = new ChildForm();
if (frm.ShowDialog(this) == DialogResult.OK)
    // Do stuff...

// In some button click event on the child form...
this.DialogResult = DialogResult.OK; // This also implicitly closes the form.
 
Be careful when using the DialogResult property on buttons. They can create a cascading effect if you have multiple modal forms layered on top of each other.

To see what I mean:
Put a button on a form and set it's DialogResult to anything but None. In the button's click event, create a new instance of you form and show it modally.

Now click the button repeatedly to show multiple modal windows. Click the X to close the topmost form (you shouldn't even be able to click the other forms). You'll see that ALL of the modal windows close, one after another.

What this means is, if you have a SAVE or OK button that sets the dialog result, and inside the click event you determine that the user hasn't entered something and decide to NOT call Close, you must ALSO remember to manually set the DialogResult to None to prevent the form from closing - even if you never run this.Close() (or Me.Close in VB).

In my opinion, you might as well just set the DialogResult yourself in code (it defaults to Cancel so you don't have to worry about changing it in a Cancel button or if the user clicks the X).

-Ner
 
Back
Top