vb.net 设置不同的文本时是否可以更改文本颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20584458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:08:49  来源:igfitidea点击:

Is it possible to change text colour when different text is set?

vb.netvb.net-2010

提问by LukeSC1993

I know the title doesn't describe too good but I was wondering if there was a certain code to type into Visual Basic to make the text in a textbox change colour when the text changes.

我知道标题描述得不太好,但我想知道是否有特定的代码可以输入到 Visual Basic 中,以便在文本更改时使文本框中的文本更改颜色。

Here is my code:

这是我的代码:

If My.Computer.Network.IsAvailable Then
            txtnetwork.Text = "Connected"
        Else
            txtnetwork.Text = "Disconnected"
        End If

So is there a way of making the "Connected" text Green, and the "Disconnected" text Red?

那么有没有办法将“已连接”文本设为绿色,将“已断开连接”文本设为红色?

采纳答案by John Koerner

Sure, just set the ForeColorproperty:

当然,只需设置ForeColor属性:

    If My.Computer.Network.IsAvailable Then
        txtnetwork.ForeColor = Color.Green
        txtnetwork.Text = "Connected"
    Else
        txtnetwork.ForeColor = Color.Red
        txtnetwork.Text = "Disconnected"
    End If