Reputation: 87
I using following code, When I run below code, Half of the text only displayed in label box. I want display full line in label box text? How to do it?
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);
Upvotes: 3
Views: 27072
Reputation: 1263
Simple way to change size of controls like label/button in win forms:
Right click on
label/button control --> properties --> autosize =false.
Now size can be changed.
Upvotes: 0
Reputation: 314
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);
Upvotes: 4
Reputation: 7703
If you want to select the number of lines depending on font/text size, you can do something like this:
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);
Edit
If what you know is the width you have available and you want your label to adjust height to show all the content, can do this:
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);
Upvotes: 0
Reputation: 2107
If you increase the height, and want less rows, set the Width property to something bigger;
dynamiclabel1.Width = 150;
Upvotes: 1
Reputation: 26846
You have used AutoSize
and explicit label size setting at the same time, this makes no sense.
If you want to enable word wrapping in your label text - you have to set dynamiclabel1.AutoSize = false;
first and then increase its Height
. Currently it has only 14 pixels of height so it is impossible to make text multiline. Increase it to something about 200 pixels and all the text will be placed inside the label in a few lines.
Upvotes: 4