Solved

event handler for modified text

Posted on 2006-06-14
5
264 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
How do I get the unique ID after insert? 87 92
How do I get the id from URL? 19 48
C# Reverse int in fast ways 6 28
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

12 Experts available now in Live!

Get 1:1 Help Now