VB.net Prompt user using Windows form


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
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
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.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much. That is exactly what I was looking for. The question was probably not that clear.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.