繁体   English   中英

c#自定义面板

[英]c# custom Panel

我正在尝试创建一个包含一些按钮和标签的自定义面板。 问题是我无法在代码中正确设置显示顺序。 我有这样的事情:

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

        }

我希望lbldivider1位于lblAutomatic的顶部 当我将此项目添加到某些WinForm项目时,仅当我将自定义面板从一个位置拖到另一个位置时才会看到第二个标签。 但是,当它不移动以及我启动应用程序时,它在设计器中看不到。

我该如何解决?

好的,如果您没有一些隐藏代码,则以下任何一种都应该起作用:

(一种)

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

(二)

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

(C) 添加时只需交换(确保lbldivider1以 z 顺序lbldivider1在第一位)

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

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

根据这个问题,似乎 ZOrder 是 C# 中不可用的 VB 属性,但是父Controls集合上有一个SetChildIndex

尝试

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

暂无
暂无

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

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