Pergunta

I would like to clarify that I already know how to check if a form is loaded by setting a boolean variable flag (Ex: dim FormIsLoaded as boolean) and setting it to True after the Load event of the form, to restrict the usage of the [Enum].Parse method that is causing the exceptio in my coden.

But that is not what I'm asking for, this is not a typicall question, please continue reading...


I have an eventhandler like this:

''' <summary>
''' Indicates the resulting Batch command redirection.
''' </summary>
Private CommandRedirection As Reg2Bat.CommandRedirection = 
        Reg2Bat.CommandRedirection.Standard

Private Sub KryptonRadioButton_CommandRedirections_CheckedChanged(sender As Object, e As EventArgs) Handles _
        KryptonRadioButton_CommandRedirection_Standard.CheckedChanged,
        KryptonRadioButton_CommandRedirection_Error.CheckedChanged,
        KryptonRadioButton_CommandRedirection_All.CheckedChanged,
        KryptonRadioButton_CommandRedirection_None.CheckedChanged

    If CBool(sender.checked) Then
        CommandRedirection = [Enum].Parse(GetType(CMD.CommandRedirection), 
                                          CStr(sender.tag), True)
    End If

End Sub

But the If is giving me an error of type An unhandled exception of type 'System.InvalidOperationException' occurred in Program.exe and I'm sure that the error occurs because the form is not totally loaded before the eventhandler tries to access the value of the control.

StackTrace:

en System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult) 
en System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)    
en Reg2Bat.GUI.KryptonRadioButton_CommandRedirection_Standard_CheckedChanged(Object sender, EventArgs e) 
en C:\Users\Administrador\Desktop\Reg2Bat\Reg2Bat\UserInterfaces\GUI\GUI.vb:línea 271  
en ComponentFactory.Krypton.Toolkit.KryptonRadioButton.set_Checked(Booleanvalue) 
en Reg2Bat.GUI.InitializeComponent() 
en C:\Users\Administrador\Desktop\Reg2Bat\Reg2Bat\UserInterfaces\GUI\GUI.Designer.vb:línea 104    
en Reg2Bat.GUI..ctor() 
en C:\Users\Administrador\Desktop\Reg2Bat\Reg2Bat\User Interfaces\GUI\GUI.vb:línea 49

I would like to improve a 'FormIsLoaded?' checking without writting unnecessary variables (boolean flags) to keep my code more pretty and simplified, so, as the title of my question says, what I'm asking for is that if exist a property, or just, an In-One-Line solution to check whether a form is fully loaded in any moment.

I'm also asking this to know more improved alternatives than setting a boolean to check that a form is loaded.

By the moment I'm trying the property (Me.IsAccessible), and seems that is working correctlly because now the [Enum].Parse method does not throws any exception, but I'm not sure of what the property is really checking (I've read the IntelliSense help of the prop but nothing).

Private Sub KryptonRadioButton_CommandRedirections_CheckedChanged(sender As Object, e As EventArgs) Handles _
        KryptonRadioButton_CommandRedirection_Standard.CheckedChanged,
        KryptonRadioButton_CommandRedirection_Error.CheckedChanged,
        KryptonRadioButton_CommandRedirection_All.CheckedChanged,
        KryptonRadioButton_CommandRedirection_None.CheckedChanged

    If Me.IsAccessible Then

        If CBool(sender.checked) Then
            CommandRedirection = [Enum].Parse(GetType(Reg2Bat.CommandRedirection),
                                              CStr(sender.tag), True)
        End If

    End If

    MsgBox(CommandRedirection.ToString)

End Sub

So, using the Me.IsAccessible property it's the properlly solution to check if a form/controls are loaded? If not, there is a property that could check this?.

Foi útil?

Solução

I believe what you're looking for is Form.IsHandleCreated to confirm is is built (and has gone through InitialiseComponent() routine in the .Designer file.)

After that it is worth checking Form.IsDisposed to make sure it has not since been destroyed.

This is more difficult in multiple threaded applications because unless you can execute the check on the form's owning thread, its state change can happen after doing the test.

Outras dicas

This has been working fine for me, too:

Dim frmShown As Boolean = False
Private Sub MemberList_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
    frmShown = True
End Sub

It ensures all controls have been rendered. Then you test for 'frmShown'

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top