NeuralJack Posted December 20, 2006 Posted December 20, 2006 I have a small form that the user types options into and I want those options to go back to the procedure that called the form. There are more than one values i'd like to return to the calling procedure. My strategy is to create variables in the calling procedure and pass those variables ByRef into the small options form. I'll open the small form with .ShowDialog and then the answers will be set in the form and the variables updated. When the user presses OK , it will simply shut down the small form and the calling procedure should be able to continue.. i think. Is there a better way to do this? Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
NeuralJack Posted December 20, 2006 Author Posted December 20, 2006 Ok, that ByRef stuff didnt work for integers and bool. Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
NeuralJack Posted December 21, 2006 Author Posted December 21, 2006 Ok , I found a good article how to do this with properties. It looks like the properties can be accessed in a form even after it's closed. I guess it wouldnt get destroyed until you either do it manuall or the small forms scope runs out http://www.informit.com/articles/article.asp?p=102148&seqNum=7&rl=1 Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Leaders snarfblam Posted December 21, 2006 Leaders Posted December 21, 2006 If you add properties to the form, as long as they don't depend on inherited properties/functions, they will always work, even if the form is closed and disposed. So, yes, if you add int and bool properties and set them before/when the form is closed (backed by a field, as opposed of reading values from a textbox or something like that) it will work. And this may be your best approach. Another option might be to create a new event and a new EventArgs class. When the form is closed, you could put the relevant data in the EventArgs object and raise the event. Quote [sIGPIC]e[/sIGPIC]
NeuralJack Posted December 21, 2006 Author Posted December 21, 2006 Thanks Marble Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
MrPaul Posted December 21, 2006 Posted December 21, 2006 ByRef Ok, that ByRef stuff didnt work for integers and bool. Really? Public Sub ReturnsManyValues(ByRef anInt As Integer, ByRef aBool As Boolean) anInt = 789 aBool = True End Sub Public Sub Test() Dim myInt As Integer = 0 Dim myBool As Boolean = False ReturnsManyValues(myInt, myBool) Console.WriteLine("myInt={0}, myBool={1}", myInt, myBool) End Sub 'Output: 'myInt=789, myBool=True If you're always going to use the form for similar tasks, I suggest sticking to the approach you attempted in the first post, so that your data entry form can be used just like MessageBox etc. Basically the form exposes one method which performs the task of showing the form modally, waiting for user input, then setting the values of the return variables. If, however, the form is more general purpose, or returns a lot of information, the properties approach might be better suited. :cool: Quote Never trouble another for what you can do for yourself.
NeuralJack Posted December 21, 2006 Author Posted December 21, 2006 MrPaul , with just what you have written of course that'd work - it doesnt show usage of anohter form. But maybe that's assumed. When I tried it with ByRef here's what I did. - Created Public variables in myInt and myBool in mySmallForm - The New() routine in mySmallForm assigned an int to myInt ByRef and a bool to myBool ByRef - I created an int and a bool variable in myLargeForm in the procedure that will call mySmallForm - I created mySmallForm from myBigForm passing the newly created int/bool variables. Ideally this was a ByRef pass. - I changed the value of those variables from mySmallForm - I exited mySmallForm - the calling procedure continued in myLargeForm - I debug.wrote the values in the int and bool var from the callign procedure The int and bool var didnt change. I may have overlooked something though. I was working fast. The prop thing is so far working sweet, and I do have many properties to set so it sounds the best way to go. I'd like to know if that byref pass should work though for future use. Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
MrPaul Posted December 21, 2006 Posted December 21, 2006 (edited) Form static method Well the idea is that the instantiation and showing of the form is contained within a static method which takes the ByRef parameters - within the form itself: 'In MySmallForm Public Shared Function ShowInputForm(ByRef intVal As Integer, ByRef boolVal As Boolean) As Boolean Dim frm As New MySmallForm() 'Show the form frm.ShowDialog() 'Set the values intVal = frm.myTrackBar.Value boolVal = frm.myCheckBox.Checked 'Return boolean indicating whether it was OK or cancel (example) 'Could use DialogResult here Return True End Function 'In MyLargeForm Dim myInt As Integer Dim myBool As Boolean If MySmallForm.ShowInputForm(myInt, myBool) Then 'Do something with myInt and myBool End If :cool: Edited December 21, 2006 by MrPaul Quote Never trouble another for what you can do for yourself.
betrl8thanever Posted December 31, 2006 Posted December 31, 2006 You can also create your own "Dialog Results" and overload the ShowDialog method of the form to return your results. http://www.edneeis.com/%5Ctutorial.aspx?ID=5 Quote "In the immortal words of Socrates, who said "' I drank what?!'"
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.