How to style the Border of Label in C#?

In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. You are allowed to style the border of the Label control using the BorderStyle Property. You can style the border of the label in three different ways and these values are provided by the BorderStyle enum:

  • Fixed3D: The border of the label is a 3-D border.
  • FixedSingle: The border of the label is single-line border.
  • None: Label without border.

The default value of this property is BorderStyle.None. You can set this property using two different methods:

1. Design-Time: It is the easiest method to set the BorderStyle property of the Label control using the following steps:



  • Step 1: Create a windows form as shown in the below image:
    Visual Studio -> File -> New -> Project -> WindowsFormApp
  • Step 2: Drag the Label control from the ToolBox and drop it on the windows form. You are allowed to place a Label control anywhere on the windows form according to your need.
  • Step 3: After drag and drop you will go to the properties of the Label control to set the BorderStyle property of the Label.

    Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can style the border of the Label control programmatically with the help of given syntax:

public virtual System.Windows.Forms.BorderStyle BorderStyle { get; set; }

Here, BorderStyle indicates border style values. It will throw an InvalidEnumArgumentException if the value assigned to this property does not belong to BorderStyle values. Following steps are used to set the BorderStyle property of the Label:

  • Step 1: Create a label using the Label() constructor is provided by the Label class.
    // Creating label using Label class
    Label mylab = new Label();
    
  • Step 2: After creating Label, set the BorderStyle property of the Label provided by the Label class.
    // Set BorderStyle property of the label
    mylab.BorderStyle = BorderStyle.FixedSingle;
    
  • Step 3: And last add this Label control to form using Add() method.
    // Add this label to the form
    this.Controls.Add(mylab);
    

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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 WindowsFormsApp16 {
      
    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.Size = new Size(120, 25);
            mylab.BorderStyle = BorderStyle.FixedSingle;
      
            // Adding this control to the form
            this.Controls.Add(mylab);
        }
    }
    }

    chevron_right

    
    

    Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.