Cause this VB6-like smell, which VB.NET allows, to error instead: WinFormType.InstanceProp=Value [DISABLE My.Forms]

后端 未结 2 1140
夕颜
夕颜 2020-11-30 15:20

I have notice something very obnoxious with VB.Net\'s treatment of Winform objects.

This has trashed several hours of our time. It will only get worse as we have more

2条回答
  •  孤独总比滥情好
    2020-11-30 16:00

    Sorta. You could create a function or override ShowDialog that checks the My.Forms collection to see if the current instance is in it and if it is thrown an exception. There's not a setting that can be used to prevent it from being used.

    EDIT: adding sample code illustrating the concept (using a beep instead of an exception).

        Shared Function FormIsInMyForms(formName As String, frm As Form)
            If formName = "Form1" Then
                If frm.Equals(Form1) Then
                    Return True
                Else
                    Return False
                End If
            End If
            Return False
    
        End Function
    
        Public Overloads Sub ShowDialog()
    
            If FormIsInMyForms("Form1", Me) Then
                Beep()
            End If
    
            MyBase.ShowDialog()
        End Sub
    

    Doing the same thing via reflection is left as an exercise for the reader... :)

提交回复
热议问题