Creating a form that retuns a value

  • Thread starter Thread starter Marco
  • Start date Start date
M

Marco

The subject pretty much says it all. I need to create a form like the
msgbox that returns a value, how would I go about doing this?
 
I am nto sure if there is a more standard way of doing this, but I would try
this approach...

Create your form, with all the functionality you need.

Add a public function to the form, that returns the value (or object) you
want.

Have the function contain the commands to show the form, and return whatever
value you need based on what the user does in the form.

After you return the value, close the form.
 
That's what I've been doing up until now but I was hopping that there was a
better way to do it. Oh well, stick with what works I guess. Thanks.
 
Don't give up yet. If there is a better way someone will post it soon.
 
Marco said:
That's what I've been doing up until now but I was hopping that there was a
better way to do it. Oh well, stick with what works I guess. Thanks.

Jim's solution isn't so bad :)

One nicety might be to make the method that's called a *Shared* method
(like MessageBox.Show is), so that the caller doesn't have to worry
about the details of instantiation.

this code is untried and might not even compile:

Class MyDialogBox
Inherits Form
'the form has a label control and a text box control on it
'and an ok button

Public Shared Function GetAnswer(byval Caption as string) as string
Dim box as new MyDialogBox()

box.Caption = Caption

Return box.Answer

'box will get tidied up automatically
End Function

Private _answer as string=""

Public Function Answer() As string
Me.ShowDialog

Return _answer
End Function

Public Property Caption as string
'(Code omitted: caption <-> label1.text)

private sub Textbox1_Changed('etc
_answer = Textbox1.Text
end sub

private sub Button1_Clicked('etc
Me.Hide
end sub
End Class

To use:

Dim TheAnswer As String = MyDialogBox.GetAnswer("What's the meaning of
life etc")
 
Making it shared is a good idea. One word of caution though...

If it is shared make absolutely certain that you are reinitializing any
variables or you will end up with the values from the last time the form was
accessed.

That said, a shared function will make things easier and is the way to go.
 
In addition to using a shared method or just having a method available,
you can actually set and use the DialogResult property of any form,
whether it's modal or not.

As long as you are ok with using the DialogResult enumeration as the
returns, then you can do this.

In your form, just set your DialogResult like so... Me.DialogResult =
DialogResult.OK
Then in your other piece that's accessing this form...
if f.DialogResult = ... then...

If you need a specific value, you can also just set any of the
variables as needed. And just your instance of that form to grab the
necessary property you want to access.

Derek Woo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top