首页 > 解决方案 > 从中心对称更新 WinForms 标签中的文本

问题描述

Label如果这里的每一行都是值更新,而不是单个文本,首先显示“A”,然后显示“New”等,如何从中心对称更新 WinForms 控件中的文本:

            A
           New
           Year
          Comes
          again 
        and again
        to spread 
      the spirit and
     Celebration have 
   a wonderful New Year 
 party and Happy New Year
    with joy and peace 

标签: c#winformslabeltext-alignment

解决方案


使用 StringBuilder,标签的 TextAlign 属性设置为 MiddleCenter,AutoSize 设置为 true。

StringBuilder sb = new StringBuilder();
sb.AppendLine("A");
sb.AppendLine("New");
sb.AppendLine("Year");
sb.AppendLine("Comes");
sb.AppendLine("again");
sb.AppendLine("and again");
sb.AppendLine("to spread");
sb.AppendLine("the spirit and");
sb.AppendLine("Celebration have");
sb.AppendLine("a wonderful New Year");
sb.AppendLine("party and Happy New Year");
sb.AppendLine("with joy and peace");

Label l = new Label();
l.AutoSize = true;
l.TextAlign = ContentAlignment.MiddleCenter;
l.Text = sb.ToString();

Controls.Add(l);

推荐阅读