-1

I'm trying to create a custom panel containing some buttons and labels. The problem is that I can't set displaying order properly in code. I have something like this:

 public partial class Pallete1 : Panel
    {
        private Label lblAutomatic;       
        private Label lbldivider1;

        public Pallete1():base()
        {
            InitializeComponent();
            this.lblAutomatic = new Label();
            this.lbldivider1 = new Label();

            this.lblAutomatic.Size = new Size(182,21);
            this.lblAutomatic.Location = new Point(0, 0);
            this.lblAutomatic.ForeColor = Color.FromArgb(0, 0, 64);
            this.lblAutomatic.TextAlign = ContentAlignment.MiddleCenter;
            this.lblAutomatic.Text = "Automatycznie";
            this.lblAutomatic.Font = new Font("Microsoft Sans Serif", 8);

            this.lbldivider1.Size = new Size(2,22);
            this.lbldivider1.Location = new Point(26, 0);
            this.lbldivider1.ForeColor = SystemColors.ControlText;
            this.lbldivider1.BackColor = SystemColors.ButtonHighlight;
            this.lbldivider1.BorderStyle = BorderStyle.Fixed3D;


            this.Size = new Size(182, 184);
            this.BackColor = SystemColors.ButtonHighlight;
            this.BorderStyle = BorderStyle.FixedSingle;
            this.Controls.AddRange(new Control[]{this.lblAutomatic,this.lbldivider1});

        }

I would like lbldivider1 to be on the top of lblAutomatic. When I add this item to some WinForm projects, this second label is only seen when I drag my custom panel from one place to another. However it's not seen in the designer when it's not moving and also when I'm launching the application.

How can I fix it?

Paweł Poręba
  • 1,084
  • 1
  • 14
  • 37

3 Answers3

1

If you want to set label's position over second one, use ZOrder property, in case of putting one under another, you would choose TableLayoutPanel or FlowLayoutPanel.

Szymon D
  • 441
  • 2
  • 13
1

Ok, if you don't have some hidden code, any of the following should work:

(A)

this.Controls.AddRange(new Control[]{this.lblAutomatic,this.lbldivider1});
this.lbldivider1.BringToFront();

(B)

this.Controls.AddRange(new Control[]{this.lblAutomatic,this.lbldivider1});
this.lblAutomatic.SendToBack();

(C) Simply swap when adding (make sure lbldivider1 goes first in z order)

this.Controls.AddRange(new Control[]{this.lbldivider1, this.lblAutomatic});
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • thanks! I appreciate this feedback a lot. Of course I was doing a stupid thing, I mean I've added `this.lbldivider1.BringToFront()` before adding it to panel's list of controls. Really thanks, sometimes such stupid mistakes steal lot of time:) – Paweł Poręba Oct 30 '15 at 16:34
0

As per this question it seems ZOrder is a VB property not available in C#, however there is a SetChildIndex on the parent's Controls collection.

Try

this.Controls.SetChildIndex(lblAutomatic, 1);
this.Controls.SetChildIndex(lbldivider1, 2);
Community
  • 1
  • 1
Ciara
  • 384
  • 2
  • 12