Is there a way to pass values back from forms.

  • Thread starter Thread starter Stephen Costanzo
  • Start date Start date
S

Stephen Costanzo

I basically am attempting to create a form with two checkboxes and want a 1, 2, or 3 to come back depending on the user choice to the calling form (like a message box, basically)

I didn't know if there was a way to pass it back other than throwing an error (which isn't an error) or raising an event (which would set a value outside of the module procedure)

Thanks
 
Here's one way . . .

if you use showDialog method to show the form with the checkboxs on it, you can then continue with the code execution and extract the value selected on the form.

Dim frmCB as new formChkBx


'Puts the form in modal state so use has to take an action/ this should be to hide the form once the choise has been made
frmCB.ShowDialog()

'get Values from form
'You can use the form like any other object so you can call its properties and methods.

'Then dispose of the form using the dispose method.

Regards - OHM





I basically am attempting to create a form with two checkboxes and want a 1, 2, or 3 to come back depending on the user choice to the calling form (like a message box, basically)

I didn't know if there was a way to pass it back other than throwing an error (which isn't an error) or raising an event (which would set a value outside of the module procedure)

Thanks
 
Thanks,

That was going to be my next question - other than downloading 17K+ headers
how to search the archives, wasn't aware I could google it.
 
Referring to the code that you pointed out where:

Private Sub Button1_Click(...) ...
Dim f As New Form2()
If f.ShowDialog(Me) = DialogResult.OK Then
MsgBox(f.Date.ToString())
Else
MsgBox("Cancelled")
End If
End Sub

I have one point of confusion, on page 57 of Microsoft's Developing
Windows-based applications with Visual Basic .net (2nd ed) they indicate
that "you can call the Form.Close method to close the form and remove it
from memory. This method closes all resources contained within the form and
marks them for garbage collection. Once you have called form.close, you
cannot call form.show to make the form visible again because the resources
for the form are no longer available."

Therefore, in form 2 where:
Private Sub btnOK_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnOK.Click
Me.DialogResult = DialogResult.OK
Me.Close()
End Sub

the close method is called. The confusion I have is that if I were expecting
that the resources from that form are unavailable that I would not be able
to get the result that was shown in the example. Granted GC happens at
irregular intervals, so there is a time when it is available, but is the
reason that one can consistently expect the example code to work because
button1 controls the memory space for the form variable and it isn't until
button1's variables leave scope that those variables are actually marked for
GC and are not available?

Thanks
 
Stephen,

Setting a form's DialogResult property will close the form (that is, HIDE it
from view) and return program control to the caller. In the caller you can
then query the form's properties where you can retrieve the values of
form-level variables marked PUBLIC or FRIEND. Once you are done with that,
you can then dispose of the form by calling it's dispose method.

Jeff


Stephen Costanzo said:
Referring to the code that you pointed out where:

Private Sub Button1_Click(...) ...
Dim f As New Form2()
If f.ShowDialog(Me) = DialogResult.OK Then
MsgBox(f.Date.ToString())
Else
MsgBox("Cancelled")
End If
End Sub

I have one point of confusion, on page 57 of Microsoft's Developing
Windows-based applications with Visual Basic .net (2nd ed) they indicate
that "you can call the Form.Close method to close the form and remove it
from memory. This method closes all resources contained within the form and
marks them for garbage collection. Once you have called form.close, you
cannot call form.show to make the form visible again because the resources
for the form are no longer available."

Therefore, in form 2 where:
Private Sub btnOK_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnOK.Click
Me.DialogResult = DialogResult.OK
Me.Close()
End Sub

the close method is called. The confusion I have is that if I were expecting
that the resources from that form are unavailable that I would not be able
to get the result that was shown in the example. Granted GC happens at
irregular intervals, so there is a time when it is available, but is the
reason that one can consistently expect the example code to work because
button1 controls the memory space for the form variable and it isn't until
button1's variables leave scope that those variables are actually marked for
GC and are not available?

Thanks


want
a 1, 2, or 3 to come back depending on the user choice
 
Understood, I was just confused by the language from Microsoft as I
explicitly used the .close function in the button.

Jeff Ptak said:
Stephen,

Setting a form's DialogResult property will close the form (that is, HIDE it
from view) and return program control to the caller. In the caller you can
then query the form's properties where you can retrieve the values of
form-level variables marked PUBLIC or FRIEND. Once you are done with that,
you can then dispose of the form by calling it's dispose method.

Jeff
 
Hi Stephen,

Did the link in my last post answer your question?
If you have any conern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Stephen,

One of the interesting things about clr garbage collection is that
when the sub shown below ends the f instance of the Form2 class is
disposed of along with all the items instantiated in the sub. So,
even though it is good practice to clean up after yourself, it isn't
really necessary in this case.

In some large projects we have worked on, we can't see a noticable
difference in code performance or resource usage either way.
 
Back
Top