By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,842 Members | 2,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,842 IT Pros & Developers. It's quick & easy.

how to check if it is text box text changed in c#

P: 1
Expand|Select|Wrap|Line Numbers
  1. bool isDirty = false;
  2.  
  3. void SomeInitMethod() //ie. Form_Load
  4. {
  5.     textbox1.TextChanged += new EventHandler(DirtyTextChange);
  6.     textbox2.TextChanged += new EventHandler(DirtyTextChange);
  7.     textbox3.TextChanged += new EventHandler(DirtyTextChange);
  8.     //...etc
  9. }
  10. void DirtyTextChange(object sender, EventArgs e)
  11. {
  12.     isDirty = true;
  13. }
  14. void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  15. {
  16.     if (isDirty) {
  17.         //ask user
  18.     }
  19. }
  20.  
  21. // to clear
  22. void Save()
  23. {
  24.     SaveMyDataMethod();
  25.     isDirty = false;
  26. }
If you have a lot of textboxes in the form loop through the forms control collection and use typeof to address the textboxes. If you have textboxes requiring different approaches use the Tag property of the textbox to distinguish.
Sep 10 '14 #1
Share this Question
Share on Google+
1 Reply


Frinavale
Expert Mod 5K+
P: 9,731
Sorry, what's the question?

It looks like all you have to do is set isDirty = true when the text changes?
Sep 11 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.