Different Windows Controls | Label | C# | BKNMU Junagadh



C#, the Label class is used to create a label control in a graphical user interface (GUI) application. Labels are often used to display text or provide descriptions for other controls. Here are some common methods and properties associated with the Label class:


Properties:

  1. Name:

    • Gets or sets the name of the control.
    • Example: label1.Name = "lblDescription";
  2. Text:

    • Gets or sets the text that is displayed in the label.
    • Example: label1.Text = "Enter your name:";
  3. AutoSize:

    • Gets or sets a value indicating whether the label automatically adjusts its size to fit its contents.
    • Example: label1.AutoSize = true;
  4. BackColor:

    • Gets or sets the background color of the label.
    • Example: label1.BackColor = Color.Yellow;
  5. ForeColor:

    • Gets or sets the foreground color (text color) of the label.
    • Example: label1.ForeColor = Color.Black;
  6. Font:

    • Gets or sets the font of the text displayed by the label.
    • Example: label1.Font = new Font("Arial", 12, FontStyle.Bold);
  7. Size:

    • Gets or sets the size of the label.
    • Example: label1.Size = new Size(150, 20);
  8. Location:

    • Gets or sets the location of the label.
    • Example: label1.Location = new Point(10, 10);

Methods:

  1. ToString:
    • Returns a string that represents the current object.
    • Example: string labelInfo = label1.ToString();

Example Usage:


using System;
using System.Windows.Forms;

namespace LabelExample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // Set properties during form load
            label1.Text = "Enter your name:";
            label1.BackColor = System.Drawing.Color.Yellow;
            label1.ForeColor = System.Drawing.Color.Black;
            label1.Font = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold);
            label1.Size = new System.Drawing.Size(150, 20);
            label1.Location = new System.Drawing.Point(10, 10);
        }
    }
}


In this example, the MainForm_Load event handler sets various properties of the label1 control during form initialization. You can customize the properties based on your specific requirements. Labels are often used in conjunction with other controls to provide information or guide users in a GUI application.


Post a Comment

Thanks for comment.

Previous Post Next Post