?
Solved

event handler for modified text

Posted on 2006-06-14
5
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 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