locked
How to prevent from the user to type any more text into a textBox1 ? RRS feed

  • Question

  • I have a textBox1 with a default text inside or ill add some text later.

    But there is allready text inside and i want to prevent for the user to type anymore/add new text or numbers without Enable=false;

    I want to keep the textBox1 enabled but to prevent the user from adding any more text and numbers.

     

    Thanks.


    danieli
    Friday, June 10, 2011 2:55 PM

Answers

All replies

  • Use the Locked property.
    MCP
    Friday, June 10, 2011 3:39 PM
  • Use a label.

    Adam


    Ctrl+Z
    • Marked as answer by chocolade Friday, June 10, 2011 8:47 PM
    Friday, June 10, 2011 3:42 PM
  • Hi

     

    Set 

    textBox.ReadOnly = true


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Edited by Kris444 Friday, June 10, 2011 4:55 PM
    • Proposed as answer by Thorsten Gudera Friday, June 10, 2011 6:03 PM
    Friday, June 10, 2011 3:53 PM
  • Use Enabled property of the TextBox:

     

    if(textBox1.Text.Lenght > 0)
       textBox1.Enabled = false;
    else
       textBox1.Enabled = true;
    

     


    Mitja

    Friday, June 10, 2011 3:56 PM
  • On KeyPress event of Textbox you can check the condition and if its true/false you can prevent user from typing, have a look below:

        private void Textbox1_KeyPress(object sender, KeyPressEventArgs e)
        {
          try
          {
            if (YourCondition == true)
            {
              e.Handled = false;// false won't accept the key
            }
            else
              e.Handled = true;// true would accept the key pressed
    
          }
          catch (FormatException ex)
          {
            MessageBox.Show(ex.Message);
          }
        }


     


    Amit Govil | MyEmail
    Friday, June 10, 2011 6:29 PM