Link to home
Start Free TrialLog in
Avatar of Sheldon Livingston
Sheldon LivingstonFlag for United States of America

asked on

VB.NET and OpenFileDialog control.

Using VS2005 on a Windows 7 machine.
Form1 opens form2 that opens form3 that opens form4.  Form4 contains a button that invokes an OpenFileDialog control.
When invoked, the dialog box displays a "Parent" folders pane, a scroll bar, a "child" pane and another scroll bar.
If I move the mouse around and move it over a scroll bar the app will "crash" with a memory violation error that cannot be captured in a Try... Catch.
If I put a OpenFileDialog control on form1 it works fine.
Any thoughts?  More info needed?
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

do you use ShowDialog to open each form?can you post a screenshot of ther 4 forms with the file dialog dispalyed?
instead of adding a OpenFileDialog control to your form, declare one by code when you need one as shown in http://www.emoreau.com/Entries/Articles/2003/06/Common-Dialogs.aspx 
Avatar of Sheldon Livingston

ASKER

emoreau:  Same issue results.

Error.JPG
Can you open form like class and object.

Example

form2 form should have class like form2
on form1 create a object of form2 and using that object open form2. same with other. I hope it will work for you.

Please let me know if you are unable to understand my answer.
If you comment out the line where you set the initial directory does it still error out?
 
Is this a duplicate question?
It seems like this issue (even same form names) was marked as resolved by you by adding an openfiledialog control on the first form?
https://www.experts-exchange.com/questions/26321832/OpenFileDialog-error.html?anchorAnswerId=33305660#a33305660 
I don't get that error even with 4 forms where form1 calls form2 calls form3 calls form4 and opens the openfiledialog object just fine.
However I found this issue being related to a damaged component of Microsoft .NET Framework 2.0 or 3.5 after a windows update.
Close your project, do a repair on your .net framework (whichever you are using) or a re-install and the error should go away.
13598:  Commenting out the error does not help.  I had previously posted this same question... don't recall accepting an answer... in fact I think I was just explaining that it works if put onto the first form.

I'm going to try a .net repair.
Ok... I look in add/remove programs and see no references to .NET framework except for a Compact 2.0 version.
I have no idea what is installed or how to go about repairing it.
I tried to download and install 3.0 and received a "turn wndows features on or off" message.
Try repairing the compact 2.0 version.
Do you also see a Microsoft .Net framework 2.0 Service Pack2?
If you don't you should install it. You need to have SP2 since it fixed a lot of issues.
Download from here after you repair 2.0 =>
http://www.microsoft.com/downloads/details.aspx?familyid=5b2c0358-915b-4eb5-9b1d-10e506da9d0f&displaylang=en
 
classnet, your problem is not really related to installation or something. Its related to code that you are doing. You are going into wrong direction. Its possible that for a little bit of time re-installation/repair will solve your problem, but it will not be a permanent solution.
VjSoft:  What code though?  I slap an OpenFileDialog box on a form and it crashes when the mouse moves over a scroll bar?
have you tried it?

open form like class and object.

Example

form2 form should have class like form2
on form1 create a object of form2 and using that object open form2. same with other. I hope it will work for you.

Please let me know if you are unable to understand my answer.
ASKER CERTIFIED SOLUTION
Avatar of Sheldon Livingston
Sheldon Livingston
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
On windows 7 (32 bit) had same problem when showing the openfiledialog from a modal form loaded for the second time with data from db in it. Solution was: do not allow autoupgrade of dialog.
Dim sfv As New System.Windows.Forms.SaveFileDialog
   With sfv
     .AutoUpgradeEnabled = False
     [...]

Cimperiali
On windows 7 (32 bit) had same problem when showing the SaveFileDialog from  a dot net windows form application (visual studio 2010, framework 4)  with data from access db (access 2010) in it. Solution appeared to be (but was not) : do not allow autoupgrade of dialog.
Dim sfv As New System.Windows.Forms.SaveFileDialog
   With sfv
     .AutoUpgradeEnabled = False
     [...]


But error came up again. Then I noticed it was apparently randomic till I realized it did not come out if I was able to show a saveFileDialog or an OpenfileDialog before loading any data from db.

Thus true workaround is: before load anything on the form you're going to show, display a dialog asking user to choose a path and file you *might* need after (arrrg!). After that, load data. Now your can let users, if needed, to choose path and file with dialog again...

ie:
  Private Sub frmReport_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     txtFilePathName.Text = "Export_" & Now.ToString("yyyy_MM_dd_HH_mm_ss", CultureInfo.GetCultureInfo("it-It")) & ".csv"
     txtFilePathName.Text = GetSaveFileName(txtFilePathName.Text, ".csv", "Choose a csv File to save exported data", "csv |*.csv|All |*.*")
     'now load data in forms, where you can also have a button to call again the GetSaveFileName
[...]

Private Function GetSaveFileName(ByVal fileName As String,
                                    ByVal defaultExtension As String,
                                   ByVal title As String,
                                   ByVal filter As String) As String
        Dim sfv As New System.Windows.Forms.SaveFileDialog
        With sfv
            .RestoreDirectory = True
            .AddExtension = True
            .DefaultExt = defaultExtension
            .FileName = fileName
            .Title = title
            .Filter = filter
            .CheckPathExists = True
            .OverwritePrompt = True
            .ShowHelp = False

            If (.ShowDialog = DialogResult.OK) Then
                fileName = .FileName
            End If
        End With
        Return fileName
    End Function

Cimperiali
The path was the issue...