can someone tell me why this VerticalLabel doesn't place itself close to the previous controls in the flowlayout container? It seems that the location gets offsetted by it's own height. Somewhere the location of this component is changed and I can seem to find where or why.
Can some help me with this. Attached you can find the complete project file. The green and the yellow text should directly under the other controls.
Tom
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
namespaceElements
{
////// A custom windows control to display text vertically///
[ToolboxBitmap(typeof(VerticalLabel), "VerticalLabel.ico")]
publicclassVerticalLabel : Control
{
private DrawMode drawMode = DrawMode.BottomUp;
privatebool transparent = false;
privatebool autoSize = true;
private AnchorStyles anchor = AnchorStyles.Top | AnchorStyles.Left;
private DockStyle dock = DockStyle.None;
private System.ComponentModel.Container components = new System.ComponentModel.Container();
publicVerticalLabel()
{
CreateControl();
AutoSize = true;
SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
}
protectedoverridevoidDispose(bool disposing)
{
if (disposing)
{
if (!((components == null)))
{
components.Dispose();
}
}
base.Dispose(disposing);
}
protectedoverridevoidOnPaint(System.Windows.Forms.PaintEventArgs e)
{
float labelControlWidth;
float labelControlHeight;
float labelTransformX;
float labelTransformY;
Color controlBackColor = BackColor;
Pen labelBorderPen;
SolidBrush labelBackColorBrush;
if (transparent)
{
labelBorderPen = new Pen(Color.Empty, 0);
labelBackColorBrush = new SolidBrush(Color.Empty);
}
else
{
labelBorderPen = new Pen(controlBackColor, 0);
labelBackColorBrush = new SolidBrush(controlBackColor);
}
SolidBrush labelForeColorBrush = new SolidBrush(base.ForeColor);
base.OnPaint(e);
labelControlWidth = this.Size.Width;
labelControlHeight = this.Size.Height;
e.Graphics.DrawRectangle(labelBorderPen, 0, 0, labelControlWidth, labelControlHeight);
e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, labelControlWidth, labelControlHeight);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
if (this.TextDrawMode == DrawMode.BottomUp)
{
labelTransformX = 0;
labelTransformY = labelControlHeight;
e.Graphics.TranslateTransform(labelTransformX, labelTransformY);
e.Graphics.RotateTransform(270);
e.Graphics.DrawString(base.Text, Font, labelForeColorBrush, this.Margin.Top, this.Margin.Left);
}
else
{
labelTransformX = labelControlWidth;
labelTransformY = labelControlHeight;
e.Graphics.TranslateTransform(labelControlWidth, 0);
e.Graphics.RotateTransform(90);
e.Graphics.DrawString(base.Text, Font, labelForeColorBrush, this.Margin.Bottom, this.Margin.Right);
}
}
protectedoverride CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
publicoverridestring Text
{
get { returnbase.Text; }
set
{
base.Text = value;
Invalidate();
}
}
public DrawMode TextDrawMode
{
get { return drawMode; }
set { drawMode = value; }
}
publicbool TransparentBackground
{
get { return transparent; }
set { transparent = value; }
}
publicoverridebool AutoSize
{
get { return autoSize; }
set
{
base.AutoSize = value;
autoSize = value;
VerticalLabel_Resize(null, null);
}
}
privatevoidVerticalLabel_Resize(object sender, System.EventArgs e)
{
if (autoSize)
{
Graphics g = this.CreateGraphics();
Size textLength = g.MeasureString(this.Text, this.Font).ToSize();
Height = this.Margin.Top + textLength.Width + this.Margin.Bottom;
Width = this.Margin.Left + textLength.Height + this.Margin.Right;
}
}
protectedoverridevoidOnTextChanged(EventArgs e)
{
VerticalLabel_Resize(null, null);
}
protectedoverridevoidOnFontChanged(EventArgs e)
{
VerticalLabel_Resize(null, null);
}
protectedoverridevoidOnAutoSizeChanged(EventArgs e)
{
VerticalLabel_Resize(null, null);
}
publicoverride AnchorStyles Anchor
{
get { return anchor; }
set { base.Anchor = value; anchor = value; }
}
publicoverride DockStyle Dock
{
get { return dock; }
set { base.Dock = value; dock = value; }
}
}
publicenum DrawMode
{
BottomUp = 1,
TopDown
}
}
Answers
Know the answer? Post it. Somebody with the same question will find it here.
Answers
Know the answer? Post it. Somebody with the same question will find it here.