Search code examples
vb.netformswinformsenumerator

How to close a form after opening another one on top of it


I have the following code - The purpose is that after saving an order, it is has been set to cancelled, then I want it to show the "New Order" form - This works fine!

However, once the new form has been opened, I want the original form, with the cancelled order, to be closed.

  Try
        cmdCheck_Click(sender, New EventArgs)
        cmdTotals_Click(sender, New EventArgs)

        For Each ugr As UltraGridRow In ugProducts.Rows

            If IsDBNull(ugr.Cells("Commission_Value").Value) = True Then
                MsgBox("Unable to save an order where one or more lines has a commission value of 0", MsgBoxStyle.OkOnly, "Error")
                Exit Sub

            Else

                If ugr.Cells("Commission_Value").Value <= 0 Then
                    MsgBox("Unable to save an order where one or more lines has a commission value of 0", MsgBoxStyle.OkOnly, "Error")
                    Exit Sub

                End If
            End If
        Next

        If chCancel.Checked = True Then

            If MsgBox("Are you sure you would like to cancel this order?", MsgBoxStyle.YesNo, "Confirm") = MsgBoxResult.No Then
                Exit Sub
            End If

        End If

        If cmbCustCode.Value = "" Or cmbSupplier.Value = "" Or txtOVal.Text = "" Or txtPVol.Text = "" Or txtPVal.Text = "" Then
            MsgBox("Not enough required data has been entered, cannot save this order", MsgBoxStyle.OkCancel, "Error")
            Exit Sub
        End If

        If isClear = True Then
            Try
                setNewValues()

            Catch ex As Exception
                errorLog(ex)
                MsgBox("Unable to save data, refer to error log", MsgBoxStyle.OkOnly, "Error")
                Exit Sub

            End Try
        End If

        gOrder.Freight = CDec(txtFVal.Text)
        gOrder.AmendedVal = CDec(txtOVal.Text)
        gOrder.AmendedVol = CDec(txtPVol.Text)
        gOrder.externalNotes = rtbExternalNotes.Text
        gOrder.InternalNotes = rtbInternalNotes.Text
        gOrder.OrderCancelled = chCancel.Checked
        gOrder.CommTotal = CDec(txtCVal.Text)
        gOrder.CommVAT = CDec(txtCVat.Text)

        Dim dtLines As New DataTable
        dtLines = ugProducts.DataSource

        Dim dsLines As New DataSet
        dsLines.Tables.Add(dtLines.Copy)

 Select Case gOrder.Stage
Case 4
                Dim proceed As Integer = 0
                For Each ugr As UltraGridRow In ugProducts.Rows

                    If ugr.Cells("Goods_Delivered").Value = False Then
                        If IsDBNull(ugr.Cells("Final_Delivery").Value) = False Then
                            ugr.Cells("Final_Delivery").Value = DBNull.Value
                        End If
                        If isamend = False Then
                            MsgBox("Unable to proceed to next stage until supplier(s) goods have been delivered", MsgBoxStyle.OkOnly, "Goods not delivered")
                        End If
                        proceed = proceed + 1
                    End If

                    If dtFreight Is Nothing Then
                        gOrder.Save(dsLines, , dtfCleared, isClear)
                      If chCancel.Checked = True Then
                            Try
                                Dim f As New frmOrder(con, False, True, currentUser, , admin)
                                f.MdiParent = Me.ParentForm
                                f.Show()

                            Catch ex As Exception
                                errorLog(ex)

                            End Try
                        End If

I tried added Me.Close() at both the start and end of the Try, however, both kept giving me the error message of

Enumerator has been exhausted. at Infragistics.Shared.SparseArray.CreateItemEnumerator.EnsureNotExhausted() at Infragistics.Shared.SparseArray.CreateItemEnumerator.System.Collections.IEnumerator.MoveNext() at Infragistics.Win.UltraWinGrid.RowEnumerator.MoveNext()

EDIT

I think that it's because the save routine is being called from another subroutine. The save button being pressed calls the subroutine that deals with the button press of another button, and this code is that subroutine.

But, even when changing this code to the actual code which is in the button click (removing the indirectness), it still happens?

So, how can I make it possible to close the existing form at the same time as opening the new one? Also, bare in mind that form being opened and the existing form are the same form, frmOrder, except that the existing form had data in it and thus some aspects were a little different.

Thanks


Solution

  • You've pretty much solved the problem in your edit.

    Initially it would have been that too many subs were in use/open at once. Moving it hasn't worked now because it's still in the Select Case.

    If you move the Me.Close() to outside of the Select Case, then it will work fine.