Label 标签控件

本教程将详细介绍C# Windows Forms应用程序中Label标签控件的使用方法,包括基本属性设置、事件处理、样式定制等内容,帮助您掌握标签控件的各种应用场景。

1. 概述

Label控件是Windows Forms中最常用的控件之一,用于显示文本信息,通常用于标识其他控件的用途或提供说明信息。Label控件是只读的,用户无法直接编辑其内容,主要用于展示静态文本。

2. 创建Label控件

在Windows Forms应用程序中创建Label控件有两种主要方法:

2.1 通过设计器创建

  1. 打开Visual Studio,创建一个Windows Forms项目
  2. 在工具箱中找到Label控件
  3. 将Label控件拖放到窗体上
  4. 使用属性窗口设置Label的属性

2.2 通过代码创建

示例:通过代码创建Label控件

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        CreateLabel();
    }

    private void CreateLabel()
    {
        // 创建Label控件
        Label label = new Label();
        
        // 设置Label属性
        label.Text = "欢迎使用Label控件";
        label.Location = new Point(50, 50);
        label.Size = new Size(200, 30);
        label.Name = "lblWelcome";
        
        // 添加点击事件处理程序
        label.Click += new EventHandler(label_Click);
        
        // 将Label添加到窗体
        this.Controls.Add(label);
    }

    private void label_Click(object sender, EventArgs e)
    {
        Label label = (Label)sender;
        MessageBox.Show($"你点击了标签:{label.Text}");
    }
}

3. Label控件的常用属性

Label控件有许多属性可以设置,以下是一些常用的属性:

属性 描述 示例值
Text 标签上显示的文本 "用户名:"
Name 标签的名称,用于在代码中引用 "lblUsername"
Location 标签在窗体上的位置 new Point(50, 50)
Size 标签的大小 new Size(100, 30)
Enabled 标签是否可用 true/false
Visible 标签是否可见 true/false
AutoSize 标签是否自动调整大小以适应文本 true/false
TextAlign 文本的对齐方式 ContentAlignment.MiddleLeft
BackColor 背景颜色 Color.White
ForeColor 文本颜色 Color.Black
Font 文本字体 new Font("微软雅黑", 10, FontStyle.Bold)
BorderStyle 边框样式 BorderStyle.None
Image 标签上显示的图像 Image.FromFile("icon.png")
ImageAlign 图像的对齐方式 ContentAlignment.MiddleLeft
ImageIndex 图像列表中图像的索引 0
ImageList 包含要显示的图像的图像列表 imageList1
TextImageRelation 文本和图像的相对位置 TextImageRelation.ImageBeforeText
UseMnemonic 是否将&符号解释为助记键前缀 true/false
TabIndex 标签的Tab键顺序 1
TabStop 标签是否可以通过Tab键获得焦点 false
Tag 存储与标签关联的自定义数据 任意对象

4. Label控件的常用事件

Label控件有多个事件,以下是最常用的事件:

事件 描述 示例
Click 单击标签时触发 private void label1_Click(object sender, EventArgs e) { }
DoubleClick 双击标签时触发 private void label1_DoubleClick(object sender, EventArgs e) { }
MouseEnter 鼠标进入标签时触发 private void label1_MouseEnter(object sender, EventArgs e) { }
MouseLeave 鼠标离开标签时触发 private void label1_MouseLeave(object sender, EventArgs e) { }
MouseDown 鼠标在标签上按下时触发 private void label1_MouseDown(object sender, MouseEventArgs e) { }
MouseUp 鼠标在标签上释放时触发 private void label1_MouseUp(object sender, MouseEventArgs e) { }
MouseMove 鼠标在标签上移动时触发 private void label1_MouseMove(object sender, MouseEventArgs e) { }
KeyDown 标签获得焦点时按下键盘按键触发 private void label1_KeyDown(object sender, KeyEventArgs e) { }
KeyPress 标签获得焦点时按下并释放键盘按键触发 private void label1_KeyPress(object sender, KeyPressEventArgs e) { }
KeyUp 标签获得焦点时释放键盘按键触发 private void label1_KeyUp(object sender, KeyEventArgs e) { }
EnabledChanged 标签的Enabled属性值改变时触发 private void label1_EnabledChanged(object sender, EventArgs e) { }
VisibleChanged 标签的Visible属性值改变时触发 private void label1_VisibleChanged(object sender, EventArgs e) { }

5. Label控件的应用场景

5.1 标识其他控件

示例:使用Label标识文本框

// 创建标签
Label lblName = new Label();
lblName.Text = "姓名:";
lblName.Location = new Point(50, 50);
lblName.Size = new Size(80, 30);
lblName.TextAlign = ContentAlignment.MiddleRight;
this.Controls.Add(lblName);

// 创建文本框
TextBox txtName = new TextBox();
txtName.Location = new Point(140, 50);
txtName.Size = new Size(200, 30);
this.Controls.Add(txtName);

5.2 显示静态信息

示例:使用Label显示静态信息

// 创建标签显示标题
Label lblTitle = new Label();
lblTitle.Text = "用户登录";
lblTitle.Location = new Point(150, 20);
lblTitle.Size = new Size(200, 40);
lblTitle.Font = new Font("微软雅黑", 16, FontStyle.Bold);
lblTitle.TextAlign = ContentAlignment.MiddleCenter;
this.Controls.Add(lblTitle);

// 创建标签显示版权信息
Label lblCopyright = new Label();
lblCopyright.Text = "© 2024 C#学习网站";
lblCopyright.Location = new Point(150, 300);
lblCopyright.Size = new Size(200, 30);
lblCopyright.Font = new Font("微软雅黑", 9);
lblCopyright.ForeColor = Color.Gray;
lblCopyright.TextAlign = ContentAlignment.MiddleCenter;
this.Controls.Add(lblCopyright);

5.3 显示动态信息

示例:使用Label显示动态信息

// 创建标签显示时间
Label lblTime = new Label();
lblTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
lblTime.Location = new Point(50, 50);
lblTime.Size = new Size(200, 30);
this.Controls.Add(lblTime);

// 创建定时器更新时间
Timer timer = new Timer();
timer.Interval = 1000; // 1秒
timer.Tick += (sender, e) =>
{
    lblTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
};
timer.Start();

5.4 显示图片和文本

示例:使用Label显示图片和文本

// 创建带图片的标签
Label lblWithImage = new Label();
lblWithImage.Text = "带图片的标签";
lblWithImage.Location = new Point(50, 50);
lblWithImage.Size = new Size(200, 50);
try
{
    // 注意:需要在项目中添加一个icon.png图像文件
    lblWithImage.Image = Image.FromFile("icon.png");
    lblWithImage.ImageAlign = ContentAlignment.MiddleLeft;
    lblWithImage.TextAlign = ContentAlignment.MiddleRight;
    lblWithImage.TextImageRelation = TextImageRelation.ImageBeforeText;
}
catch { }
this.Controls.Add(lblWithImage);

5.5 超链接效果

示例:使用Label创建超链接效果

// 创建超链接标签
Label lblLink = new Label();
lblLink.Text = "访问C#学习网站";
lblLink.Location = new Point(50, 50);
lblLink.Size = new Size(200, 30);
lblLink.ForeColor = Color.Blue;
lblLink.Cursor = Cursors.Hand;
lblLink.Font = new Font(lblLink.Font, FontStyle.Underline);
lblLink.Click += (sender, e) =>
{
    System.Diagnostics.Process.Start("https://www.c.com");
};
this.Controls.Add(lblLink);

6. Label控件的样式定制

6.1 自定义字体和颜色

示例:自定义字体和颜色

// 创建自定义样式的标签
Label lblCustom = new Label();
lblCustom.Text = "自定义样式标签";
lblCustom.Location = new Point(50, 50);
lblCustom.Size = new Size(200, 40);

// 设置字体
lblCustom.Font = new Font("微软雅黑", 12, FontStyle.Bold | FontStyle.Italic);

// 设置颜色
lblCustom.ForeColor = Color.White;
lblCustom.BackColor = Color.FromArgb(52, 152, 219);

// 设置边框
lblCustom.BorderStyle = BorderStyle.FixedSingle;

// 设置文本对齐
lblCustom.TextAlign = ContentAlignment.MiddleCenter;

this.Controls.Add(lblCustom);

6.2 边框样式

示例:设置边框样式

// 创建带边框的标签
Label lblBorder = new Label();
lblBorder.Text = "带边框的标签";
lblBorder.Location = new Point(50, 50);
lblBorder.Size = new Size(200, 40);

// 设置边框样式
lblBorder.BorderStyle = BorderStyle.FixedSingle;

// 或者设置为3D边框
// lblBorder.BorderStyle = BorderStyle.Fixed3D;

// 设置文本对齐
lblBorder.TextAlign = ContentAlignment.MiddleCenter;

this.Controls.Add(lblBorder);

6.3 鼠标悬停效果

示例:添加鼠标悬停效果

// 创建带悬停效果的标签
Label lblHover = new Label();
lblHover.Text = "鼠标悬停效果";
lblHover.Location = new Point(50, 50);
lblHover.Size = new Size(200, 40);
lblHover.BackColor = Color.LightGray;
lblHover.TextAlign = ContentAlignment.MiddleCenter;
lblHover.Cursor = Cursors.Hand;

// 添加鼠标悬停事件
lblHover.MouseEnter += (sender, e) =>
{
    lblHover.BackColor = Color.LightBlue;
    lblHover.ForeColor = Color.White;
};

lblHover.MouseLeave += (sender, e) =>
{
    lblHover.BackColor = Color.LightGray;
    lblHover.ForeColor = Color.Black;
};

this.Controls.Add(lblHover);

7. Label控件的高级用法

7.1 文本换行

示例:实现文本换行

// 创建支持文本换行的标签
Label lblMultiline = new Label();
lblMultiline.Text = "这是一段很长的文本,需要自动换行显示。" +
                   "Label控件默认不支持自动换行,需要设置AutoSize为false并手动设置Size。";
lblMultiline.Location = new Point(50, 50);
lblMultiline.Size = new Size(200, 80); // 设置足够的高度
lblMultiline.AutoSize = false; // 必须设置为false
lblMultiline.TextAlign = ContentAlignment.TopLeft;
this.Controls.Add(lblMultiline);

7.2 助记键(快捷键)

示例:使用助记键

// 创建带助记键的标签
Label lblMnemonic = new Label();
lblMnemonic.Text = "&姓名:"; // &符号后的字符将成为助记键
lblMnemonic.Location = new Point(50, 50);
lblMnemonic.Size = new Size(80, 30);
lblMnemonic.TextAlign = ContentAlignment.MiddleRight;
lblMnemonic.UseMnemonic = true; // 启用助记键
this.Controls.Add(lblMnemonic);

// 创建对应的文本框
TextBox txtName = new TextBox();
txtName.Location = new Point(140, 50);
txtName.Size = new Size(200, 30);
txtName.TabIndex = 1; // 设置Tab顺序
this.Controls.Add(txtName);

// 当用户按下Alt+N时,焦点会移动到下一个控件(文本框)

7.3 动态调整大小

示例:动态调整Label大小

// 创建自动调整大小的标签
Label lblAutoSize = new Label();
lblAutoSize.Text = "自动调整大小的标签";
lblAutoSize.Location = new Point(50, 50);
lblAutoSize.AutoSize = true; // 自动调整大小以适应文本
lblAutoSize.Font = new Font("微软雅黑", 12);
this.Controls.Add(lblAutoSize);

// 动态修改文本,标签会自动调整大小
Button btnChangeText = new Button();
btnChangeText.Text = "修改文本";
btnChangeText.Location = new Point(50, 100);
btnChangeText.Click += (sender, e) =>
{
    lblAutoSize.Text = "这是修改后的更长的文本内容,标签会自动调整大小";
};
this.Controls.Add(btnChangeText);

7.4 使用Label作为分隔线

示例:使用Label作为分隔线

// 创建水平分隔线
Label lblHorizontalLine = new Label();
lblHorizontalLine.Location = new Point(50, 100);
lblHorizontalLine.Size = new Size(300, 2);
lblHorizontalLine.BackColor = Color.Gray;
this.Controls.Add(lblHorizontalLine);

// 创建垂直分隔线
Label lblVerticalLine = new Label();
lblVerticalLine.Location = new Point(200, 50);
lblVerticalLine.Size = new Size(2, 200);
lblVerticalLine.BackColor = Color.Gray;
this.Controls.Add(lblVerticalLine);

8. 完整示例程序

示例:用户信息表单

using System;
using System.Drawing;
using System.Windows.Forms;

namespace LabelExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetupForm();
        }
        
        private void SetupForm()
        {
            this.Text = "用户信息";
            this.Size = new Size(450, 300);
            this.StartPosition = FormStartPosition.CenterScreen;
            
            // 创建标题标签
            Label lblTitle = new Label();
            lblTitle.Text = "用户信息表单";
            lblTitle.Location = new Point(100, 20);
            lblTitle.Size = new Size(250, 40);
            lblTitle.Font = new Font("微软雅黑", 16, FontStyle.Bold);
            lblTitle.ForeColor = Color.FromArgb(52, 152, 219);
            lblTitle.TextAlign = ContentAlignment.MiddleCenter;
            this.Controls.Add(lblTitle);
            
            // 创建姓名标签和文本框
            Label lblName = new Label();
            lblName.Text = "&姓名:";
            lblName.Location = new Point(50, 80);
            lblName.Size = new Size(80, 30);
            lblName.TextAlign = ContentAlignment.MiddleRight;
            lblName.UseMnemonic = true;
            this.Controls.Add(lblName);
            
            TextBox txtName = new TextBox();
            txtName.Location = new Point(140, 80);
            txtName.Size = new Size(200, 30);
            txtName.TabIndex = 1;
            this.Controls.Add(txtName);
            
            // 创建年龄标签和文本框
            Label lblAge = new Label();
            lblAge.Text = "&年龄:";
            lblAge.Location = new Point(50, 120);
            lblAge.Size = new Size(80, 30);
            lblAge.TextAlign = ContentAlignment.MiddleRight;
            lblAge.UseMnemonic = true;
            this.Controls.Add(lblAge);
            
            TextBox txtAge = new TextBox();
            txtAge.Location = new Point(140, 120);
            txtAge.Size = new Size(200, 30);
            txtAge.TabIndex = 2;
            this.Controls.Add(txtAge);
            
            // 创建性别标签和单选按钮
            Label lblGender = new Label();
            lblGender.Text = "&性别:";
            lblGender.Location = new Point(50, 160);
            lblGender.Size = new Size(80, 30);
            lblGender.TextAlign = ContentAlignment.MiddleRight;
            lblGender.UseMnemonic = true;
            this.Controls.Add(lblGender);
            
            RadioButton rdoMale = new RadioButton();
            rdoMale.Text = "男";
            rdoMale.Location = new Point(140, 160);
            rdoMale.Size = new Size(60, 30);
            rdoMale.Checked = true;
            this.Controls.Add(rdoMale);
            
            RadioButton rdoFemale = new RadioButton();
            rdoFemale.Text = "女";
            rdoFemale.Location = new Point(200, 160);
            rdoFemale.Size = new Size(60, 30);
            this.Controls.Add(rdoFemale);
            
            // 创建提示标签
            Label lblHint = new Label();
            lblHint.Text = "提示:请填写完整的用户信息";
            lblHint.Location = new Point(50, 200);
            lblHint.Size = new Size(300, 30);
            lblHint.ForeColor = Color.Gray;
            lblHint.Font = new Font("微软雅黑", 9);
            this.Controls.Add(lblHint);
            
            // 创建超链接标签
            Label lblLink = new Label();
            lblLink.Text = "访问我们的网站";
            lblLink.Location = new Point(150, 240);
            lblLink.Size = new Size(150, 30);
            lblLink.ForeColor = Color.Blue;
            lblLink.Font = new Font(lblLink.Font, FontStyle.Underline);
            lblLink.Cursor = Cursors.Hand;
            lblLink.Click += (sender, e) =>
            {
                System.Diagnostics.Process.Start("https://www.c.com");
            };
            this.Controls.Add(lblLink);
        }
    }
}

9. 总结

通过本教程的学习,你应该掌握了C# Windows Forms中Label标签控件的使用方法:

  • 如何创建Label控件(通过设计器和代码)
  • Label控件的常用属性设置
  • Label控件的常用事件处理
  • Label控件的应用场景(标识其他控件、显示静态信息、显示动态信息、显示图片和文本、超链接效果)
  • Label控件的样式定制(自定义字体和颜色、边框样式、鼠标悬停效果)
  • Label控件的高级用法(文本换行、助记键、动态调整大小、使用Label作为分隔线)

Label控件是Windows Forms应用程序中最基础、最常用的控件之一,虽然它的功能相对简单,但在构建用户界面时起着重要的作用。通过合理使用Label控件,可以使应用程序的界面更加清晰、直观,提高用户体验。在实际开发中,你可以根据具体需求,结合Label控件的各种属性和事件,创建出符合用户需求的界面元素。