我使用下面的代码,当我在代码下面运行时,一半的文本只显示在标签框中。我想在标签框文字中显示全行?怎么做?
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);
答案 0 :(得分:3)
您已同时使用dynamiclabel1.AutoSize = false;
和显式标签尺寸设置,这没有任何意义。
如果要在标签文字中启用自动换行功能,则必须先设置Height
,然后再设置{{1}}。目前它只有14个像素的高度,因此不可能使文本多行。将它增加到约200像素,所有文本将放在标签内几行。
答案 1 :(得分:2)
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);
答案 2 :(得分:0)
如果增加高度并想要更少的行,请将Width属性设置为更大的值;
gethostname
答案 3 :(得分:0)
如果要根据字体/文字大小选择行数,可以执行以下操作:
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);