简体   繁体   English

c#自定义面板

[英]c# custom Panel

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 .我希望lbldivider1位于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.当我将此项目添加到某些WinForm项目时,仅当我将自定义面板从一个位置拖到另一个位置时才会看到第二个标签。 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?我该如何解决?

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) (C) 添加时只需交换(确保lbldivider1以 z 顺序lbldivider1在第一位)

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

如果要将标签的位置设置在第二个之上,请使用 ZOrder 属性,如果将一个放在另一个之下,您将选择 TableLayoutPanel 或 FlowLayoutPanel。

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.根据这个问题,似乎 ZOrder 是 C# 中不可用的 VB 属性,但是父Controls集合上有一个SetChildIndex

Try尝试

this.Controls.SetChildIndex(lblAutomatic, 1);
this.Controls.SetChildIndex(lbldivider1, 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM