有一个属性来确定是否加载了窗体(和它的控件)?

时间:2022-11-19 15:53:58

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.

我想澄清的是,我已经知道如何通过设置布尔变量标志(例如:dim FormIsLoaded as boolean)来检查窗体是否已加载,并在窗体加载事件之后将其设置为True,以限制[Enum]的使用。解析方法,导致代码中的异常。

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

但这不是我要问的,这不是一个典型的问题,请继续读……


I have an eventhandler like this:

我有一个像这样的eventhandler:

''' <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 acces the value of the control.

但是If给了我一个类型错误类型系统的未处理异常。InvalidOperationException”发生在程序。exe和我确信出现错误是因为在eventhandler尝试加入控件的值之前,表单没有被完全加载。

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 unecessary 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.

我想改进一下?“检查时不需要写unecessary变量(布尔标志),以使我的代码更漂亮、更简单,所以,正如我的问题的标题所说,我所要求的是,如果存在一个属性,或者只是一个单行的解决方案,来检查表单是否在任何时刻都被完全加载。

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 'cause 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).

当我尝试这个属性(我可以访问)的时候,它似乎正在正确地工作,因为现在[Enum]。解析方法不会抛出任何异常,但是我不确定属性是否真正检查了(我已经读过了这个道具的智能感知帮助,但是没有)。

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?.

所以,使用我。isaccess属性它是检查窗体/控件是否已加载的合适解决方案?如果没有,有一个属性可以检查这个?

1 个解决方案

#1


6  

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.)

我相信你在寻找的是形式。IsHandleCreated to confirm is是构建的(并且已经在. designer文件中执行了InitialiseComponent()例程)。

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.

这在多线程应用程序中更加困难,因为除非您能够对窗体所属的线程执行检查,否则在执行测试之后,窗体的状态就会发生改变。

#1


6  

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.)

我相信你在寻找的是形式。IsHandleCreated to confirm is是构建的(并且已经在. designer文件中执行了InitialiseComponent()例程)。

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.

这在多线程应用程序中更加困难,因为除非您能够对窗体所属的线程执行检查,否则在执行测试之后,窗体的状态就会发生改变。