I don't know much about software development. I am trying to learn with it... I am recently stuck at a place where i don't know How to text wrap with label in winforms???? Can someone Help Please...??
Printable View
I don't know much about software development. I am trying to learn with it... I am recently stuck at a place where i don't know How to text wrap with label in winforms???? Can someone Help Please...??
Here control is a customized label that fits its height automatically. To add a new class to its project, to stick the code that appears next and to construct its project. Now it can loosen to a GrowLabel the control of the part superior of the toolbox in his form.
Code:using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
public class GrowLabel : Label {
private bool mGrowing;
public GrowLabel() {
this.AutoSize = false;
}
private void resizeLabel() {
if (mGrowing) return;
try {
mGrowing = true;
Size sz = new Size(this.Width, Int32.MaxValue);
sz = TextRenderer.MeasureText(this.Text, this.Font, sz, TextFormatFlags.WordBreak);
this.Height = sz.Height;
}
finally {
mGrowing = false;
}
}
protected override void OnTextChanged(EventArgs e) {
base.OnTextChanged(e);
resizeLabel();
}
protected override void OnFontChanged(EventArgs e) {
base.OnFontChanged(e);
resizeLabel();
}
protected override void OnSizeChanged(EventArgs e) {
base.OnSizeChanged(e);
resizeLabel();
}
}
You can give suitable size of label using a Graphics object and is MeasureString method:
Code:using (System.Drawing.Graphics g = myLabel.CreateGraphics())
{
myLabel.Width = (int) g.MeasureString(myLabel.Text, myLabel.Font).Width + 10;
}
Did you look for the similar Threads made here. Everytime you make a thread you must first look for the similar threads... I have given you some links below for that similar threads. Now just have a look to find your solution there...
http://forums.techarena.in/software-...ent/971830.htm
http://forums.techarena.in/microsoft-project/615350.htm