-2

Now i'm using a'jobdone' flag and the following behaviour (that it looks quite horrible to me...):

Dim NewLoginForm As New LoginClass

LoginClass.jobdone = False

NewLoginForm.Show()

While (LoginClass.jobdone = False)

    Application.DoEvents()

End While

NewLoginForm.Close()

If I try to use ShowDialog() the behaviour is better but it's a problem to manage all the opening and closing windows (if the forms are many) and I notice that all the background already opened forms close themselves either if one ShowDialog() form is closed...

Thanks

Luf
  • 63
  • 1
  • 2
  • 11
  • 1
    Forms are either modal or modeless. What are you trying to achieve exactly ? – Luc Morin Jul 16 '15 at 14:55
  • I want a clear method to open forms and when opened the other buttons in general can't be opened/clicked; or I need a method to prevent the calling (already opened) forms to be closed when a Modal form is closed – Luf Jul 16 '15 at 15:07
  • I'm sorry, English is not my mother tongue, so I can make no sense of what you're saying. I think I'll let someone with better English skills handle it. – Luc Morin Jul 16 '15 at 15:11
  • 1
    Definitely don't loop around a DoEvents. Definitely do ShowDialog. – Andrew Mortimer Jul 16 '15 at 16:05
  • FWIW, I think your code should be `While (NewLoginForm.jobdone = False)` (or simply `While Not NewLoginForm.jobdone`). Anyway, the answer I gave you uses a radically different approach, so unless you can't use it for some reason, it's a moot point. – Josh Jul 16 '15 at 17:30

1 Answers1

0

Ran a quick test and this worked perfectly:

Public Sub ForceOpen(ByRef frm As Form)
    frm.Show()
    For Each otherForm As Form In Application.OpenForms
        If otherForm Is frm Then
            AddHandler frm.FormClosing, AddressOf BlockFormClosing
        Else
            otherForm.Enabled = False
        End If
    Next
End Sub

Public Sub BlockFormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
    e.Cancel = True
End Sub

Public Sub EnableOpenForms()
    For Each frm As Form In Application.OpenForms
        frm.Enabled = True
        RemoveHandler frm.FormClosing, AddressOf BlockFormClosing
    Next
End Sub

The calling form will open the stay-open form with ForceOpen(FormStayOpen). When the stay-open form's conditions for allowing the user to close it have been satisfied, have it call EnableOpenForms().

Josh
  • 1,088
  • 1
  • 7
  • 16