How to Set the Location of the Label in C#?
Last Updated :
30 Jun, 2019
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 set the location of the Label control using the Location Property in the windows form. This property contains the coordinates of the upper-left corner of the Label control relative to the upper-left corner of its form. You can set this property using two different methods:
1. Design-Time: It is the easiest method to set the Location property of the Label control using the following steps:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can set the location of the Label control in the windows forms programmatically with the help of given syntax:
public System.Drawing.Point Location { get; set; }
Here, the Point indicates the upper-left corner of the Label control relative to the upper-left corner of its form. Following steps are used to set the Location 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 Location property of the Label provided by the Label class.
// Set Location property of the label
mylab.Location = new Point(222, 90);
- Step 3: And last add this Label control to form using Add() method.
// Add this label to the form
this.Controls.Add(mylab);
Example:
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)
{
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;
this .Controls.Add(mylab);
}
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...