How to prevent Label text from wrapping?

  • Thread starter Thread starter abc
  • Start date Start date
A

abc

This seems like a stupid question, but I can't find the property to prevent
the text in a Label control from wrapping on the next line. Thanks.
 
Try with setting 'AutoSize' property to true.

Thanks,
VijayaKrishna P.
 
Hi,

Make the height of the label lower, just the height of a line, in this case
you will not see the rest of the text. or use AutoSize as pondala suggested.


Cheers,
 
Hi,

Thanks for posting in this group.
If you want to adjust your label to fit all your text to display, the
simplest way is autosize property.
If you want to fix the label's length and cut the wapped text of label
control, you should use Graphics.MeasureString to determine the string's
length.
Sample code like this:

private void button1_Click(object sender, System.EventArgs e)
{
Graphics g=this.CreateGraphics();
Font f=label1.Font;
string labelstr=label1.Text;
char [] char_arr=labelstr.ToCharArray();
int width;
int count=0;
for(int i=0;i<char_arr.Length;i++)
{
width=(int)(g.MeasureString(labelstr.Substring(0,i+1),f).Width);
if(width>=label1.Width)
{
count=i;
break;
}
}
label1.Text=labelstr.Substring(0,count);
}

It works well on my machine.
Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi,

Does our posts make sense to you?
If you still have any concern, please feel free to tell me.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top