Groups | Blog | Home
all groups > c# > january 2004 >

c# : How to prevent Label text from wrapping?


abc
1/6/2004 11:55:13 PM
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.

Ignacio Machin ( .NET/ C# MVP )
1/7/2004 8:41:09 AM
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

[quoted text, click to view]

Vijayakrishna Pondala
1/7/2004 6:53:44 PM
Try with setting 'AutoSize' property to true.

Thanks,
VijayaKrishna P.
[quoted text, click to view]

v-jetan NO[at]SPAM online.microsoft.com (
1/8/2004 1:58:07 AM

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.
v-jetan NO[at]SPAM online.microsoft.com (
1/12/2004 12:58:30 AM

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.
AddThis Social Bookmark Button