Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
{
    /// <summary>
    /// A custom windows control to display text vertically
    /// </summary>
    [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
    }
}
Posted

The flowdirection of the panel is not the problem. If I add several of these controls, some with flowbreak others without flowbreak. Then they are all spaced 2px in x or y direction apart. Not the required place so the components wouldn't overlap anymore.

If some would like it, I could send you the complete project so you can take a look.
 
Share this answer
 
Comments
JOAT-MON 5-Jan-11 2:37am    
You should not use 'Answer' to clarify. Post a comment on my answer so that I will get notified.
Did you set the FlowLayoutPanel.FlowDirection property to TopDown?

this.flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;


[EDIT]

This may have to do with the Region property. Try explicitly setting the Region of the control to encompass the text.

[EDIT]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900