Click here to Skip to main content
12,406,124 members (77,731 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# User-controls
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 28-Dec-10 12:28pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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]
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160726.1 | Last Updated 5 Jan 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100