wyrd Posted May 10, 2003 Posted May 10, 2003 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. Quote Gamer extraordinaire. Programmer wannabe.
Guest mutant Posted May 10, 2003 Posted May 10, 2003 Couldnt you just set a value for some variable during form closing event and then read the variable? Quote
*Experts* jfackler Posted May 10, 2003 *Experts* Posted May 10, 2003 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 Quote
wyrd Posted May 10, 2003 Author Posted May 10, 2003 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. Quote Gamer extraordinaire. Programmer wannabe.
hog Posted May 10, 2003 Posted May 10, 2003 Mmm that is interesting, I didn't know that either! Quote My website
Administrators PlausiblyDamp Posted May 10, 2003 Administrators Posted May 10, 2003 btw The button also has a DialogResult property that can be set through the designer to acheive the same result. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* Nerseus Posted May 12, 2003 *Experts* Posted May 12, 2003 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 Quote "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
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.