Question Closing and re-opening the same form

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
Hi there

I am attempting to find a way to get an existing form to close and re-open (with the controls re-set).

If I use a method like

VB.NET:
If AppBox.Show("Transaction successfully saved! Process another?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            AccountingMainWindow.Close()
            AccountingMainWindow.Refresh()
            LoadPettyCashPayment()
        Else
            AccountingMainWindow.Close()

        End If

..and the user selects 'Yes' then another instance of the form does open as required, but directly over the original.

Is there a method to ensure that the first instance is fully closed before raising the next instance?

Thanks....


This is how the form loads

VB.NET:
Public Sub LoadPettyCashPayment()
        'Validate petty cash account
        Dim vNLDT As DataTable = ReturnDT(NL_Codes, "NL_Codes")
        Dim FR() As DataRow = vNLDT.Select("NL_Code = 1220", Nothing)
        Dim vPCName As String = NullString(FR(0)("NL_Description"))
        If vPCName = "" Then
            AppBox.Show("The Petty Cash account (1220) has been deleted, or is invalid!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        'Determine if the Petty cash account has funds in it!
        Dim BankDT As DataTable = ReturnDT(A_Bank_Transactions, "A_Bank_Transactions")
        Dim vCredit As Double = NullValues(BankDT.Compute("SUM(Credit)", "Nominal_Code = 1220"))
        Dim vDebit As Double = NullValues(BankDT.Compute("SUM(Debit)", "Nominal_Code = 1220"))
        Dim vBalance As Double = vCredit - vDebit
        If vBalance <= 0 Then
            AppBox.Show("The current Petty Cash balance is $" & Format(vBalance, "###,##0.00") & ". You will need to add funds (Bank / Transfer) to continue!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        'OK to launch Petty Cash form
        'AppBox.Show(vBalance.ToString, MessageBoxButtons.OK, MessageBoxIcon.Information)

        AccountingMainWindow = New Form
        AccountingMainWindow.Controls.Clear()
        AccountingMainWindow.Size = New Point(650, 450)
        AccountingMainWindow.Text = "Petty Cash Payment"

        Dim vPanel As New Panel
        vPanel.Dock = DockStyle.Fill
        vPanel.AutoScroll = True

Labels, ComboBox and TextBoxes added to the Panel

ToolStrip and buttons added to the form

VB.NET:
        AMGStatusStrip = New StatusStrip
        AMGStatusLabel = New ToolStripStatusLabel
        AMGProgressBar = New ToolStripProgressBar
        AMGStatusStrip.Items.Add(AMGProgressBar)
        AMGStatusStrip.Items.Add(AMGStatusLabel)
        AccountingMainWindow.Controls.Add(vPanel)
        AccountingMainWindow.Controls.Add(vToolStrip)
        AccountingMainWindow.Controls.Add(AMGStatusStrip)
        AccountingMainWindow.StartPosition = FormStartPosition.CenterParent
        AccountingMainWindow.ShowDialog()
 
Last edited:
Instead of trying to close and reopen the form from itself, create a method (FormReload maybe?) and then put all the code that you use to initialize the form into it. Then you can call the method from anywhere in the form and it will be like reloading the form.
 
Reload

Thanks for your reply

I did try a method like that - the code in the Sub used in the example would have a boolean..

VB.NET:
Public Sub LoadPettyCashPayment(byval Reload as boolean)

If Reload = False Then
        AccountingMainWindow = New Form
End If
        AccountingMainWindow.Controls.Clear()
        AccountingMainWindow.Size = New Point(650, 450)
        AccountingMainWindow.Text = "Petty Cash Payment"

        Dim vPanel As New Panel
        vPanel.Dock = DockStyle.Fill
        vPanel.AutoScroll = True

Other controls loaded......

VB.NET:
        AccountingMainWindow.Controls.Add(vPanel)
        AccountingMainWindow.Controls.Add(vToolStrip)
        AccountingMainWindow.Controls.Add(AMGStatusStrip)
If Reload = False then
        AccountingMainWindow.StartPosition = FormStartPosition.CenterParent
        AccountingMainWindow.ShowDialog()
End If

The problem is twofold
1. The controls do not 'rebuild' - the data is retained
2. The control handlers no longer respond!
 
The AccountingMainForm is created entirely in code, there is no design time form for it. Correct?

And the LoadPettyCashPayment method is on a different form. Correct?
 
Yup - eveything is created in code - hardly ever use the designer, gets too cluttered!

The LoadPettyCashPayment method loads the AccountingMainForm (Defined as a Public variable)

For some odd reason the reload method now seems to be working! Watch this space...

Thanks
 
Back
Top