How to prevent Label text from wrapping?

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.
 
V

Vijayakrishna Pondala

Try with setting 'AutoSize' property to true.

Thanks,
VijayaKrishna P.
 
I

Ignacio Machin \( .NET/ C# MVP \)

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,
 
J

Jeffrey Tan[MSFT]

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.
 
J

Jeffrey Tan[MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top