none
How to make "Max character limit for text box"

    Question

  • I will post my code for what I have right now. 

    I have a form, that is going to be gathering information for doctor appointments. I have two text boxes... Who and Where that need to be filled out.

    I want to make it so that the user can't type more than 30 characters in these text boxes. If so I would like to display a message saying "Too long" But I've never done this before. Anyone have any idea how to do this?

     

     

    Here is my form code...

     

     

    Wednesday, August 17, 2011 10:36 PM

Answers

  • you can use textbox Maxlenght property but if you want to display a message dont use it and write an if clause to textbox textchanged event..

     

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
          if (textBox1.Text.Length > 30)
          {
            label1.Text = "display message here!";
          }
        }
    



    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Wednesday, August 17, 2011 11:25 PM
  • Hi

    You should be setting MaxLength property as suggested by Mathew and Yasin

    For showing message box depend on KeyDown as TextChange will not fire when the length is restricted 

    void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
          if (textBox1.Text.Length == 30)
          {
    
          }
        }
    


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Thursday, August 18, 2011 3:12 AM
  • TextBox controls have a property called MaxLength that you can use to prevent users typing in more than a certain number of characters.
    Wednesday, August 17, 2011 10:51 PM

All replies

  • Here is my form code

     

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ApptMgr
    {
      public partial class Form1 : Form
      {
        
    
        public Form1()
        {
          InitializeComponent();
        }
    
        private void dtpStartTime_ValueChanged(object sender, EventArgs e)
        {
    
        }
        private void button1_Click(object sender, EventArgs e)
        {
    
          if (txtWho.Text == "")
          {
            MessageBox.Show(
              "Who is a required field.", "Entry Error");
            txtWho.Focus();
          }
          if (txtWhere.Text == "")
          {
            MessageBox.Show(
              "Where is a required field.", "Entry Error");
            txtWhere.Focus();
          }
          
         
    
          dtpStartTime.CustomFormat = "MM/dd/yyyy HH:mm:ss tt";
          dtpStartTime.MaxDate = DateTime.Now.AddYears(1);
    
          MessageBox.Show(dtpStartTime.Value.ToString());
        }
    
    
        
      }
    }
    
    


    Wednesday, August 17, 2011 10:37 PM
  • TextBox controls have a property called MaxLength that you can use to prevent users typing in more than a certain number of characters.
    Wednesday, August 17, 2011 10:51 PM
  • you can use textbox Maxlenght property but if you want to display a message dont use it and write an if clause to textbox textchanged event..

     

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
          if (textBox1.Text.Length > 30)
          {
            label1.Text = "display message here!";
          }
        }
    



    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Wednesday, August 17, 2011 11:25 PM
  • Hi

    You should be setting MaxLength property as suggested by Mathew and Yasin

    For showing message box depend on KeyDown as TextChange will not fire when the length is restricted 

    void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
          if (textBox1.Text.Length == 30)
          {
    
          }
        }
    


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Thursday, August 18, 2011 3:12 AM
  •  

    Hello mMjM80802,
    

     

    I will post my code for what I have right now.

    I have a form, that is going to be gathering information for doctor appointments. I have two text boxes... Who and Where that need to be filled out.

    I want to make it so that the user can't type more than 30 characters in these text boxes. If so I would like to display a message saying "Too long" But I've never done this before. Anyone have any idea how to do this?

     

     

    Here is my form code...

     

     


    you can use also Validated event , se example below.

     

    private void textBox1_Validating(object sender, EventArgs e)
     {
     if (textBox1.Text.Length > 30)
     {
     label1.Text = "Max input is 30 character!";
     }
     }
     
    
    

    Regards.

     

     


    Carmelo La Monica http://community.visual-basic.it/carmelolamonica/

    WordPress  http://carmelolamonica.wordpress.com/


    Thursday, August 18, 2011 4:37 AM
  • You can trim the output in TextChanged event

    if(TextBox1.TextLength>30)

    TextBox1.Text = TextBox1.Text.Substring(0,30);

     


    Muthu Krishnan.R Use only what you need, Reduce global warming
    Thursday, August 18, 2011 4:50 AM