[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

event handler for modified text

Posted on 2006-06-14
5
Medium Priority
?
286 Views
Last Modified: 2010-04-16
What is the best event Handler to determine when text has changed for a textbox?

I tried TextChanged and it automatically enables the button.  I used Validated and that works okay when the user tabs or moves to another control.

But it enables the button even if no text is changed, all it takes is to move to another control.  

so If I have an empty text box and I tab to another the save button is enabled.

What do I need to use to check when text has been modified?

Thanks for your help.
0
Comment
Question by:yanci1179
  • 2
  • 2
5 Comments
 

Author Comment

by:yanci1179
ID: 16903976
i used key press and everything worked.

does anybody know how I can only include

numbers, letters, backspace, and delete

when looking at keys pressed event handler?

thanks.
0
 
LVL 18

Assisted Solution

by:Ravi Singh
Ravi Singh earned 800 total points
ID: 16904226
To only allow numbers, letters, backspace and delete in a textbox, handle the KeyPress event for the textbox like:

      private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
      {
            if (!char.IsLetterOrDigit(e.KeyChar) && e.KeyChar != (char) Keys.Back && e.KeyChar != (char) Keys.Delete)
                  e.Handled = true;
      }
0
 
LVL 11

Accepted Solution

by:
Expert1701 earned 1200 total points
ID: 16904324
The Delete key does not trigger the TextBox.KeyPress event, but you can check for numbers, letters and backspaces using this:

  if(Char.IsNumber(e.KeyChar)||Char.IsLetter(e.KeyChar)||e.KeyChar==(char)Keys.Back)
  {
    //...
  }

If you are simply trying to tell if the TextBox was modified, try something like the following:

  string savedText;

  private void Form_Load(object sender, System.EventArgs e)
  {
    //...

    savedText = textBox.Text;
    saveButton.Enabled = false;
  }

  void textBox_TextChanged(object sender, System.EventArgs e)
  {
    saveButton.Enabled = savedText!=textBox.Text;
  }

  private void saveButton_Click(object sender, System.EventArgs e)
  {
    savedText = textBox.Text;
    saveButton.Enabled = false;
  }
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 16904330
Sorry Zephyr__, I should have refreshed...
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16904355
Hi - no probs at all, your post is more detailed and more helpful! :)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question