Results 1 to 16 of 16

Thread: [RESOLVED] openfiledialog question

  1. #1

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Resolved [RESOLVED] openfiledialog question

    ' Display an OpenFileDialog so the user can select a Cursor.
    Dim openFileDialog1 As New OpenFileDialog()

    openFileDialog1.Filter = "VMware Configuration files (*.txt)|*.txt|All files (*.*)|*.*"
    openFileDialog1.Title = "Selecteer een bestand"

    ' Show the Dialog.
    ' If the user clicked OK in the dialog and
    ' a .XML file was selected, open it.

    If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then


    I gote a problem whit this code
    you can open a txt file but when
    I click an the button cancel he gives error
    Ik know that with this it to do has


    If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    I can just open it but i wanne cancel to how do i do that

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: openfiledialog question

    There shouldn't be an error really. At least not from what you have shown. I guess there's code that tries to execute outside of the Open file dialog but it's hard to say without seeing it. You could try this:
    vb Code:
    1. If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
    2.  
    3.       'You have code here
    4.  
    5. ElseIf OFD.ShowDialog = Windows.Forms.DialogResult.Cancel Then
    6.  
    7.        Exit Sub  'Don't execute any code that follows this
    8.  
    9. End If
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    Addicted Member NeedHelp01's Avatar
    Join Date
    May 2007
    Location
    holland
    Posts
    142

    Re: openfiledialog question

    thanx it's solved

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: openfiledialog question

    That will show the dialog twice though.

    Should be


    Code:
    If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
        'Ok clicked
    Else
        'Canceled
    End If
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Member
    Join Date
    Feb 2008
    Posts
    57

    Re: [RESOLVED] openfiledialog question

    Guys help, I can' t understund
    I used the above code, I used three different codes, none works.

    Here is another example

    Select Case MessageBox.Show(MessageBoxButtons.OKCancel)
    Case MsgBoxResult.Yes
    'do yes stuff here
    MessageBox.Show("OK")
    Case MsgBoxResult.Cancel
    'do cancel stuff here
    MessageBox.Show("Cancel")
    End Select

    I press "Open" or "Cancel", and the dialogbox shows itself again and again, for 10-15 times.
    It executes Messagebox, and the same again. After this it stops.
    Some times (with Select-Case), it gives me MessageBox "1".
    It doesn't make sence.
    I traced with breakpoint, but no luck. It goes round and round.

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [RESOLVED] openfiledialog question

    Four things:

    1) You have Option Strict Off if that code compiles, so I would suggest turning it on.
    2) The first parameter of the Show method is a string containing the text you want to show, the second parameter is the title of the message box, the third parameter is where the "MessageBoxButtons.OKCancel" should go.
    3) The show method returns a DialogResult, not a MsgBoxResult
    4) You didn't mention where this code even is.

    Try this to show your message box:

    vb.net Code:
    1. Select Case MessageBox.Show("text", _
    2.                             "caption", _
    3.                             MessageBoxButtons.OKCancel)
    4.     Case Windows.Forms.DialogResult.OK
    5.         'Run code for acceptance
    6.     Case Windows.Forms.DialogResult.Cancel
    7.         'Run code for cancel
    8. End Select

    The reason you are getting a message box of "1" is because (as mentioned earlier) you have Option Strict Off so there is an implicit conversion to string going on in your MessageBox.Show. The Show method is expecting a string where you put MessageBoxButtons.OKCancel so it automatically converts that to it's string representation which is "1".

  7. #7
    Member
    Join Date
    Feb 2008
    Posts
    57

    Re: [RESOLVED] openfiledialog question

    Now it comes up only twice, and then it gives me an error.
    Code:
     Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click, cmdLoad.Click
            Dim ofd As New OpenFileDialog
            Dim FileName As String
            Dim sr As StreamReader
    
            ofd.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
            ofd.Filter = "Excel Files|*.xls|Excel 2007 Files|*.xlsx"
            ofd.DefaultExt = "*.xls"
            ofd.ShowDialog()
    
            Select Case MessageBox.Show("text", _
                                       "MMMMMMmmmmmm", _
                                      MessageBoxButtons.OKCancel)
    
                Case Windows.Forms.DialogResult.OK
                    'Run code for acceptance
                    Try
                        MyConn.Open()
                        MyComm = New OleDbCommand("select * from [TZOKER$]", MyConn)
                        dtAdapter = New OleDbDataAdapter(MyComm)
                        dtAdapter.Fill(dtSet, "TZOKER")
                        For Each dtRow As DataRow In dtSet.Tables(0).Rows
                            'sum = dtRow("N1") + dtRow("N2") + dtRow("N3") + dtRow("N4") + dtRow("N5")
                            'code to do something with the 'sum' value
                        Next
                        dgViewSample.DataSource = dtSet.Tables(0)
                        MyConn.Close()
    
                    Catch ex As Exception
                        MessageBox.Show(ex.Message)
                        MyConn.Close()
                    Finally
                        MyConn.Close()
                        MyConn = Nothing
                        MyComm = Nothing
                        dtAdapter = Nothing
                        dtSet = Nothing
                    End Try
                    Exit Select
                Case Windows.Forms.DialogResult.Cancel
                    'Run code for cancel
                    Exit Select
            End Select
    
        End Sub

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] openfiledialog question

    Take the close out of the catch block as it also runs in the finally block.

    Code:
    MyConn.Close()
    What comes up twice? The dialog form?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Member
    Join Date
    Feb 2008
    Posts
    57

    Re: [RESOLVED] openfiledialog question

    Quote Originally Posted by RobDog888 View Post

    What comes up twice? The dialog form?[/color]
    Yes.

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] openfiledialog question

    Hey,

    Rather than explicitly call the Close method on the connection, you might think about wrapping the Connection and Command objects in Using statements, that way they will automatically closed before they go out of scope.

    On another note, are you meaning to call the OpenFileDialog, and the straight away call a MessageBox, that seems quite unintuitive to me. Are you not actually wanting to call the OpenFileDialog then, based on whether the user hits Ok or Cancel perform some action?

    What you are doing just now is, display the OpenFileDialog, then, regardless of what this click on there, you are then opening a MessageBox, and then doing an action based on what they press.

    Is that actually what you want?

    Gary

  11. #11
    Member
    Join Date
    Feb 2008
    Posts
    57

    Re: [RESOLVED] openfiledialog question

    Hello,
    I want to open the openfiledialog, choose an excel file, and open it.
    I have done several tries with different codes, but when I choose my file,
    (sometimes the data loads to datagridview, sometimes not) the openfiledialog opens again.
    With the code I posted, the openfiledialog does it only two times. With other codes I just stopped counting.
    I don't want the messege box, with "OK" or "Cancel".
    I just want to understand what is happenning.
    If I figure out I will do it other way

    Edit. For some reason I cannot leave " Private Sub cmdLoad_Click", if it don't executes the hole code twice.
    I placed an "Exit Sub", but....
    Code:
                   Finally
                        MyConn.Close()
                        MyConn = Nothing
                        MyComm = Nothing
                        dtAdapter = Nothing
                        dtSet = Nothing
                    End Try
                    Exit Select
                Case Windows.Forms.DialogResult.Cancel
                    'Run code for cancel
                    Exit Select
                    Exit Sub
            End Select
            Exit Sub
    
        End Sub
    Last edited by manin; Sep 14th, 2009 at 12:39 PM.

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] openfiledialog question

    Hey,

    Let's see if we can break this down slightly for you.

    This line:

    Code:
    ofd.ShowDialog()
    Causes an OpenFileDialog to appear. When this window appears, the end user has two options.

    1) Select a file to open and then hit OK, which will return a DialogResult of OK.
    2) Hits the Cancel button, which will return a DialogResult of Cancel.

    Now this line:

    Code:
    Select Case MessageBox.Show("text", _
                                       "MMMMMMmmmmmm", _
                                      MessageBoxButtons.OKCancel)
    Will cause a MessageBox to appear, where the user again, has the option to hit OK and Cancel, which would return a value of DialogResult.OK or DialogResult.Cancel respectively.

    You are currently switching your logic based on the result of the MessageBox.Show line, which in my opinion, based on what you have described, is not what you want to do.

    Instead, you want to switch on the result of the ShowDialog line. Something like:

    Code:
    Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click, cmdLoad.Click
            Dim ofd As New OpenFileDialog
            Dim FileName As String
            Dim sr As StreamReader
    
            ofd.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
            ofd.Filter = "Excel Files|*.xls|Excel 2007 Files|*.xlsx"
            ofd.DefaultExt = "*.xls"
    
            Select Case ofd.ShowDialog()
    
                Case Windows.Forms.DialogResult.OK
                    'Run code for acceptance
                    Try
                        MyConn.Open()
                        MyComm = New OleDbCommand("select * from [TZOKER$]", MyConn)
                        dtAdapter = New OleDbDataAdapter(MyComm)
                        dtAdapter.Fill(dtSet, "TZOKER")
                        For Each dtRow As DataRow In dtSet.Tables(0).Rows
                            'sum = dtRow("N1") + dtRow("N2") + dtRow("N3") + dtRow("N4") + dtRow("N5")
                            'code to do something with the 'sum' value
                        Next
                        dgViewSample.DataSource = dtSet.Tables(0)
    
                    Catch ex As Exception
                        MessageBox.Show(ex.Message)
                    Finally
                        MyConn.Close()
                        MyConn = Nothing
                        MyComm = Nothing
                        dtAdapter = Nothing
                        dtSet = Nothing
                    End Try
                    Exit Select
                Case Windows.Forms.DialogResult.Cancel
                    'Run code for cancel
                    Exit Select
            End Select
    
        End Sub
    However, what is confusing me, is that you don't seem to use the excel file anywhere in the rest of the code that you have pasted. So where is your code to look at the excel spreadsheet? Have you hardcoded the link to the spreadsheet in your connection string?

    Gary

  13. #13
    Member
    Join Date
    Feb 2008
    Posts
    57

    Re: [RESOLVED] openfiledialog question

    TZOKER is the sheet of my excel file.
    The data of the file, are loaded corectly to the datagridview.
    Unfortunatly after the data are previewed to the datagridview, the openfiledialog open appears again.
    This is what I can't understand. The messagebox, is another try to avoid the multiple appering of datagridview.

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] openfiledialog question

    Hey,

    Search your code for ShowDialog.

    I can't see any reason, based on the code you have shown why it would be showing twice.

    Gary

  15. #15
    Member
    Join Date
    Feb 2008
    Posts
    57

    Re: [RESOLVED] openfiledialog question

    Neither do I.
    I just deleted and write it again. I was at the begining, so it was easy.
    Now it's all ok.
    Thanks

  16. #16
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] openfiledialog question

    Glad to hear it.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width