WPF Form Validation -


private void startdate_lostfocus(object sender, routedeventargs e) {     if (!validate())     {         dispatcher.begininvoke(         dispatcherpriority.contextidle,         new action(delegate()         {             startdate.focus();         })      }); } 

i'm validating date in lost focus of textbox of wpf application. currently, date validation - if fails validation reset focus textbox. correct approach?

it seems working fine, i'm hoping i'm not creating problems or memory leaks begininvoke.

thanks in advance

as adi suggested, please consider using more common approach validation. however, if application you're working on fun or know how wpf works, please consider:

  1. validating asynchronously
  2. interacting ui synchronously in ui thread

also, validate before losing focus (textchanged might option). this:

    private void textbox_textchanged(object sender, textchangedeventargs e)     {         textbox validatedtextbox = sender textbox;         validatedtextbox.isenabled = false;          backgroundworker worker = new backgroundworker();          worker.dowork += (s, ea) => { thread.sleep(1000); }; // simulate long validation         worker.runworkercompleted += (s, ea) =>         {             if (validatedtextbox.text.contains('a'))             {                 validatedtextbox.background = new solidcolorbrush(colors.red);             }             else             {                 validatedtextbox.background = new solidcolorbrush(colors.green);             }              validatedtextbox.isenabled = true;         };          worker.runworkerasync();     } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -