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
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 16935009
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
ID: 16935025
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
ID: 16935035
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
ID: 16935068
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

762 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