c#web表示如何在一个标签文本中使用两种或更多种颜色

时间:2016-05-06 05:14:58

标签: c# winforms

label1.Text = "welcome to stackoverflow'" + textbox1.text + "'";
label1.ForeColor = Color.Green;

我想让textbox1.text有不同的颜色如何在获胜形式中采取不同的颜色?
在必须带标签的网络表单中,但在win表单中它不起作用?

regads,
龙树。

2 个答案:

答案 0 :(得分:0)

那是不可能的。您可以使用richtextbox并删除边框并使其不可编辑,因此它看起来像标签或使用HTML控件并使用HTML标记。

这是使用像文本

这样的richtextbox的xaml示例
<RichTextBox BorderStyle="none" Enabled="true" ReadOnly="true" Height="160" HorizontalAlignment="Left" Margin="43,20,0,0" Name="richTextBox1" VerticalAlignment="Top" Width="258" TextChanged="richTextBox1_TextChanged">
    <FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Paragraph>
            <Run Foreground="Red">Red</Run>
            <Run Foreground="Green">Green</Run>
        </Paragraph>
    </FlowDocument>
</RichTextBox>

这是一个代码示例

Paragraph para = new Paragraph();
Run run1 = new Run("welcome to stackoverflow ");
run1.Foreground = Brushes.Red;
Run run2 = new Run(textbox1.text);
run2.Foreground = Brushes.Green;
para.Inlines.Add(run1);
para.Inlines.Add(run2);
// Get the document
FlowDocument doc = richTextBox1.Document;
// Clear existing content
doc.Blocks.Clear();
// Add new content
doc.Blocks.Add(para);

答案 1 :(得分:0)

.NET(winform)中没有可以为标签添加多种颜色的本机控件,但您可以尝试Label With multiple colors