Return Values from Forms [vb.net]

NeuralJack

Centurion
Joined
Jul 28, 2005
Messages
138
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?
 
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.
 
ByRef

Ok, that ByRef stuff didnt work for integers and bool.

Really?

Visual Basic:
    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:
 
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.
 
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:

Visual Basic:
    '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

Visual Basic:
    '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:
 
Last edited:
Back
Top