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

asked on

OpenFileDialog error

I have frmMain that opens frmPicture via a

dim frmPicture as New frmPicture
frmPicture.show()

button click.

On frmPicture I have a button that attempts to open a dialog box via
Me.OpenFileDialog.ShowDialog()
and I get a memory error shown below:

System.AccessViolationException was unhandled
  Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
  Source="System.Windows.Forms"
  StackTrace:
       at System.Windows.Forms.FileDialogNative.IFileDialog.Show(IntPtr parent)
       at System.Windows.Forms.FileDialog.RunDialogVista(IntPtr hWndOwner)
       at System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner)
       at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
       at System.Windows.Forms.CommonDialog.ShowDialog()
       at safetyLog.frmPicture.cmdSelect_Click(Object sender, EventArgs e) in C:\Users\VSL\Desktop\TroyerProject\safetyLog\safetyLog\frmPicture.vb:line 15
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at safetyLog.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Any ideas on what is going on?  If I put an OpenFileDialog control and test button on frmMain and use the control it works fine.
Avatar of dschauhan82
dschauhan82

'Declaration

Public Function ShowDialog As Nullable(Of Boolean)
'Usage

Dim instance As OpenFileDialog
Dim returnValue As Nullable(Of Boolean)

returnValue = instance.ShowDialog()
Hope this help
Try performing clean in the solution then rebuild it.

Avatar of Sheldon Livingston

ASKER

Still no joy on this problem.
Sometimes running the code will show the dialog box for a few seconds and then error out.  If I compile and run it on another machine there isn't an error.
Essentially form A opens form B that opens form C.  

Using the openFileDialog control from C causes an error.
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. At first, 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