Tom V

Tom V

  • NA
  • 1
  • 2.6k

Flowlayout with a custom control behaves wrong

Dec 28 2010 10:38 AM
Hi,

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

 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
{
    /// <summary>
    /// A custom windows control to display text vertically
    /// </summary>
    [ToolboxBitmap(typeof(VerticalLabel), "VerticalLabel.ico")]
    public class VerticalLabel : Control
    {
        private DrawMode drawMode = DrawMode.BottomUp;
        private bool transparent = false;
        private bool autoSize = true;
        private AnchorStyles anchor = AnchorStyles.Top | AnchorStyles.Left;
        private DockStyle dock = DockStyle.None;

        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(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);
            }
        }

        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; }
        }

        public override bool AutoSize
        {
            get { return autoSize; }
            set
            {
                base.AutoSize = value;
                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();

                Height = this.Margin.Top + textLength.Width + this.Margin.Bottom;
                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 override AnchorStyles Anchor
        {
            get { return anchor; }
            set { base.Anchor = value; anchor = value; }
        }

        public override DockStyle Dock
        {
            get { return dock; }
            set { base.Dock = value; dock = value; }
        }
    }

    public enum DrawMode
    {

        BottomUp = 1,

        TopDown
    }
}


Attachment: IntegerElement.zip