Solved

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

Posted on 2006-06-19
4
296 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:VBBRett
  • 2
  • 2
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
Hi VBBRett,

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

Only show it once:

DidWork = OpenFD.ShowDialog()

Get rid of the preceeding line!

Tim Cottee
0
 

Author Comment

by:VBBRett
Comment Utility
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.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
Comment Utility
VBBRett,

What I am saying is this, you open the file dialog with OpenFD.ShowDialog() then on the next line, the DidWork = OpenFD.ShowDialog() will open it again and return the result. So the first one is redundant. Your code should be:
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"
        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

Tim
0
 

Author Comment

by:VBBRett
Comment Utility
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.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now