A new language’s success massively depends on how good the initial set of classes are. Microsoft has done a good job with the base classes for C# (a.k.a. .Net framework) which I think is one of the reasons C# is doing so good (plus the facts that Microsoft is behind it and the familiar C++/Java syntax). Anyways tonight while working on RFS Explorer I had a need for truncating the file path to fit the width of a label (by replacing the extra characters with ellipses). I remember doing this before for another program that I was working on so I started by searching online and after a minute I ended up at this page. The fourth comment is exactly what I was looking for. Below is the C# version in form of a function so it can be easily called or added to a code library.
COULD NOT FIND A LEXER FOR LANGUAGE ‘C#’
///
/// This should be called from the 'Paint' event of the label.
///
/// Label with the text and the size.
/// System.Drawing.Graphics provided in the paint call (e.Graphics).
public void MakePathElliptical_OnPaint(Label lbl, Graphics graphics) {
graphics.Clear(lbl.BackColor, ');
RectangleF rect = new RectangleF(0, 0, lbl.Width, lbl.Height, ');
Brush br = new SolidBrush(lbl.ForeColor, ');
StringFormat sf = new StringFormat(StringFormatFlags.NoWrap, ');
sf.Trimming = StringTrimming.EllipsisPath;
graphics.DrawString(lbl.Text, lbl.Font, br, rect, sf, ');
}
To use this you just have to override the Paint event of the label (this might also work in a textbox with slight modifications) and call this function like below:
COULD NOT FIND A LEXER FOR LANGUAGE ‘C#’
private void lblName_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
winFormUtils.MakePathElliptical_OnPaint(lblName, e.Graphics, ');
}
Below is a screenshot of what label will look like after going through this function.