Question form.show not working second time around

dsk96m

Well-known member
Joined
Jan 11, 2013
Messages
173
Programming Experience
1-3
I have a form with a grid on it. I have handled the row click event (I am using devexpress winforms addins). I take the employee id from the first form, open the second form and set a value for one of the lookup combos. The first time i do this it works perfectly. If i then close that second form and double click on a different row in that grid, the application freezes on frmglobals.frm_metrics.show. It doesnt even get into the second forms load event. if i debug and F11 it, it just freezes when i f11 that line.

No idea what is causing the problem

VB.NET:
    Private Sub smetsumGV_DoubleClick(sender As Object, e As EventArgs) Handles smetsumGV.DoubleClick
        'Handle row double click
        Try
            'Get hit point of grid double click
            Dim view As GridView = CType(sender, GridView)
            Dim pt As Point = view.GridControl.PointToClient(Control.MousePosition)
            Dim info As GridHitInfo = view.CalcHitInfo(pt)
            'Is point in row
            If info.InRow OrElse info.InRowCell Then

                Dim emplid As String = smetsumGV.GetRowCellValue(smetsumGV.FocusedRowHandle, GridColumn17)

                ''See if form is already open, but just hidden
                If frmGlobals.frm_metrics Is Nothing Then
                    'if form is not already open, create new instance of form
                    frmGlobals.frm_metrics = New frm_metrics

                    'Set start position of form to center screen (since MDI Child, sets it to center screen of frm_menu)
                    frmGlobals.frm_metrics.StartPosition = FormStartPosition.CenterScreen
                    'Set parent container to menu form.
                    frmGlobals.frm_metrics.MdiParent = frm_menu

                    'Show form
                    frmGlobals.frm_metrics.Show()
                End If

                'Filter by manager.
                frmGlobals.frm_metrics.mgrLUE.EditValue = emplid

                'Bring form to front
                frmGlobals.frm_metrics.BringToFront()
            End If
        Catch ex As Exception
            'Provide Error message to user.
            MsgBox("Error: " + ex.Message, MsgBoxStyle.Exclamation, "Error")

            'Log Error
            Dim errta As New EMS_DSTableAdapters.StoredProcedures
            errta.insertError(Date.Now, ex.ToString, "frm_metrics_sectsum: Row Double Click", My.Settings.user)
        End Try
    End Sub

Am i closing the second form incorrectly or something?
VB.NET:
Private Sub frm_metricsnew_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        'Handle form close by disposing controls and form
        For Each c In Me.Controls
            c.dispose()
        Next
        Me.Dispose(True)
    End Sub
 
So if i leave the second form open, go back to the first form, click on another row, it brings the form to the front with the correct combo set. This is the expect result. It is only when I close the second form and the first form tries to reopen in.
 
You are disposing the form on close, this means it can't be used again, but frm_metrics still has reference to the form object so your check for Nothing is False.

You don't need to dispose the controls on form before disposing the form, this is part of what the form does itself when disposing. Actually you don't even need to dispose manually, when form is closed (and was not opened with ShowDialog) it will self-dispose.
 
Ok, i understand, but the weird thing is, i open that form from other places, like a menu ribbon and it doesnt have this problem. Not sure why it does here.
 
So i removed the formclosed event i had above that is on frm_metrics that had the disposing stuff in it. But when i try it the second time, it is still freezing.

BTW, the code in the first post, the top code section is from the first form frm_metrics_sectsum and the bottom code section is from the second form frm_metrics
 
As I said when form is closed it is disposed, but your variable is not Nothing, it now points to disposed form, and you can't Show a disposed form.
 
Ok. The entire purpose of how i did it with frmglobals is so that only one instance of the form can be open at any given time. I don't want to have two instances of a form open at once. I did some research on how to do that, and the frmglobals seemed the easiest. It has been working, but The whole dispose, etc is killing me. What is the best way to only have one instance of a form open at a time?
 
Back
Top