[英]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.