Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
hi every body

how can i make th size of a label flexible ....i mean when i increase its text length ... it doesnt show all of its contents ,, or changing font ....

i tried autosize but no use

hope find a help

thnx
Posted

You can use the
C#
TextRenderer.MeasureText Method
Like...
C#
Font font = new Font("Arial", 14, FontStyle.Regular);
string a = "some text you need to display";
Size size = TextRenderer.MeasureText(a, font);
label1.Size = size;
label1.Text = a;


The
C#
Size size = TextRenderer.MeasureText(a, font);
gets the size of the text in pixel. Change the size of your label,
C#
label1.Size = size;
, and print the text.
 
Share this answer
 
always use percentage (like size="100%") value instead of using fixed pixel value
 
Share this answer
 
We need to override OnTextInput for a label in C#. when we add some text to a label,this method will be called. we can change the size of the label by calculating its width and height of the input text....

using(Graphics g = CreateGraphics())
{
SizeF size = g.MeasureString(text, lbl.Font, 495);
lbl.Height = (int) Math.Ceiling(size.Height);
lbl.Text = text;
}

we can also use TextRenderer.MeasureText() method to get size of the input text.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900