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
297 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Procedure or function has too many arguments specified 9 41
Help with WinAPI Signatures in VB.NET for SetWindowSubclass 2 34
vb.net 2 37
Variable Event ? 3 17
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

947 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

23 Experts available now in Live!

Get 1:1 Help Now