Wine Too
Wine Too

Reputation: 4655

Non modal form that blocks its Owner

I was trying to get forms configuration for my project that can suit my needs here and didn't get to a solution. It seem's I find a solution and now I have to implement it application-wide.

Here I have main form and a few subforms (which have many subforms) and through the main form I should ALWAYS be able to close all opened forms, watch for universal keypresses, close application permanently and watch for critical events.

I find solution in this facts:

In forms that have to block its own caller (Owner) I added property "Blocking" so that my code looks like this:

If Not formIsOpened(frm_myFirstChild) Then
    Dim f As New frm_myFirstChild
    f.Blocking = True
    f.Show(Me)
    f = Nothing
End If

In the frm_myFirstChild I have property:

<Browsable(True), _
 DefaultValue(False)> _
Public Property Blocking() As Boolean
    Get
        Return _Blocking
    End Get
    Set(ByVal value As Boolean)
        _Blocking = value
    End Set
End Property

If boolean property "Blocking" is TRUE then under _Load this code has to be executed:

If Blocking And Me.Owner IsNot Nothing Then
   Me.Owner.Enabled = False
End If

In _FormClosing that:

If Blocking And Me.Owner IsNot Nothing Then
   Me.Owner.Enabled = True
   Me.Owner.Activate()
End If

All of that work as expected so I try to implement that for all forms and use when needed in a subclass "cls_transform":

Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
    If Blocking And Me.Owner IsNot Nothing Then
        Me.Owner.Enabled = True
        Me.Owner.Activate()
    End If
    MyBase.OnFormClosing(e)
End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    If Blocking And Me.Owner IsNot Nothing Then
        Me.Owner.Enabled = False
    End If
    MyBase.OnLoad(e)
End Sub

Here I have problem that subclass doesn't understand the property Blocked (not declared).

How do you get a Form's property Blocked to subclass so I can use those subclasses for all forms, and switch the property Blocking from the outside regarding functionality needs?

Upvotes: 0

Views: 305

Answers (1)

Hans Passant
Hans Passant

Reputation: 941337

Sounds to me that "cls_transform" is actually a class derived from Form, the only way that OnFormClosing could work. Which is okay, your "sub-forms" now need to derive from cls_transform instead of Form. Do pick a better name.

Then simply add the Blocking property to that cls_transform class to solve your problem.

Do note that there's a bug in your OnFormClosing method. It can be canceled and that will leave the form opened with its owner in the wrong state. You need to write it like this instead:

Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
    MyBase.OnFormClosing(e)
    If Not e.Cancel And Blocking And Me.Owner IsNot Nothing Then
        Me.Owner.Enabled = True
        Me.Owner.Activate()
    End If
End Sub

Upvotes: 1

Related Questions