none
How can I tell when a (custom) control's form has been created? RRS feed

  • Question

  • WHAT I HAVE:

    Visual Basic 2015, .NET 4+, WinForms

    MY PROBLEM:

    I have a UserControl custom control that needs to know when its ultimate container form is attached and has an active window--as certain operations are performed against the form itself. I currently check to see if Me.FindForm IsNot Nothing, but I find myself checking that in several events--including the control's HandleCreated, ControlAdded, VisibleChanged, Enter, and Load events. It would be nice if there was a specific event or group of events where I could be reasonably sure that the parent (top-level) form has been established/dis-established and its handle created/destroyed. Any advice?


    Robert Gustafson

    Thursday, November 14, 2019 7:20 AM

Answers

  • I did a little experimentation, and it seems that the best time to see if the control's container form is defined and has a handle is in the control's Load and HandleCreated events (particularly Load), and the best time to see if it isn't is in the HandleDestroyed and Disposed events (particularly Disposed). For example:

    Public Class MyControl

       ...

       Private IsFormAvailable As Boolean = False 'used by other procedures

       ...

       Private Sub MyControl_Load(sender As Object, e As EventArgs) Handles Me.Load

       CheckForForm()

       DoLoadCode()

       End Sub


       Private Sub MyControl_HandleCreated(sender As Object, e As EventArgs) Handles Me.HandleCreated

       CheckForForm()

       DoHandleCreatedCode()

       End Sub


       Private Sub MyControl_HandleDestroyed(sender As Object, e As EventArgs) Handles Me.HandleDestroyed

       CheckForForm()

       DoHandleDestroyedCode()

       End Sub


       Private Sub MyControl_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed

       CheckForForm()

       DoDisposedCode()

       End Sub

       ...

       Private Sub CheckForForm() 'can be used by other procedures

       IsFormAvailable = _

          (Me.FindForm IsNot Nothing AndAlso Me.FindForm.IsHandleCreated)

       End Sub

       ...

    End Class


    Robert Gustafson



    Saturday, November 16, 2019 5:06 AM

All replies

  • Hi RobertGustafson,

    According to your description, I am not sure what your requirement is.

    If about active window, you can refer to this document.

    [Form.ActiveForm Property].

    Best Regards

    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 15, 2019 5:36 AM
  • I'm creating a custom control. My problem is knowing what event to wait for if I want to have to test for the existence of a user control's form as little as possible.

    >>>I should note that I'm talking about the code inside a user control's or extended control's definition, not the code of a host project that uses the control.

    I know that when Me.FindForm (Me being the control being defined) isn't Nothing, then control is attached to a form, and the form has a handle. The question is, what event inside the control's class module should I wait for to optimally make this test? As in the following code:

    Public Class MyControl 'my custom control

       ...

       Private Sub MyControl_SomeEvent(sender As Object, e As SomeEventArgs) Handles Me.SomeEvent

       SomeCode()

       If Me.FindForm IsNot Nothing Then

          DoThis() 'control's container form has a handle

        Else

          DoThat() 'control has no form with a handle

       End If

       SomeMoreCode()

       End Sub

       ...

    End Class


    So, what particular event (or most probable set of events) would "SomeEvent" actually be in order for DoThis() to execute at the earliest possible time? For DoThat() to first be executable? (That is, in what event of MyControl does Me.FindForm first become something? First become Nothing?)


    Robert Gustafson









    Saturday, November 16, 2019 1:58 AM
  • While the above link is in itself useful, it still doesn't solve the problem. I need to know what events inside my custom control to look out for, not the events in the container form. While I could theoretically use AddHandler to catch events in the container form, that approach presupposes that the control has already been attached to a form to catch events for, and checking for that brings me back to Square 1.

    What I found out through trial and error is that the creation of a handle for a custom control does not necessarily mean that the control has been attached to a form and that the form already has a handle--hence the need for me to make the check in several of the custom control's events.


    Robert Gustafson


    Saturday, November 16, 2019 3:19 AM
  • After the form loads call a procedure from the control form from whichever event you deem good.

    https://awwshop.wikidot.com


    • Edited by GeoFrias Saturday, November 16, 2019 3:29 AM
    Saturday, November 16, 2019 3:28 AM
  • How does the control know when its form is loaded? I'm looking for a solution which is self-contained within the control's class logic, and not dependent on the host project "telling it" (i.e., via a method) that a form has it. Once again, I'm talking about the logic defining a user control's or extended control's implementation, not the logic of the host project using the control.

    Am I not making my situation clear?!


    Robert Gustafson

    Saturday, November 16, 2019 3:36 AM
  • I can't find anything related to that, in fact something says to use caution with UserControl.Load here: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.usercontrol.load?view=netframework-4.8

    I would start a timer from the control to check for an IsLoaded variable.


    https://awwshop.wikidot.com

    Saturday, November 16, 2019 4:29 AM
  • I did a little experimentation, and it seems that the best time to see if the control's container form is defined and has a handle is in the control's Load and HandleCreated events (particularly Load), and the best time to see if it isn't is in the HandleDestroyed and Disposed events (particularly Disposed). For example:

    Public Class MyControl

       ...

       Private IsFormAvailable As Boolean = False 'used by other procedures

       ...

       Private Sub MyControl_Load(sender As Object, e As EventArgs) Handles Me.Load

       CheckForForm()

       DoLoadCode()

       End Sub


       Private Sub MyControl_HandleCreated(sender As Object, e As EventArgs) Handles Me.HandleCreated

       CheckForForm()

       DoHandleCreatedCode()

       End Sub


       Private Sub MyControl_HandleDestroyed(sender As Object, e As EventArgs) Handles Me.HandleDestroyed

       CheckForForm()

       DoHandleDestroyedCode()

       End Sub


       Private Sub MyControl_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed

       CheckForForm()

       DoDisposedCode()

       End Sub

       ...

       Private Sub CheckForForm() 'can be used by other procedures

       IsFormAvailable = _

          (Me.FindForm IsNot Nothing AndAlso Me.FindForm.IsHandleCreated)

       End Sub

       ...

    End Class


    Robert Gustafson



    Saturday, November 16, 2019 5:06 AM