-1

Working on a text editor here...

I'm trying to figure out if the user has actually caused any changes in a textbox after a keyboard event has occurred. Obviously if they press a standard keys like, A B C, it will fire, but I'm more interested if there's a way, besides enumerating all possible, non-input options.

I will note that I realize the state of the textbox could be compared before and after, but that seems like it would be a poor use of memory, and CPU time if it was a large document. Also, I'm using the Modified property for another purpose, and since it only fires when it changes, that also isn't an options, as it will more often than not be in the true state.

I've though about the enumeration side, but that also seems inefficient, as, I would not only have to ensure that every non-input option is there, but also check for all the exceptions. One example that comes to mind:

If Ctrl is pressed, I don't want to do anything, unless I have Ctrl + V or Ctrl + X.

However, if there's nothing in the clipboard, then Ctrl + V shouldn't do anything, and if nothing is selected, then Ctrl + X shouldn't do anything.

I'm sure there are more exceptions that I'm not thinking of right now.

As for the Modified property, when that changes I'm using it to modify the widow title with/without an asterisk based on it's state. So if I reset the property, it will remove the asterisk.

David
  • 4,744
  • 5
  • 33
  • 64
  • What about hashing the content? – Steve B Jan 19 '15 at 15:58
  • @SteveB Feel free to add that method. Though I think since .NET already fires the `TextChanged` event, that is the fastest method, as all it does it add a function call and whatever code is inside the function. – David Jan 19 '15 at 23:25

1 Answers1

-1

In the famous method of rubber duck debugging, I ended up solving this simply by asking it. If I've missed a more efficient option, please let me know.

The first and what would seem the be the simpliest option would be to use the TextChanged event, which I actually learned about by adding tags to my post.

If for whatever reason that doesn't work for the future reader, then using the ModifiedChanged event would be the next best option.

Simply put, in my situation, the asterisk only goes away when the document is saved. By setting a flag, I can use an if statement to determine if I need to remove it, and then reset the flag at that point. Something like this:

void saved(){
    // ...
    saved = true;
}

// ...
if(saved){
    removeAsterisk();
    saved = false;
}
// ...

Those seem to be the best options, which are neither long in code, nor consuming in time by checking 100k characters.

David
  • 4,744
  • 5
  • 33
  • 64