Using Color As Optional Parameter In a function within a class

后端 未结 4 1294
轮回少年
轮回少年 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 16:40

    In Visual Basic, the solution proposed by @Steve nearly works but setting Color to Nothing does not do what you might normally expect; the check needs to be for the Empty Color, not for Nothing. Here is an example. It Flashes a Windows Forms Control's BackColor for 50 milliseconds, so long as it has a .ForeColor:

    Sub Flash(c As Control, Optional FlashColor As Color = Nothing,
              Optional Duration As Integer = 50)
        If FlashColor.Equals(System.Drawing.Color.Empty) Then FlashColor = Color.Red
        Try
            Dim CurrColor = c.BackColor
            c.BackColor = FlashColor
            c.Refresh()
            System.Threading.Thread.Sleep(Duration)
            c.BackColor = CurrColor
            c.Refresh()
        Catch ex as Exception
        End Try
    End Sub

提交回复
热议问题