Answered by:
Error showing form for the second time.

Question
-
We are upgrading our application from VB 6.0 to VB.Net 2008.
Among many forms we have simple form that contains a label, Far Point spread control and two buttons OK and Cancel.
We use this form as a dialog to get user selection, and in VB 6.0 we show this form as modal. It works great in VB 6.0.
In VB.Net we use Me.ShowDialog() to show this form.
It works fine when we show this form for the first time.
The same call performed for the second time ends with the following error:
Error number -2147467261, description: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
This error message appears while statement Me.ShowDialog() executing, and for brief moment part of the form appears on the screen.
Here is how we invoke this form from the other form:
Dim rc As Short
rc = dlgDocOption.sSetDocSections(sDocId)
Inside of the form dlgDocOption we have initialization of the controls and finally display of the form with Me.ShowDialog().
Dialog is dismissed when one of the buttons is selected by the user and Me.Close() statement is executed.
We try many different modifications of the code in calling procedure and in problematic form, but every time end up with error during second call.
Please help.
AMTFriday, August 26, 2011 11:05 PM
Answers
-
as said Faraz, Me.ShowDialog has no sense.
whatever you need the dialog, you instantiate the dialog class with
Dim dlg as new dlgDocOption
and show the instance withDim dr as DialogResult = dlg.ShowDialog
when execution flow cames back here, you verify the resultIf dr = dialogresultok then rc = dlg.sSetDocSections(sDocId) End If
Speaking about other things, don't waste your time using Short variable instead of Integer:
this doesn't gain anything in memory or in performance.
the names of the class and of the property should start with capital letters (for example, DocOptionDlg and DocSections)
and you need no longer to use type prefix (as 's' for string)
please, mark this as answer if it is THE answer
----------------
Diego Cattaruzza
Microsoft MVP - Visual Basic: Development
blog: http://community.visual-basic.it/Diego
web site: http://www.visual-basic.it- Marked as answer by Liliane Teng Thursday, September 1, 2011 7:24 AM
Saturday, August 27, 2011 7:50 AM -
Your description is a little confusing for me,
Showing a form as dialog form from another form
Form1.ShowDialog()
Please tell in which event you use for
Me.ShowDialog()
Please mark those posts as answer which answers your question. Faraz- Marked as answer by Liliane Teng Thursday, September 1, 2011 7:24 AM
Saturday, August 27, 2011 6:22 AM -
Dear All,
We have dialog form named dlgDocOption.
Here is how we invoke this form from the other form:
Dim rc As Short
rc = dlgDocOption.sSetDocSections(sDocId)
Inside of the dlgDocOption form we have a function sSetDocSections()
where we perform initialization of the controls and finally display the form with
Me.ShowDialog().
At this moment execution of the function sSetDocSections() pauses and
dlgDocOption form is shown as modal, allowing user to make his/her
options selection. Two buttons (OK and Cancel) are used to dismiss the dialog
with Me.Hide() method. Besides that if OK button is clicked options selected
by the user in Far Point spread control are stored in persistent storage to be
used shortly after that in document generation.
We try already to invoke this form in a different way, similar to what you have proposed, like this:
Dim dlgDocumentOption As dlgDocOptions
dlgDocumentOption = New dlgDocOptions
rc = dlgDocumentOption.sSetDocSections(sDocId)
...
dlgDocumentOption.Close()
dlgDocumentOption.Dispose()
dlgDocumentOption = Nothing
Unfortunately we get the same result (error during second call).
In our VB 6.0 project rc variables (stands for return code) were declared As Integer. During upgrade to VB.Net their type has been converted to Short. We have Option Strict Off in all the modules therefore it is OK to declare rc As Short because implicit conversion will take place. In fact during the first run it works just fine. Just to try I have changed return value type of the sSetDocSections()function to DialogResult, but outcome did not change.
Then I accidentally removed all the following cleanup statements
dlgDocumentOption.Close()
dlgDocumentOption.Dispose()
dlgDocumentOption = Nothing
without them I can run dialog as many times as I want with no errors!
I have narrowed problem to two statements that cause it:
dlgDocumentOption.Close()
dlgDocumentOption.Dispose()
If present, any of the statement, or both of them cause the error during second run!
I have commented them out and now code works like charm.
I would like to thank you all for your replies, they were really helpful.
Have a nice day.
AMT
- Edited by A M T Monday, August 29, 2011 9:22 PM Spelling
- Marked as answer by Liliane Teng Thursday, September 1, 2011 7:23 AM
Monday, August 29, 2011 9:14 PM
All replies
-
Your description is a little confusing for me,
Showing a form as dialog form from another form
Form1.ShowDialog()
Please tell in which event you use for
Me.ShowDialog()
Please mark those posts as answer which answers your question. Faraz- Marked as answer by Liliane Teng Thursday, September 1, 2011 7:24 AM
Saturday, August 27, 2011 6:22 AM -
as said Faraz, Me.ShowDialog has no sense.
whatever you need the dialog, you instantiate the dialog class with
Dim dlg as new dlgDocOption
and show the instance withDim dr as DialogResult = dlg.ShowDialog
when execution flow cames back here, you verify the resultIf dr = dialogresultok then rc = dlg.sSetDocSections(sDocId) End If
Speaking about other things, don't waste your time using Short variable instead of Integer:
this doesn't gain anything in memory or in performance.
the names of the class and of the property should start with capital letters (for example, DocOptionDlg and DocSections)
and you need no longer to use type prefix (as 's' for string)
please, mark this as answer if it is THE answer
----------------
Diego Cattaruzza
Microsoft MVP - Visual Basic: Development
blog: http://community.visual-basic.it/Diego
web site: http://www.visual-basic.it- Marked as answer by Liliane Teng Thursday, September 1, 2011 7:24 AM
Saturday, August 27, 2011 7:50 AM -
Here is how we invoke this form from the other form:
Dim rc As Short
rc = dlgDocOption.sSetDocSections(sDocId)
Inside of the form dlgDocOption we have initialization of the controls and finally display of the form with Me.ShowDialog().
Hi AMT,
Could you please describe this issue more specific? Which form do you want to invoke exactly? Why do you use Me.ShowDialog()? Faraz and Diego's replies are helpful. Please have a try and let us know the situation on your side.
If you have any concerns, please feel free to follow up.
Have a nice day.
Best regards
Liliane Teng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Monday, August 29, 2011 8:15 AM -
Dear All,
We have dialog form named dlgDocOption.
Here is how we invoke this form from the other form:
Dim rc As Short
rc = dlgDocOption.sSetDocSections(sDocId)
Inside of the dlgDocOption form we have a function sSetDocSections()
where we perform initialization of the controls and finally display the form with
Me.ShowDialog().
At this moment execution of the function sSetDocSections() pauses and
dlgDocOption form is shown as modal, allowing user to make his/her
options selection. Two buttons (OK and Cancel) are used to dismiss the dialog
with Me.Hide() method. Besides that if OK button is clicked options selected
by the user in Far Point spread control are stored in persistent storage to be
used shortly after that in document generation.
We try already to invoke this form in a different way, similar to what you have proposed, like this:
Dim dlgDocumentOption As dlgDocOptions
dlgDocumentOption = New dlgDocOptions
rc = dlgDocumentOption.sSetDocSections(sDocId)
...
dlgDocumentOption.Close()
dlgDocumentOption.Dispose()
dlgDocumentOption = Nothing
Unfortunately we get the same result (error during second call).
In our VB 6.0 project rc variables (stands for return code) were declared As Integer. During upgrade to VB.Net their type has been converted to Short. We have Option Strict Off in all the modules therefore it is OK to declare rc As Short because implicit conversion will take place. In fact during the first run it works just fine. Just to try I have changed return value type of the sSetDocSections()function to DialogResult, but outcome did not change.
Then I accidentally removed all the following cleanup statements
dlgDocumentOption.Close()
dlgDocumentOption.Dispose()
dlgDocumentOption = Nothing
without them I can run dialog as many times as I want with no errors!
I have narrowed problem to two statements that cause it:
dlgDocumentOption.Close()
dlgDocumentOption.Dispose()
If present, any of the statement, or both of them cause the error during second run!
I have commented them out and now code works like charm.
I would like to thank you all for your replies, they were really helpful.
Have a nice day.
AMT
- Edited by A M T Monday, August 29, 2011 9:22 PM Spelling
- Marked as answer by Liliane Teng Thursday, September 1, 2011 7:23 AM
Monday, August 29, 2011 9:14 PM -
My Reply did not show up
AMTMonday, August 29, 2011 9:23 PM