Solved

event handler for modified text

Posted on 2006-06-14
5
265 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 200 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 300 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 53
bulid json format 3 46
Facial recognition to look through the whole database for a person 3 40
Code works but it's slow 23 30
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now