By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 853 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

System.Windows.Forms.Label

P: n/a
Hi,

I have two questions related to the Label control.

How do i prevent the label from expand the canvas into 2 lines (word wrap)
??
At the moment i set AutoSize to false (to prevent the word wrap from
happening) and Anchor to Left, Right (to enable the with autosize).

How do i prevent a tool-tip from happening if the text is to large to fit in
the control area ??
At the moment i set AutoEllipse to false to prevent auto tootips, but i do
want AutoEllipse on.

Thank you in advance.
BR
Peter
Oct 15 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hi Peter,
How do i prevent the label from expand the canvas into 2 lines (word
wrap) ??

Do you mean that you'd like the text to be shown in one line within the
Label without clipping the text? If so, you could set the AutoSize property
of the Label to true or adjust the size of the Label to the preferred size.

As the second option, you could handle the TextChanged event of the Label
and call the Control.GetPreferredSize method to get the preferred size of
the Label. The following is a sample:

void label1_TextChanged(object sender, EventArgs e)
{
Size txtsize = this.label1.GetPreferredSize(this.ClientSize);
this.label1.Width = txtsize.Width;
this.label1.Height = txtsize.Height;
}
How do i prevent a tool-tip from happening if the text is to large to fit
in the control area ??

It is the internal implementation of the Label class that shows a ToolTip
when the AutoEllipsis property is set to true and the text in the label
extends beyond the width of the label control.

A solution to prevent the ToolTip from showing if the text extends beyond
the width of the label control but still have the ellipsis showing at the
end of the text is to set the AutoEllipsis property to false and draw the
text in the label control by ourselves in the case that the text is too
large to fit in the label control.

We could handle the Paint event of the label control to do this. The
following is a sample:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.label1.Paint += new PaintEventHandler(label1_Paint);
this.label1.AutoEllipsis = false;
}

void label1_Paint(object sender, PaintEventArgs e)
{
Size preferredsize =
this.label1.GetPreferredSize(this.label1.ClientSiz e);
if(this.label1.ClientSize.Width < preferredsize.Width ||
this.label1.ClientSize.Height < preferredsize.Height )
{
// erase the text drawn by system
using (Brush b = new SolidBrush(this.label1.BackColor))
{
e.Graphics.FillRectangle(b, e.ClipRectangle);
}
int rowheight = this.label1.Font.Height;
int rowcount = e.ClipRectangle.Height / rowheight;
string text = this.label1.Text;
StringBuilder drawingtext = new StringBuilder();
int indicator = 0;
for (int i = 0; i < rowcount; i++)
{
Size txtsize = new Size();
drawingtext.Length=0;
while(txtsize.Width < e.ClipRectangle.Width &&
indicator < text.Length )
{
drawingtext.Append(text[indicator++]);
txtsize =
TextRenderer.MeasureText(drawingtext.ToString(), this.label1.Font);
}
if (i == rowcount - 1 && indicator < text.Length)
{
drawingtext.Remove(drawingtext.Length - 1, 1);
drawingtext.Append("...");
}
if (drawingtext.ToString() != "")
{
using (Brush b = new
SolidBrush(this.label1.ForeColor))
{
e.Graphics.DrawString(drawingtext.ToString(),
this.label1.Font, b, new PointF(e.ClipRectangle.X, e.ClipRectangle.Y + i *
rowheight));
}
}
if (indicator == text.Length)
{
break;
}
}
}
}
}

The above code works well on my side. Please try it on your side to see if
it is what you want and let me know the result.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 16 '07 #2

P: n/a
Hi Linda and thanks.

Yes - i don't want the text to be shown in two lines.
If i understand you right, your solution is to allow the label width to
expand until the text is in one line, right ??
The problem is that it isn't possible to expand the label so the text is
drawn in one line (the parent is to small), so AutoScroll is not an option.
The rest of the stuff your are writing about (the tooltip problem) is solved
if you dont have AutoScroll on because the second line isn't visible then.

The problem by using that method (AutoScroll = false) is that the text
clipping occur between words which could leave the visible line allmost
empty (the next word is forced to the second line).
Eg."The verylongwordinthesecondline" could be "The" in the first visible
line and "verylongwordonthesecondline" in the invisible second line.

Anyway, it doesn't matter where the wordwrap happen, the primary issue was
the tooltip problem - which is solved by AutoScroll or by the OnPaint method
of yours.

The first part, of my question, is still open but the second part is closed.

BR
Peter
Oct 17 '07 #3

P: n/a
Hi Peter,

Thank you for your feedback!

About the first part of your question, your understanding of my solution is
correct. To ensure the text to display in one line within a Label control,
we could only either set the AutoSize property of the Label to true or
expand the width of the Label control to the preferred size.
The problem is that it isn't possible to expand the label so the text is
drawn in one line (the parent is to small),

Could you please explain more about your scenario and what you really want?

Sincerely,
Linda Liu
Microsoft Online Community Support

Oct 18 '07 #4

P: n/a
Hi Linda,

I think i understand you now - you set AutoScroll to false and expand the
width beyond the size of the parent, right ??

/Peter

Oct 18 '07 #5

P: n/a
Hi Peter,

Thank you for your prompt reply!

The System.Windows.Forms.Label class doesn't have the AutoScroll property.

I mean to ensure the text to display in one line within a Label control, we
could set the AutoSize property to true or expand the width of the Label
control to the preferred size manually if the AutoSize property is set to
false.
you set AutoScroll to false and expand the width beyond the size of the
parent, right ??

What's the parent you refer to, the form that contains the Label control?
We can not expand the width of the Label control beyond the bound of its
parent form. The parent form always clips the part of its child controls
that lay out of the bound of it.

Sincerely,
Linda Liu
Microsoft Online Community Support

Oct 19 '07 #6

P: n/a
Hi Linda,

Sorry, i meant AutoSize - not AutoScroll - my mistake.
Does it make sense then ??

/Peter
Oct 19 '07 #7

P: n/a
Hi Peter,

Thank you for your quick response!

Yes, your understanding is almost correct.

Let me summarize this post:
You have two questions:
1. How to prevent the text from extending into 2 lines within the Label
control?
2. How to prevent the tool-tip from showing if the text extends beyond the
width of the Label control?

Now, we have solved the second question by setting the AutoEllipsis
property to false and drawing the text within the label control by
ourselves.

About the first question, my solution is to set the AutoSize property to
true OR adjust the width of the label control to the preferred value if
necessary.

If the first question is still not solved, please let me know what you want
in detail.

Sincerely,
Linda Liu
Microsoft Online Community Support

Oct 19 '07 #8

P: n/a
Hi Linda,

I think the two questions has been answered/solved, but i don't agree with
you in the solution about the AutoSize property.
Setting AutoSize to true, will allow the label control to expand itself into
two lines if necessay. Isn't that what we are trying to prevent ??

Best Regards
Peter

Oct 23 '07 #9

P: n/a
Hi Peter,

Thank you for your reply!
Setting AutoSize to true, will allow the label control to expand itself
into two lines if necessay

When the AutoSize property of a Label control is set to true, the Label
control will never expand itself into two lines even if the parent form's
width is not big enough.

Have you tested this behavior on your side?
Sincerely,
Linda Liu
Microsoft Online Community Support

Oct 24 '07 #10

P: n/a
Hi Linda,

Well, its true what you are saying.
If you place a label on a panel and set AutoSize to true, the text will be
written in one line.

I have a setup a little bit differently from yours:
I have a TableLayoutPanel with some labels placed in the cells. Setting
AutoSize will cause the labels to expand to the entire cell canvas if
necessay.

I guess thats why we didn't experience the same behavior :-)

BR
Peter

Oct 31 '07 #11

P: n/a
Hi Peter,

Thank you for pointing this out!

If a Label with AutoSize property set to true is placed in a
TableLayoutPanel and the size type of the column where the label resides is
Absolute or Percent but the AutoSize property of the TableLayoutPanel is
false, the text in the Label may be written to the second line.

You can set the size type of the column to AutoSize to ensure the text in
the Label to always be written in one line.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Nov 1 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.