views:

600

answers:

1

Maybe I'm just an idiot, but I can't seem to find an event that will fire for a textbox at the same time as the leave, but only when the contents of the textbox has changed. Kinda like a combination of textchanged and leave. I can't use textchanged cause it fires on each keystroke. Right now I'm storing the current value of the textbox in a variable and comparing it on the leave event, but it seems really hackish.

Thanks

+2  A: 

You can create your own (derived) class which overrides OnEnter, OnLeave and OnTextChanged to set flags and trigger "your" event.

Something like this:

 public class TextBox: System.Windows.Forms.TextBox {
  public event EventHandler LeaveWithChangedText;

  private bool textChanged;

  protected override void OnEnter(EventArgs e) {
   textChanged = false;
   base.OnEnter(e);
  }

  protected override void OnLeave(EventArgs e) {
   base.OnLeave(e);
   if (textChanged) {
    OnLeaveWithChangedText(e);
   }
  }

  protected virtual void OnLeaveWithChangedText(EventArgs e) {
   if (LeaveWithChangedText != null) {
    LeaveWithChangedText(this, e);
   }
  }

  protected override void OnTextChanged(EventArgs e) {
   textChanged = true;
   base.OnTextChanged(e);
  }
 }
Lucero
Yeah, that's probably a better approach than what I'm doing. I just wanted to make sure I wasn't missing something obvious...
Rob
Wow... that's impressive.. I didn't consider overriding both events... damn good idea..
Rob