C# label1属性连接label2属性

C# label1属性连接label2属性,c#,winforms,C#,Winforms,label1text&property和label2text&property是否可以合并并在label3中显示它,并添加文本=?因为我现在使用的是并排使用label1和label2 告诉我是否还有其他方法 Ps:我在数据库中定义颜色,如红色或蓝色您可以这样组合文本内容: label3.Text = label1.Text + " = " + label2.Text; 但你会失去不同的颜色。不幸的是,这是不可能的。有关更多详细信息,请选中使用string.format将两个标签文本组合在一起

label1
text&property和
label2
text&property是否可以合并并在
label3
中显示它,并添加文本
=
?因为我现在使用的是并排使用
label1
label2

告诉我是否还有其他方法


Ps:我在数据库中定义颜色,如
红色
蓝色

您可以这样组合文本内容:

label3.Text = label1.Text + " = " + label2.Text;

但你会失去不同的颜色。不幸的是,这是不可能的。有关更多详细信息,请选中使用string.format将两个标签文本组合在一起

label3.Text = string.Format("{0}={1}", label1.Text, label2.Text);
为什么否决投票

您可以在label3上编写自己的文本图像。像

首先设置label3 AutoSize=false并设置大小。

    // Add this lines to InitializeComponent() in yourform.Designer.cs
    this.label1.TextChanged += new System.EventHandler(this.label_TextChanged);
    this.label2.TextChanged += new System.EventHandler(this.label_TextChanged);


    // this is label1 and label2 TextCahanged Event
    private void label_TextChanged(object sender, EventArgs e)
    {
        SetMultiColorText(string.Format("{0} = {1}", label1.Text, label2.Text),label3);
    }

// this method set multi color image text for label(paramter lb)
    public void SetMultiColorText(string Text, Label lb)
    {
        lb.Text = "";
        // PictureBox needs an image to draw on
        lb.Image = new Bitmap(lb.Width, lb.Height);
        using (Graphics g = Graphics.FromImage(lb.Image))
        {


            SolidBrush brush = new SolidBrush(Form.DefaultBackColor);
            g.FillRectangle(brush, 0, 0,
                lb.Image.Width, lb.Image.Height);

            string[] chunks = Text.Split('=');
            brush = new SolidBrush(Color.Black);

            // you can get this colors from label1 and label2 colors... or from db.. or an other where you want
            SolidBrush[] brushes = new SolidBrush[] { 
        new SolidBrush(Color.Black),
        new SolidBrush(Color.Red) };
            float x = 0;
            for (int i = 0; i < chunks.Length; i++)
            {
                // draw text in whatever color
                g.DrawString(chunks[i], lb.Font, brushes[i], x, 0);
                // measure text and advance x
                x += (g.MeasureString(chunks[i], lb.Font)).Width;
                // draw the comma back in, in black
                if (i < (chunks.Length - 1))
                {
                    g.DrawString("=", lb.Font, brush, x, 0);
                    x += (g.MeasureString(",", lb.Font)).Width;
                }
            }
        }
    }
//将此行添加到yourform.Designer.cs中的InitializeComponent()
this.label1.TextChanged+=新的System.EventHandler(this.label\u TextChanged);
this.label2.TextChanged+=新的System.EventHandler(this.label\u TextChanged);
//这是label1和label2文本更改事件
私有无效标签\u text已更改(对象发送方,事件参数e)
{
SetMultiColorText(string.Format(“{0}={1}”,label1.Text,label2.Text),label3);
}
//此方法为标签设置多色图像文本(参数lb)
公共无效设置多色文本(字符串文本,标签lb)
{
lb.Text=“”;
//PictureBox需要一个图像来绘制
lb.图像=新位图(lb.宽度,lb.高度);
使用(Graphics g=Graphics.FromImage(lb.Image))
{
SolidBrush笔刷=新的SolidBrush(Form.DefaultBackColor);
g、 圆角矩形(笔刷,0,0,
磅图像宽度,磅图像高度);
string[]chunks=Text.Split('=');
笔刷=新的SolidBrush(颜色为黑色);
//您可以从label1和label2颜色…或从db…或其他您想要的地方获取此颜色
SolidBrush[]笔刷=新的SolidBrush[]{
新SolidBrush(颜色为黑色),
新的SolidBrush(颜色为红色)};
浮动x=0;
for(int i=0;i
好的,谢谢确认。我只想知道,当你说“合并属性”时,是否可以将2属性标签合并为1。哪些属性?