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.ClientSize);
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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.