*Experts* Nerseus Posted August 14, 2003 *Experts* Posted August 14, 2003 I'm 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. I'm 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 I'm 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. Here's the code for the inherited label. I've 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. That's also why the rectangle used after MeasureString has a top position of 0 hard-coded. 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 here's 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 wouldn't be any space at all in any of the strings. 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Gurus* divil Posted August 15, 2003 *Gurus* Posted August 15, 2003 Instead of creating a new StringFormat to work with, start off with StringFormat.GenericTypographic and work from there: StringFormat sf = StringFormat.GenericTypographic; //new StringFormat(); sf.Alignment = StringAlignment.Near; sf.FormatFlags |= StringFormatFlags.NoWrap; Don't ask me why this happens, because I don't know! But I've seen the problem before and happened to know this was the fix for it. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Nerseus Posted August 15, 2003 Author *Experts* Posted August 15, 2003 Excellent, worked great! :) Thanks, Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.