Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

event handler for modified text

Posted on 2006-06-14
5
Medium Priority
?
281 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 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