How to set the Location of the MaskedTextBox in C#?
In C#, MaskedTextBox control gives a validation procedure for the user input on the form like date, phone numbers, etc. Or in other words, it is used to provide a mask which differentiates between proper and improper user input. In MaskedTextBox control, you can set the location of the MaskedTextBox on the form using Location Property. This property gives the coordinates of the upper-left corner of the MashedTextBox control relative to the upper-left corner of its form or container. You can set this property in two different ways:
1. Design-Time: It is the easiest way to set the location of the MaskedTextBox as shown in the following steps:
- Step 1: Create a windows form as shown in the below image:
Visual Studio -> File -> New -> Project -> WindowsFormApp
- Step 2: Next, drag and drop the MaskedTextBox control from the toolbox on the form. As shown in the below image:

- Step 3: After drag and drop you will go to the properties of the MaskedTextBox and set the location of the MaskedTextBox as shown in the below image:

Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can set the location of the MaskedTextBox control programmatically with the help of given syntax:
public System.Drawing.Point Location { get; set; }Here, Point indicates the upper-left corner of the MaskedTextBox control relative to the upper-left corner of its form. The following steps show how to set the location of the MaskedTextBox dynamically:
- Step 1: Create a MaskedTextBox using the MaskedTextBox() constructor is provided by the MaskedTextBox class.
// Creating a MaskedTextBox MaskedTextBox m = new MaskedTextBox();
- Step 2: After creating MaskedTextBox, set the Location property of the MaskedTextBox provided by the MaskedTextBox class.
// Setting the location m.Location = new Point(374, 137);
- Step 3: And last add this MaskedTextBox control to the form using the following statement:
// Adding MaskedTextBox control on the form this.Controls.Add(m);
Example:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApp36 {publicpartialclassForm1 : Form {publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender, EventArgs e){// Creating and setting the// properties of the LabelLabel l1 =newLabel();l1.Location =newPoint(413, 98);l1.Size =newSize(176, 20);l1.Text =" Example";l1.Font =newFont("Bauhaus 93", 12);// Adding label on the formthis.Controls.Add(l1);// Creating and setting the// properties of the LabelLabel l2 =newLabel();l2.Location =newPoint(242, 135);l2.Size =newSize(126, 20);l2.Text ="Phone number:";l2.Font =newFont("Bauhaus 93", 12);// Adding label on the formthis.Controls.Add(l2);// Creating and setting the// properties of the MaskedTextBoxMaskedTextBox m =newMaskedTextBox();m.Location =newPoint(374, 137);m.Mask ="000000000";m.Size =newSize(176, 20);m.Name ="MyBox";m.Font =newFont("Bauhaus 93", 18);// Adding MaskedTextBox// control on the formthis.Controls.Add(m);}}}Output:



