Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set the alignment of Check Mark in CheckBox in C#?
In a graphical user interface, a CheckBox control in C# enables users to pick a true/false or yes/no choice. The check mark in a CheckBox control is normally aligned to the left, but you can customize its position using the CheckAlign property.
Syntax
The syntax to change the alignment of the check mark is
checkBox1.CheckAlign = ContentAlignment.MiddleRight;
The CheckAlign property accepts values from the ContentAlignment enumeration
-
MiddleLeft Align the check mark to the left edge of the control (default).
-
MiddleRight Align the check mark to the right edge of the control.
-
MiddleCenter Align the check mark in the center of the control.
Using CheckAlign Property
Example
The following example creates checkboxes with different check mark alignments
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
this.Size = new Size(400, 300);
this.Text = "CheckBox Alignment Example";
// Creating and setting the properties of label
Label label1 = new Label();
label1.Text = "Select fruits:";
label1.Location = new Point(50, 30);
label1.Size = new Size(100, 23);
this.Controls.Add(label1);
// CheckBox with MiddleLeft alignment (default)
CheckBox leftCheckBox = new CheckBox();
leftCheckBox.Text = "Orange (Left)";
leftCheckBox.Size = new Size(150, 30);
leftCheckBox.Location = new Point(50, 60);
leftCheckBox.CheckAlign = ContentAlignment.MiddleLeft;
this.Controls.Add(leftCheckBox);
// CheckBox with MiddleRight alignment
CheckBox rightCheckBox = new CheckBox();
rightCheckBox.Text = "Mango (Right)";
rightCheckBox.Size = new Size(150, 30);
rightCheckBox.Location = new Point(50, 100);
rightCheckBox.CheckAlign = ContentAlignment.MiddleRight;
this.Controls.Add(rightCheckBox);
// CheckBox with MiddleCenter alignment
CheckBox centerCheckBox = new CheckBox();
centerCheckBox.Text = "Apple (Center)";
centerCheckBox.Size = new Size(150, 30);
centerCheckBox.Location = new Point(50, 140);
centerCheckBox.CheckAlign = ContentAlignment.MiddleCenter;
this.Controls.Add(centerCheckBox);
}
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
CheckAlign vs TextAlign Properties
It's important to understand the difference between these two properties
| Property | Purpose | Effect |
|---|---|---|
CheckAlign |
Controls check mark position | Moves the check box (square) within the control |
TextAlign |
Controls text position | Moves the text label within the control |
Example with Both Properties
using System;
using System.Drawing;
using System.Windows.Forms;
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
this.Size = new Size(350, 200);
this.Text = "CheckAlign vs TextAlign";
CheckBox customCheckBox = new CheckBox();
customCheckBox.Text = "Custom Alignment";
customCheckBox.Size = new Size(200, 50);
customCheckBox.Location = new Point(75, 75);
// Check mark on the right, text centered
customCheckBox.CheckAlign = ContentAlignment.MiddleRight;
customCheckBox.TextAlign = ContentAlignment.MiddleCenter;
this.Controls.Add(customCheckBox);
}
public static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Setting Alignment in Visual Studio Designer
You can also set checkbox alignment using the Visual Studio Properties window
-
Select the CheckBox control on the form designer
-
Open the Properties window (F4)
-
Find the
CheckAlignproperty -
Click the dropdown and select the desired alignment
Conclusion
The CheckAlign property in C# allows you to customize the position of the check mark within a CheckBox control. Use MiddleLeft for default alignment, MiddleRight to move the check mark to the right, or MiddleCenter for center alignment. This property works independently of TextAlign, giving you full control over checkbox layout.
