如何在 c# 窗口应用程序中增加标签框文本大小

本文关键字:加标签 文本 增加 窗口 应用程序 | 更新日期: 2023-09-27 18:32:25

我使用以下代码,当我运行下面的代码时,一半的文本仅显示在标签框中。我想在标签框文本中显示整行?怎么办?

Label dynamiclabel1 = new Label();
dynamiclabel1.Location = new Point(280, 90);
dynamiclabel1.Name = "lblid";
dynamiclabel1.Size = new Size(150, 14);    
dynamiclabel1.Text="Smith had omitted the paragraph in question (an omission which had escaped notice for twenty years) on the ground that it was unnecessary and misplaced; but Magee suspected him of having been influenced by deeper reasons.";        
dynamiclabel1.AutoSize = true;
dynamiclabel1.Font = new Font("Arial", 10, FontStyle.Regular);
panel1.Controls.Add(dynamiclabel1);

如何在 c# 窗口应用程序中增加标签框文本大小

            Label dynamiclabel = new Label();
            dynamiclabel.Location = new Point(38, 30);
            dynamiclabel.Name = "lbl_ques";
            dynamiclabel.Text = question;
            //dynamiclabel.AutoSize = true;
            dynamiclabel.Size = new System.Drawing.Size(900, 26);
            dynamiclabel.Font = new Font("Arial", 9, FontStyle.Regular);

您同时使用了AutoSize和显式标签大小设置,这毫无意义。

如果要在标签文本中启用自动换行 - 必须先设置dynamiclabel1.AutoSize = false;,然后增加其Height。目前它只有 14 像素的高度,因此无法使文本多行。将其增加到大约 200 像素,所有文本都将以几行的形式放置在标签内。

如果增加高度,并且想要更少的行,请将 Width 属性设置为更大的属性;

dynamiclabel1.Width = 150;
如果要

根据字体/文本大小选择行数,可以执行以下操作:

Label dynamiclabel1 = new Label();
dynamiclabel1.Location = new Point(280, 90);
dynamiclabel1.Name = "lblid";
dynamiclabel1.Size = new Size(150, 100);
dynamiclabel1.Text = "Smith had omitted the paragraph in question (an omission which had escaped notice for twenty years) on the ground that it was unnecessary and misplaced; but Magee suspected him of having been influenced by deeper reasons.";
dynamiclabel1.Font = new Font("Arial", 10, FontStyle.Regular);
int lines = 3; //number of lines you want your text to display in
using (Graphics g = CreateGraphics())
{
     SizeF size = g.MeasureString(dynamiclabel1.Text, dynamiclabel1.Font);
     float w=size.Width / lines;
     dynamiclabel1.Height = (int)Math.Ceiling(size.Height*lines);
     dynamiclabel1.Width = (int)Math.Ceiling(w);
}
panel1.Controls.Add(dynamiclabel1);

编辑
如果您知道的是可用的宽度,并且希望标签调整高度以显示所有内容,则可以执行以下操作:

Label dynamiclabel1 = new Label();
dynamiclabel1.Location = new Point(280, 90);
dynamiclabel1.Name = "lblid";
dynamiclabel1.Size = new Size(150, 100);
dynamiclabel1.Text = "Smith had omitted the paragraph in question (an omission which had escaped notice for twenty years) on the ground that it was unnecessary and misplaced; but Magee suspected him of having been influenced by deeper reasons.";
dynamiclabel1.Font = new Font("Arial", 10, FontStyle.Regular);
int width  = 600; //Width available to your label
using (Graphics g = CreateGraphics())
{
    SizeF size = g.MeasureString(dynamiclabel1.Text, dynamiclabel1.Font);
    lines = (int)Math.Ceiling(size.Width / width);
    dynamiclabel1.Height = (int)Math.Ceiling(size.Height * lines);
    dynamiclabel1.Width = width;
}
panel1.Controls.Add(dynamiclabel1);