C#中Panel控件应用实例
Panel控件
1.功能
Panel控件用于为其他控件提供可识别的分组。开发Windows应用程序时,通常使用Panel控件按功能细分窗体。在窗体设计时,所有控件都可以自由移动,而当移动Panel控件时,它包含的所有控件也将随着移动。图1为Panel控件。
图1 Panel控件
2.属性
Panel控件常用属性及说明如表1所示。
表1 Panel控件常用属性及说明
下面详细介绍BackColor属性,此属性用来获取或设置控件的背景色。
语法:
public virtual Color BackColor { get; set; }
属性值:表示控件背景色的Color。默认为DefaultBackColor属性的值。
示例
BackColor属性的使用
本示例中,当程序运行时,单击【颜色】按钮,面板显示背景色。示例运行结果如图2所示。
图2 BackColor属性的使用
程序主要代码如下:
private void button1_Click(object sender, EventArgs e)
{
this.panel1.BackColor = Color.Gray;
}
完整程序代码如下:本教程来自http://www.isstudy.com/
★★★★★主程序文件完整程序代码网站源代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _8_20
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmPanel());
}
}
}
★★★★★frmPanel窗体设计文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _8_20
{
public partial class frmPanel : Form
{
public frmPanel()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.panel1.BackColor = Color.Gray;
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
private void frmPanel_Load(object sender, EventArgs e)
{
}
}
}
★★★★★frmPanel窗体代码文件完整程序代码网站源代码★★★★★
namespace _8_20
{
partial class frmPanel
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.button4 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.checkBox1);
this.panel1.Controls.Add(this.button4);
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 100);
this.panel1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;