天天看點

Panel控件



命名空間:System.Windows.Forms

程式集:System.Windows.Forms(在 system.windows.forms.dll 中)

[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]

[ComVisibleAttribute(true)]

public class Panel : ScrollableControl

Panel 是一個包含其他控件的控件。可以使用 Panel 來組合控件的集合,例如一組 RadioButton 控件。與其他容器控件(如 GroupBox 控件)一樣,如果 Panel 控件的 Enabled 屬性設定為 false,則也會禁用包含在 Panel 中的控件。

預設情況下,Panel 控件在顯示時沒有任何邊框。可以用 BorderStyle 屬性提供标準或三維的邊框,将窗面闆區與窗體上的其他區域區分開。因為 Panel 控件派生于 ScrollableControl 類,是以可以用 AutoScroll 屬性來啟用 Panel 控件中的滾動條。當 AutoScroll 屬性設定為 true 時,使用所提供的滾動條可以滾動顯示 Panel 中(但不在其可視區域内)的所有控件。

Panel 控件不顯示标題。如果需要與 Panel 類似可顯示标題的控件,請參見 GroupBox 控件

下面的代碼示例建立一個 Panel 控件,并向 Panel 添加一個 Label 和一個 TextBox。Panel 控件顯示有三維邊框,以将 Panel 控件所在的區域與窗體上其他的對象區分開。此示例需要它所定義的方法是從現有窗體中調用的,并且已将 System.Drawing 命名空間添加到該窗體的源代碼中。

public void CreateMyPanel()
{
   Panel panel1 = new Panel();
   TextBox textBox1 = new TextBox();
   Label label1 = new Label();
   
   // Initialize the Panel control.
   panel1.Location = new Point(56,72);
   panel1.Size = new Size(264, 152);
   // Set the Borderstyle for the Panel to three-dimensional.
   panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

   // Initialize the Label and TextBox controls.
   label1.Location = new Point(16,16);
   label1.Text = "label1";
   label1.Size = new Size(104, 16);
   textBox1.Location = new Point(16,32);
   textBox1.Text = "";
   textBox1.Size = new Size(152, 20);

   // Add the Panel control to the form.
   this.Controls.Add(panel1);
   // Add the Label and TextBox controls to the Panel.
   panel1.Controls.Add(label1);
   panel1.Controls.Add(textBox1);
}