在Windows窗体中,标签控件用于在窗体上显示文本,并且不参与用户输入或鼠标或键盘事件。 Label是一个类,它在System.Windows.Forms命名空间下定义。在Windows窗体中,可以用两种不同的方式创建Label:
1.设计时:使用以下步骤创建Label控件是最简单的方法:
- 第1步:创建一个Windows窗体,如下图所示:
Visual Studio->文件->新建->项目-> WindowsFormApp - 步骤2:从“工具箱”中拖动“标签”控件,并将其放在Windows窗体上。您可以根据需要将Label控件放置在Windows窗体上的任何位置。
- 步骤3:拖放之后,您将转到Label控件的属性,以根据需要设置Label的属性。
输出:
2.运行时:比上述方法有些棘手。在此方法中,您可以使用Label类设置创建自己的Label控件。创建动态标签的步骤:
- 步骤1:使用Label类提供的Label()构造函数创建标签。
// Creating label using Label class Label mylab = new Label();
- 步骤2:创建Label后,设置Label类提供的Label属性。
// Set the text in Label mylab.Text = "GeeksforGeeks"; // Set the location of the Label mylab.Location = new Point(222, 90); // Set the AutoSize property of the Label control mylab.AutoSize = true; // Set the font of the content present in the Label Control mylab.Font = new Font("Calibri", 18); // Set the foreground color of the Label control mylab.ForeColor = Color.Green; // Set the padding in the Label control mylab.Padding = new Padding(6);
- 步骤3:最后使用Add()方法将此Label控件添加到窗体中。
// Add this label to the form this.Controls.Add(mylab);
例子:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp18 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Creating and setting the label Label mylab = new Label(); mylab.Text = "GeeksforGeeks"; mylab.Location = new Point(222, 90); mylab.AutoSize = true; mylab.Font = new Font("Calibri", 18); mylab.ForeColor = Color.Green; mylab.Padding = new Padding(6); // Adding this control to the form this.Controls.Add(mylab); } } }
输出:
标签控件的重要属性
Property | Description |
---|---|
AutoSize | This property is used to set a value indicating whether the Label control is automatically resized to display its entire contents. |
BackColor | This property is used to set the background color for the Label control. |
BackgroundImage | This property is used to set the background image for the Label control. |
BorderStyle | This property is used to set the border style for the Label control. |
FlatStyle | This property is used to set the flat style appearance of the label control. |
Font | This property is used to set the font of the text displayed by the Label control. |
FontHeight | This property is used to set the height of the font of the Label control. |
ForeColor | This property is used to set the foreground color of the Label control. |
Height | This property is used to set the height of the Label control. |
Image | This property is used to set the image that is displayed on a Label. |
Location | This property is used to set the coordinates of the upper-left corner of the Label control relative to the upper-left corner of its form. |
Name | This property is used to set the name of the Label control. |
Padding | This property is used to set padding within the Label control. |
Size | This property is used to set the height and width of the Label control. |
Text | This property is used to set the text associated with this Label control. |
TextAlign | This property is used to set the alignment of text in the label. |
Visible | This property is used to set a value indicating whether the control and all its child controls are displayed. |
Width | This property is used to set the width of the Label control. |