have a form return a value when it closes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi, im attempting to create an intellisense type form and have hit a
roadblock. I need the form to return (like the ShowDialog method returns the
dialog result) the value that was selected when the form closes. however, i
cannot just create a method that shadows ShowDialog and return it that way
because the form needs to close when the user clicks elsewhere on the screen,
or Alt-Tabs away from the window. i already have the code to close the window
when the user clicks or aalt-tabs away, but how can i have the form return a
result when this occurs? thanks
 
You can create a public property on the form and then after the return from
ShowDialog get the value. When the form returns the class object (which is
the form) is still available and you can read the value from the property.
I do this lots.

LS
 
You can invoke a function on the form that returns a value, and in the
function do a me.showdialog(). Like this:

'to invoke this class, do this: X = SelectDatabase.PromptUser
' X will be the returned ConnectionString.
'Return empty string if user clicks Cancel.
Public Function PromptUser() As String
Me.ShowDialog()
If (Me.DialogResult = Windows.Forms.DialogResult.OK) Then
My.Settings.DBConnString = CreateConnectionString("Y")
My.Settings.DatabaseName = DatabaseComboBox.Text
My.Settings.ServerName = ServerTextBox.Text
My.Settings.Save()
Return DatabaseComboBox.Text
Else
Return String.Empty
End If
End Function

Robin S.
 
i realize i can do those things, the issue is that if the user clicks away or
alt-tabs away from the window, ShowDialog does not let the window close. i
want the window to close if the user clicks away or alt-tabs away, so i
cannot use ShowDialog.....i pretty much need to make an exact replica of
Intellisense
 
I thought you said (in your first post) that you solved the problem of
closing the form when the user clicks away. Are you now saying you
didn't solve that? I'd probably try putting a me.close() in the
form's LostFocus event. And I may need to show the form modeless with
a Show() method rather than modally with a ShowDialog() method (if it
beeps at you when you try to click somewhere else).

I still think Lloyd's solution should work for you. Not sure why you
don't.
Regards,
ImageAnalyst
 
If you want click away functionallity then perhaps you can use the context
menu. It can be shown thru code as well as auto and as a control's context
menu.

Then when you click away the dialog (context menu) will disappear.

LS
 
I thought you said (in your first post) that you solved the problem of
closing the form when the user clicks away. Are you now saying you
didn't solve that? I'd probably try putting a me.close() in the
form's LostFocus event. And I may need to show the form modeless with
a Show() method rather than modally with a ShowDialog() method (if it
beeps at you when you try to click somewhere else).

If he uses the Show method, as soon as the form closes, it will be
disposed and if he then tries to access a custom property he will
likely get an exception.
 
Chris Dunaway said:
If he uses the Show method, as soon as the form closes, it will be
disposed and if he then tries to access a custom property he will
likely get an exception.
He could put a property in his first form, and set it from the second form
in the form_closing event.

Robin S.
 
Back
Top