Nerseus
Danner
Im creating an inherited Label control to hold a string. The inherited version will draw the text in two colors, the first portion of the string in the standard Foreground color of the control, the last 6 digits will be a special color.
Im handling the drawing in an overriden OnPaint event by figuring out the two strings, creating appropriate Brush and Font objects, and drawing the strings.
The problem Im noticing is that MeasureString seems to be off by a varying amount depending on the letters (both the contents and number) used in the first part of the string.
Heres the code for the inherited label. Ive turned off wrapping as I know the length of the strings will never be greater than the size of the Label in my production code. Thats also why the rectangle used after MeasureString has a top position of 0 hard-coded.
And heres some code to create 3 labels, and set their Text. The space between the first and second portions of the string is a different size. I would HOPE that there wouldnt be any space at all in any of the strings.
Any ideas?
-Nerseus
Im handling the drawing in an overriden OnPaint event by figuring out the two strings, creating appropriate Brush and Font objects, and drawing the strings.
The problem Im noticing is that MeasureString seems to be off by a varying amount depending on the letters (both the contents and number) used in the first part of the string.
Heres the code for the inherited label. Ive turned off wrapping as I know the length of the strings will never be greater than the size of the Label in my production code. Thats also why the rectangle used after MeasureString has a top position of 0 hard-coded.
C#:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace WinTest
{
public class VINLabel : Label
{
public VINLabel()
{
}
[Description("The Short VIN portion of the VIN, readonly"), Category("Appearance")]
public string TextShortVIN
{
get
{
int shortVinPos = (this.Text.Length >= 6 ? this.Text.Length - 6 : 0);
return this.Text.Substring(shortVinPos);
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
int shortVinPos = (this.Text.Length >= 6 ? this.Text.Length - 6 : 0);
string shortVIN = this.Text.Substring(shortVinPos);
string remainingVIN = this.Text.Substring(0, shortVinPos);
Graphics g = e.Graphics;
Brush remainingVINBrush = Brushes.Black;
Brush shortVINBrush = Brushes.Blue;
Font remainingVINFont = this.Font;
Font shortVINFont = new Font(remainingVINFont, FontStyle.Bold);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.FormatFlags |= StringFormatFlags.NoWrap;
// Clear the area of the control
e.Graphics.DrawRectangle(new Pen(this.BackColor), this.ClientRectangle);
Rectangle r = this.ClientRectangle;
// Draw the first part of the text
e.Graphics.DrawString(remainingVIN, remainingVINFont, remainingVINBrush, r, sf);
// Get the position (r) where the shortVIN should be drawn from
SizeF layoutArea = new SizeF(r.Width, r.Height);
SizeF textSize = g.MeasureString(remainingVIN, remainingVINFont, layoutArea, sf);
int width = (int)textSize.Width;
r.Location = new Point(width, 0);
// Draw the short VIN
e.Graphics.DrawString(shortVIN, shortVINFont, shortVINBrush, r, sf);
}
}
}
And heres some code to create 3 labels, and set their Text. The space between the first and second portions of the string is a different size. I would HOPE that there wouldnt be any space at all in any of the strings.
C#:
VINLabel vl = new VINLabel();
vl.Size = new Size(120, 20);
vl.Location = new Point(0, 0);
this.Controls.Add(vl);
vl.Text = "ABCDEFGHIJKL123456";
VINLabel v2 = new VINLabel();
v2.Size = new Size(120, 20);
v2.Location = new Point(0, 21);
this.Controls.Add(v2);
v2.Text = "ABC123456";
VINLabel v3 = new VINLabel();
v3.Size = new Size(120, 20);
v3.Location = new Point(0, 42);
this.Controls.Add(v3);
v3.Text = "abc123456";
Any ideas?
-Nerseus