Search code examples
vb.netformclosing

Form Enabled in FormClosing Event Not Working


I'll show you my code first:

Private Sub AddProductToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AddProductToolStripMenuItem.Click
    Me.Enabled = False
    Dim frmAddProduct As New FormAddProduct
    frmAddProduct.Show()
    frmAddProduct.Owner = Me
End Sub

That is my Main Form to call AddProduct form, and this is my FormClosing in AddProduct

Private Sub FormAddProduct_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    ButtonReset.PerformClick()
    Lock()
    Me.Owner = Nothing
    Me.Hide()
    Dim frmMainIndex As New FormMainIndex
    frmMainIndex.Enabled = True
End Sub

So I have set enabled = false in my main form when it call Add Product form, and enabled = true when I close my Add Product form, but enabled = true won't work.

When I close my Add Product, it's only hide Add Product form but not enabling main form, main form still not enabled. Is there something wrong with my code?


Solution

  • This line is your problem:

    Dim frmMainIndex As New FormMainIndex
    

    You are instantiating a new FormMainIndex. Whenever you use the New keyword you are creating a completely new and independant object. frmMainIndex is a completely different form than the first one which opened your FormAddProduct form.

    Since you've set the FormAddProduct's owner to your FormMainIndex form, just set the owner's Enabled property to True instead:

    ButtonReset.PerformClick()
    Lock()
    Me.Owner.Enabled = True
    Me.Owner = Nothing
    Me.Hide()
    

    Also, your Me.Hide() call doesn't make any sense since your form is about to be closed.