首页 > 解决方案 > 检查表单的图标是否为空或设置为默认图标?

问题描述

知道如何检查表单的图标值是否为空值或设置为默认值吗?我尝试过这样的事情,但没有运气......

If Me.Icon Is Nothing Then
     MsgBox("Nothing")
End If

我注意到表单的图标属性已经有一个值。默认一个。那么,如何检查我的表单是否使用此默认值?

先感谢您!!!

标签: vb.netvisual-studio-2017

解决方案


似乎没有办法确定当前Icon是否为DefaultIcon. 但是有一个静态/共享属性DefaultIcon,您可以通过反射访问它:

Imports System.Reflection
Imports System.Runtime.CompilerServices

Module FormsExtensions

    <Extension()>
    Public Function HasDefaultIcon(form As System.Windows.Forms.Form) As Boolean
        ' relies on reflection, so might break in future
        ' necessary because the DefaultIcon property is internal
        Dim fType = GetType(Windows.Forms.Form)
        Dim defaultIconProp = fType.GetProperty("DefaultIcon", BindingFlags.NonPublic Or BindingFlags.Static)
        Dim defaultIcon = TryCast(defaultIconProp?.GetValue(form), System.Drawing.Icon)
        Return form.Icon Is defaultIcon
    End Function

End Module

使用这种扩展方法,检查很容易:

Dim hasDefaultIcon As Boolean = Me.HasDefaultIcon()

推荐阅读