How to set the Alignment of the Text in the Label in C#?

Configurare noua (How To)

Situatie

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 alignment of the text present in the Label control using the TextAlign Property in the windows form. You can set this property using two different methods:

1. Design-Time

2. Run-Time

Solutie

Pasi de urmat

Design-Time: It is the easiest method to set the TextAlign 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

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.

After drag and drop you will go to the properties of the Label control to set the TextAlign property of the Label.

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

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.AutoSize = true;
mylab.Font = new Font(“Calibri”, 18);
mylab.BorderStyle = BorderStyle.Fixed3D;
mylab.ForeColor = Color.Green;
mylab.Padding = new Padding(6);
mylab.TextAlign = ContentAlignment.MiddleCenter;

// Adding this control to the form
this.Controls.Add(mylab);

// Creating and setting the label
Label mylab1 = new Label();
mylab1.Text = “Welcome To GeeksforGeeks”;
mylab1.Location = new Point(155, 170);
mylab1.AutoSize = true;
mylab1.BorderStyle = BorderStyle.Fixed3D;
mylab1.Font = new Font(“Calibri”, 18);
mylab1.Padding = new Padding(6);
mylab.TextAlign = ContentAlignment.MiddleCenter;

// Adding this control to the form
this.Controls.Add(mylab1);
}
}
}

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

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.AutoSize = true;
mylab.Font = new Font("Calibri", 18);
mylab.BorderStyle = BorderStyle.Fixed3D;
mylab.ForeColor = Color.Green;
mylab.Padding = new Padding(6);
mylab.TextAlign = ContentAlignment.MiddleCenter;

// Adding this control to the form
this.Controls.Add(mylab);

// Creating and setting the label
Label mylab1 = new Label();
mylab1.Text = "Welcome To GeeksforGeeks";
mylab1.Location = new Point(155, 170);
mylab1.AutoSize = true;
mylab1.BorderStyle = BorderStyle.Fixed3D;
mylab1.Font = new Font("Calibri", 18);
mylab1.Padding = new Padding(6);
mylab.TextAlign = ContentAlignment.MiddleCenter;

// Adding this control to the form
this.Controls.Add(mylab1);
}
}
}


Tip solutie

Permanent

Voteaza

(6 din 14 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?