Link to home
Start Free TrialLog in
Avatar of VBBRett
VBBRett

asked on

What is wrong with my open file dialog statement? I need help with this when I press cancel.

I have run into a problem that seems fairly simple, but my program is not working the way I want it to.  I am opening a file dialog, and when I press cancel, the file dialog will open and again..I press cancel and then it shows the message box(You have pressed cancel).  Again I open the file dialog and when I select the file and press OK, the file dialog box opens yet again, I have to select the file again and then it displays the filepath in the text box and the message box accordingly.  How can I get this down to be only one pass at both the cancel button and the file selection with the OK button.  Please help asap.  I have the code below.

Private Sub btnFileFind1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFileFind1.Click

        Dim strFileName As String
        Dim DidWork As Integer


        'OpenFD.InitialDirectory = "C:\PRU401k"

        OpenFD.Title = "Open a text file"
        OpenFD.Filter = "Text Files(*.txt)|*******R.txt"
        OpenFD.ShowDialog()
        DidWork = OpenFD.ShowDialog()


        If DidWork = DialogResult.Cancel Then
            MsgBox("Cancel button clicked")
            Exit Sub
        Else
            strFileName = OpenFD.FileName
            MsgBox(strFileName)
            TextBox1.Text = strFileName
        End If



    End Sub
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi VBBRett,

>         OpenFD.ShowDialog()
>         DidWork = OpenFD.ShowDialog()

Only show it once:

DidWork = OpenFD.ShowDialog()

Get rid of the preceeding line!

Tim Cottee
Avatar of VBBRett
VBBRett

ASKER

I am not getting what you are saying?  How would I set up my if statement in order for this to be done right?  Sorry, I have been looking at this code since last week and it is driving me nuts.
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VBBRett

ASKER

Oh man!  It was staring me in the face all this time and I didn't even see it.  I feel so...NEVERMIND!  500 points for you.