Using Color As Optional Parameter In a function within a class

后端 未结 4 1268
轮回少年
轮回少年 2021-01-11 16:09

How can I declare an optional color parameter in some function or sub, as if I do that in the normal way (I mean to give some default color for that optional parameter) as t

4条回答
  •  借酒劲吻你
    2021-01-11 17:01

    There is another feature in the .NET color world that will allow you to carry out your original intentions. The feature is an enumeration called "KnownColor" which can freely translate back and forth to System.Drawing.Color objects. While it doesn't have ALL possible colors, it has all the colors I have ever needed. And because it is an enumeration, it has "constants" that work as default value specifiers in an optional argument. Example:

    Private Sub Test(a As Integer, Optional kc As KnownColor = KnownColor.Black)
        Dim MyColor As System.Drawing.Color = Color.FromKnownColor(kc)
        ......
    End Sub
    

    According to

    https://docs.microsoft.com/en-us/dotnet/api/system.drawing.color.toknowncolor
    

    you can use the System.Drawing.Color.ToKnownColor() function to translate back to a value in the KnownColor enumeration if the Color is created from a predefined color by using either the FromName(String) method or the FromKnownColor(KnownColor) method. Otherwise it will return the value 0.

提交回复
热议问题