2016-01-26 28 views
0

我正在製作一個控制檯應用程序,並且我想將Console.foregroundcolor代碼轉換爲布爾值,以便如果該值爲true,則文本顏色將爲綠色,如果爲false,則爲紅色。 謝謝!VB.NET Console.foregroundcolor as boolean?

+0

'Console.foregroundcolor代碼爲boolean'你不能這樣做...你可能想要做的就是有一個布爾標誌來確定'Console.Foregroundcolor' – Ian

+0

你在找什麼是'if'語句。 https://msdn.microsoft.com/en-us/library/752y8abs.aspx –

回答

0

簡單地使一個變量和一個if語句將幫助你在這裏,因爲你沒有指定什麼它我會做一個例子:

Dim False As String = console.foregroundcolor = consolecolor.Red 
Dim True As String = console.foregroundcolor = consolecolor.Green 

然後做你的編碼四周,當談到是否需要將其剛剛要麼驗證的時刻做出:

驗證=假

OR

驗證=真

然後執行以下「如果」語句:「這是真的」

If Verification = True 
True          (To change the text colour) 
Console.Writeline("This is True!") 
ElseIf Verification = False 
False          (To change the text colour) 
Console.Writeline("This is False!") 
EndIf 

,這樣就會將顯示綠色的文字與書面或紅色文字,「這是假的!」書面。

0

另一種方法是使用擴展方法。

Module ExtensionMethods 

    <Runtime.CompilerServices.Extension> 
    Public Function ToColor(ByRef b As Boolean) As System.ConsoleColor 
     If b Then 
      Return ConsoleColor.Green 
     Else 
      Return ConsoleColor.Red 
     End If 
    End Function 

End Module 

然後,你可以調用.ToColor()您布爾

Dim myBool As Boolean = True 

Console.ForegroundColor = myBool.ToColor 
Console.WriteLine("This is green") 

myBool = False 
Console.ForegroundColor = myBool.ToColor 
Console.WriteLine("This is Red") 

Console.ForegroundColor = ConsoleColor.White 

Color foreground console app