In my app i will get text from server whose length is unknown. can some one give idea for how to change the height of label so that the text will not get cutted off if is larger than the length of label.
Asked
Active
Viewed 790 times
2 Answers
1
Use Graphics.MeasureString
. Here's a simplified example:
public class MyForm : Form
{
private string m_text;
public string NewLabelText
{
get { return m_text; }
set
{
m_text = value;
this.Refresh();
}
}
protected override void OnPaint(PaintEventArgs e)
{
if (NewLabelText != null)
{
var size = e.Graphics.MeasureString(NewLabelText, label1.Font);
label1.Width = (int)size.Width;
label1.Height = (int)size.Height;
label1.Text = NewLabelText;
NewLabelText = null;
}
base.OnPaint(e);
}
}

ctacke
- 66,480
- 18
- 94
- 155
-
But this code changes the width and height. What about changing only the height (add a 'row') ask the question stated the width should remain the same? – josef Dec 23 '12 at 09:10
-
Adding line wrap is a different story - the OP didn't ask for that. If that's what's desired, I have a solution for that as well, but it basically requires creating your own Control and handling the text drawing manually. Not hard, but a substantially different answer than just checking for size. – ctacke Dec 23 '12 at 15:24
-
@ctacke in your solution label1.Width is getting value which is larger than mobile width as a result again text is getting cutted. If i use josef solution i am getting multiple line, but i am facing one issue. If i type long string without space then text is not getting wrapped. for example: "ddddddddddddddddddd" - will get cutted. Looks like creating own control may solve my issue. Can you kindly post that code. – Lydia Dec 24 '12 at 05:37
-
1If it has no spaces and the font isn't fixed width, you'll have to test and reduce to get the right number of characters ("wwwwww" will get cropped differently than "lllllll"). You'd have to estimate with an average character width, clip at that point and then use MeasureString to validate if it fits or not. – ctacke Dec 24 '12 at 13:47
0
Using ctacke's solution adopted to the question (constant width of label):
protected override void OnPaint(PaintEventArgs e)
{
if (NewLabelText != null)
{
//get the width and height of the text
var size = e.Graphics.MeasureString(NewLabelText, label1.Font);
if(size.Width>label1.Width){
//how many lines are needed to display the text
int iLines = (int)(System.Math.Round((size.Width / label1.Width)+.5));
//multiply with using the normal height of a one line text
//label1.Height=iLines*label1.PreferredHeight; //preferredHeight not supported by CF
label1.Height=(int)(iLines*size.Height*1.1); // add some gutter
}
label1.Text = NewLabelText;
NewLabelText = null;
}
base.OnPaint(e);
}
I was unable to test that with CF. Possibly PreferredHeight is not available in CF, if so, use label1.height instead.

josef
- 5,951
- 1
- 13
- 24
-
i am getting multiple line, but i am facing one issue. If i type long string without space then text is not getting wrapped. for example: "ddddddddddddddddddd" - will get cutted. Is there any way to manually check and draw the character in screen. – Lydia Dec 24 '12 at 05:40
-
When you like to start your own text wrapping you are going to implement your own drawing. I would prefer to use a TextBox and offer the user a scroll bar instead of starting my own text wrapping. What should the user think, when he/she sees a long term broken into lines, for example:
`\MyVeryLongSubdirPathName`
displayed as `\MyVeryLong
SubDirPa
thName`
? (damned, no line break support in comments) – josef Dec 25 '12 at 07:05