VB.net Prompt user using Windows form

Murray Brown
Murray Brown used Ask the Experts™

I have a Windows Form that I use to prompt the user. It is loaded with a list that the user
has to make a selection from.
I have an OK button that then runs the rest of the code.
Is there a way to pause the code, show the form and then run the rest of the code
without havening to channel it through the OK button?

In essence, I want the Windows form to gather feedback just like the OpenFileDialog would in the
following code and gather feedback from the user
                      With Globals.ThisAddIn.oAUTOMATOR.OpenFileDialog1
                            .FileName = ""
                            .InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                            .Filter = Nothing

                            sFileAndPath = .FileName
                            sPath = System.IO.Path.GetDirectoryName(sFileAndPath)
                            sFile = System.IO.Path.GetFileName(sFileAndPath)
                            oSource_File = sFileAndPath
                        End With
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Éric MoreauSenior .Net Consultant
Top Expert 2016

You need to replace .ShowDialog() with .Show() but ...

That will cause another problem: the line that follows check for .FileName which will be empty at best.

That means that you cannot have code after .Show that is dependant on the result of the dialog box.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Actually, I think he's saying he put the code to continue with...literally in the "OK" button handler.

If that is the case, then what you need actually is ShowDialog().  You'd display the form the gets user input with ShowDialog():
' .... some code ....
Dim dlg As New frmDialog
If dlg.ShowDialog() = DialogResult.Ok Then ' code STOPS here until "dlg" is dismissed by the user
    ' ... some continuation code ...
    ' Grab something from "dlg":
    ' xxx = dlg.yyyy
End If

Open in new window

And in frmDialog, you set DialogResult to Ok (it is set to Cancel if the user clicks the "X" in the upper right):
Public Sub btnOK_Click(...) Handles btnOK.Click
    If conditionsAreRight Then
        Me.DialogResult = DialogResult.Ok
        MessageBox.Show("Please select blah, blah first!)
    End If
End Sub

Open in new window

Murray BrownASP.net/VSTO Developer


Thanks very much. That is exactly what I was looking for. The question was probably not that clear.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial