Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Gamer extraordinaire. Programmer wannabe.
Guest mutant
Posted
Couldnt you just set a value for some variable during form closing event and then read the variable?
  • *Experts*
Posted

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


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:

 

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:

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

Posted

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.

 

// 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.

Gamer extraordinaire. Programmer wannabe.
  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...