?
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
Medium Priority
?
302 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

764 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