Hi,
can someone tell me why this VerticalLabel doesn't place itself close to the previous controls in this flowlayout container? It seems that the location gets offsetted by the sum of all previous components in the flowlayout container.
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
{
public class VerticalLabel : Control
{
private string labelText;
private DrawMode drawMode = DrawMode.TopDown;
private bool transparent = false;
private bool autoSize = true;
private System.ComponentModel.Container components = new System.ComponentModel.Container();
public VerticalLabel()
{
CreateControl();
AutoSize = true;
SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (!((components == null)))
{
components.Dispose();
}
}
base.Dispose(disposing);
}
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(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(labelText, 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(labelText, Font, labelForeColorBrush, this.Margin.Bottom, this.Margin.Right);
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
public override string Text
{
get { return labelText; }
set { labelText = value;
Invalidate(); }
}
public DrawMode TextDrawMode
{
get { return drawMode; }
set { drawMode = value; }
}
public bool TransparentBackground
{
get { return transparent; }
set { transparent = value; }
}
public override bool AutoSize
{
get { return autoSize; }
set
{
autoSize = value;
VerticalLabel_Resize(null, null);
}
}
private void VerticalLabel_Resize(object sender, System.EventArgs e)
{
if (autoSize)
{
Graphics g = this.CreateGraphics();
Size textLength = g.MeasureString(this.Text, this.Font).ToSize();
this.Height = this.Margin.Top + textLength.Width + this.Margin.Bottom;
this.Width = this.Margin.Left + textLength.Height + this.Margin.Right;
}
}
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
}
}
Displaying the text goes correct, only the Location of the control seems to get changed by some function.
Here is the main code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Elements;
namespace TestApp
{
public partial class Form1 : Form
{
public List<Control> interfaceContainer;
public Form1()
{
InitializeComponent();
interfaceContainer = new List<Control>();
IntegerElement i1, i2;
DoubleElement d1, d2;
StringElement s1, s2;
ComboElement c1, c2;
BoolElement b1, b2;
VerticalLabel v1, v2;
i1 = new IntegerElement();
i2 = new IntegerElement();
d1 = new DoubleElement();
d2 = new DoubleElement();
s1 = new StringElement();
s2 = new StringElement();
c1 = new ComboElement();
c2 = new ComboElement();
b1 = new BoolElement();
b2 = new BoolElement();
v1 = new VerticalLabel();
v2 = new VerticalLabel();
c1.AddItem(new NumberedItem("An 0", 0));
c1.AddItem(new NumberedItem("An 1", 1));
c1.AddItem(new NumberedItem("RPM", 5));
c1.AddItem(new NumberedItem("PCM", 7));
c2.AddItem(new NumberedItem("Kat", 1));
c2.AddItem(new NumberedItem("hond", 100));
c2.AddItem(new NumberedItem("poes", 50));
v1.Text = "test 1 2 3 testinggg";
v1.BackColor = Color.Green;
v2.Text = "v1";
v2.BackColor = Color.Yellow;
interfaceContainer.Add(i1);
interfaceContainer.Add(i2);
interfaceContainer.Add(d1);
interfaceContainer.Add(d2);
interfaceContainer.Add(s1);
interfaceContainer.Add(s2);
interfaceContainer.Add(c1);
interfaceContainer.Add(c2);
interfaceContainer.Add(b1);
interfaceContainer.Add(b2);
interfaceContainer.Add(v1);
interfaceContainer.Add(v2);
foreach (Control uc in interfaceContainer)
{
if (!uc.Name.Equals("VerticalLabel"))
uc.Size = new Size(200, 20);
uc.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold);
flowLayoutPanel.Controls.Add(uc);
flowLayoutPanel.SetFlowBreak(uc, true);
}
}
}
}
If you want I can email you the complete project.
Tom