Skip to content
Related Articles

Related Articles

How to set the Length of the Characters in TextBox in C#?
  • Last Updated : 29 Nov, 2019

In Windows forms, TextBox plays an important role. With the help of TextBox, the user can enter data in the application, it can be of a single line or of multiple lines. In TextBox, you are allowed to set the maximum number of characters which the user can type or paste or entered into the TextBox with the help of MaxLength property. The default value of this property is 32767. You can use this property in the form where you are restricted to enter some certain amount of characters like phone number, postal code, etc. In Windows form, you can set this property in two different ways:

1. Design-Time: It is the simplest way to set the MaxLength property of the TextBox 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: Drag the TextBox control from the ToolBox and Drop it on the windows form. You can place TextBox anywhere on the windows form according to your need. As shown in the below image:
  • Step 3: After drag and drop you will go to the properties of the TextBox control to set the MaxLength property of the TextBox. 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 MaxLength property of the TextBox programmatically with the help of given syntax:

public virtual int MaxLength { get; set; }

Here, the value of this property is of System.Int32 type. And it will throw an ArgumentOutOfRangeException if the value assigned to the property is less than 0. Following steps are used to set the MaxLength property of the TextBox:



  • Step 1 : Create a textbox using the TextBox() constructor provided by the TextBox class.
    // Creating textbox
    TextBox Mytextbox = new TextBox();
    
  • Step 2 : After creating TextBox, set the MaxLength property of the TextBox provided by the TextBox class.
    // Set MaxLength property
    Mytextbox2.MaxLength = 6;
    
  • Step 3 : And last add this textbox control to from using Add() method.
    // Add this textbox to form
    this.Controls.Add(Mytextbox1);
    

    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 my {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting the properties of Lable1
            Label Mylablel1 = new Label();
            Mylablel1.Location = new Point(96, 54);
            Mylablel1.Text = "Enter Name";
            Mylablel1.AutoSize = true;
            Mylablel1.BackColor = Color.LightGray;
      
            // Add this label to form
            this.Controls.Add(Mylablel1);
      
            // Creating and setting the properties of TextBox1
            TextBox Mytextbox1 = new TextBox();
            Mytextbox1.Location = new Point(187, 51);
            Mytextbox1.BackColor = Color.LightGray;
            Mytextbox1.AutoSize = true;
            Mytextbox1.Name = "text_box1";
      
            // Add this textbox to form
            this.Controls.Add(Mytextbox1);
      
            // Creating and setting the properties of Lable1
            Label Mylablel2 = new Label();
            Mylablel2.Location = new Point(96, 102);
            Mylablel2.Text = "Enter Pincode";
            Mylablel2.AutoSize = true;
            Mylablel2.BackColor = Color.LightGray;
      
            // Add this label to form
            this.Controls.Add(Mylablel2);
      
            // Creating and setting the properties of TextBox2
            TextBox Mytextbox2 = new TextBox();
            Mytextbox2.Location = new Point(187, 99);
            Mytextbox2.BackColor = Color.LightGray;
            Mytextbox2.AutoSize = true;
            Mytextbox2.MaxLength = 6;
            Mytextbox2.Name = "text_box2";
      
            // Add this textbox to form
            this.Controls.Add(Mytextbox2);
        }
    }
    }

    Output:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :