Hi,
can someone help me debug this on. If I add several instances of following class derived from a UserControl to a FlowLayout
and add a FlowBreak to each component, they will all be placed at point(0,0).
I want them to appear under each other. Can some help me find the problem with this?
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;
namespace Elements
{
[ToolboxBitmap(typeof(VerticalLabel), "VerticalLabel.ico")]
public class VerticalLabel : UserControl
{
private DrawMode drawMode = DrawMode.TopDown;
private bool transparent = false;
private void VerticalLabel_Resize(object sender, System.EventArgs e)
{
if (AutoSize)
{
Graphics g = CreateGraphics();
Size textLength = g.MeasureString(Text, Font).ToSize();
this.Height = Margin.Top + textLength.Width + Margin.Bottom;
this.Width = Margin.Left + textLength.Height + Margin.Right;
}
}
public VerticalLabel()
{
CreateControl();
Name = "VerticalLabel";
Location = new Point();
Margin = new Padding();
AutoSize = true;
AutoSizeMode = AutoSizeMode.GrowAndShrink;
BackColor = Color.Transparent;
Margin = new Padding(1);
SetStyle(ControlStyles.Opaque, true);
Resize += new EventHandler(VerticalLabel_Resize);
}
protected override void OnPaint(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(ForeColor);
base.OnPaint(e);
labelControlWidth = Size.Width;
labelControlHeight = 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(Text, Font, labelForeColorBrush, Margin.Top, Margin.Left);
}
else
{
labelTransformX = labelControlWidth;
labelTransformY = labelControlHeight;
e.Graphics.TranslateTransform(labelControlWidth, 0);
e.Graphics.RotateTransform(90);
e.Graphics.DrawString(Text, Font, labelForeColorBrush, Margin.Bottom, Margin.Right);
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
public override string Text
{
get { return base.Text; }
set
{
base.Text = value;
Invalidate();
}
}
public DrawMode TextDrawMode
{
get { return drawMode; }
set { drawMode = value; }
}
public bool TransparentBackground
{
get { return transparent; }
set { transparent = value; }
}
protected override void OnTextChanged(EventArgs e)
{
VerticalLabel_Resize(null, null);
}
protected override void OnFontChanged(EventArgs e)
{
VerticalLabel_Resize(null, null);
}
protected override void OnAutoSizeChanged(EventArgs e)
{
VerticalLabel_Resize(null, null);
}
}
public enum DrawMode
{
BottomUp = 1,
TopDown
}
}