Sheldon Livingston
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.ShowDial og()
and I get a memory error shown below:
System.AccessViolationExce ption was unhandled
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Source="System.Windows.For ms"
StackTrace:
at System.Windows.Forms.FileD ialogNativ e.IFileDia log.Show(I ntPtr parent)
at System.Windows.Forms.FileD ialog.RunD ialogVista (IntPtr hWndOwner)
at System.Windows.Forms.FileD ialog.RunD ialog(IntP tr hWndOwner)
at System.Windows.Forms.Commo nDialog.Sh owDialog(I Win32Windo w owner)
at System.Windows.Forms.Commo nDialog.Sh owDialog()
at safetyLog.frmPicture.cmdSe lect_Click (Object sender, EventArgs e) in C:\Users\VSL\Desktop\Troye rProject\s afetyLog\s afetyLog\f rmPicture. vb:line 15
at System.Windows.Forms.Contr ol.OnClick (EventArgs e)
at System.Windows.Forms.Butto n.OnClick( EventArgs e)
at System.Windows.Forms.Butto n.OnMouseU p(MouseEve ntArgs mevent)
at System.Windows.Forms.Contr ol.WmMouse Up(Message & m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Butto nBase.WndP roc(Messag e& m)
at System.Windows.Forms.Butto n.WndProc( Message& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.De buggableCa llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Unsaf eNativeMet hods.Dispa tchMessage W(MSG& msg)
at System.Windows.Forms.Appli cation.Com ponentMana ger.System .Windows.F orms.Unsaf eNativeMet hods.IMsoC omponentMa nager.FPus hMessageLo op(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoopInner (Int32 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoop(Int3 2 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Run (Applicati onContext context)
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.OnRun( )
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.DoAppl icationMod el()
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.Run(St ring[] commandLine)
at safetyLog.My.MyApplication .Main(Stri ng[] Args) in 17d14f5c-a337-4978-8281-53 493378c107 1.vb:line 81
at System.AppDomain._nExecute Assembly(A ssembly assembly, String[] args)
at System.AppDomain.ExecuteAs sembly(Str ing assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.Hos tingProces s.HostProc .RunUsersA ssembly()
at System.Threading.ThreadHel per.Thread Start_Cont ext(Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
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.
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.ShowDial
and I get a memory error shown below:
System.AccessViolationExce
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Source="System.Windows.For
StackTrace:
at System.Windows.Forms.FileD
at System.Windows.Forms.FileD
at System.Windows.Forms.FileD
at System.Windows.Forms.Commo
at System.Windows.Forms.Commo
at safetyLog.frmPicture.cmdSe
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at Microsoft.VisualBasic.Appl
at Microsoft.VisualBasic.Appl
at Microsoft.VisualBasic.Appl
at safetyLog.My.MyApplication
at System.AppDomain._nExecute
at System.AppDomain.ExecuteAs
at Microsoft.VisualStudio.Hos
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Threading.ThreadHel
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.
Try performing clean in the solution then rebuild it.
ASKER
Still no joy on this problem.
ASKER
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.
ASKER
Essentially form A opens form B that opens form C.
Using the openFileDialog control from C causes an error.
Using the openFileDialog control from C causes an error.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.SaveF ileDialog
With sfv
.AutoUpgradeEnabled = False
[...]
Cimperiali
Dim sfv As New System.Windows.Forms.SaveF
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.SaveF ileDialog
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_H H_mm_ss", CultureInfo.GetCultureInfo ("it-It")) & ".csv"
txtFilePathName.Text = GetSaveFileName(txtFilePat hName.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.SaveF ileDialog
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
Dim sfv As New System.Windows.Forms.SaveF
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_H
txtFilePathName.Text = GetSaveFileName(txtFilePat
'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.SaveF
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
Public Function ShowDialog As Nullable(Of Boolean)
'Usage
Dim instance As OpenFileDialog
Dim returnValue As Nullable(Of Boolean)
returnValue = instance.ShowDialog()
Hope this help