.net - When deriving a WPF control, can it be guaranteed that the control's event handler handles the event first? -


i'm deriving wpf textbox control create control accepts currency value input. i'm aware has been done before , there existing libraries use, more of learning exercise borne failed attempt use 1 of existing library controls -- didn't suit requirements.

in doing this, i'm trying prevent textbox accepting text not fit currency format (i.e. optional leading currency symbol, decimal numbers, optional group separators, optional fractional component). i'm aware there previewtextinput event. many sources googled suggested (with approbation community) 1 can handle event , reject unwanted input setting e.handled = true (setting aside moment won't work copy/pasted text, updated data binding, or design-time xaml value, name few).

i have been wondering whether approach works time. given the order event handlers called not guaranteed, how know control's event handler called first? put way: how know someone's event handler doesn't run first , else value allows format i'm trying disallow , sets e.handled = true? onpreviewtextinput method? believe suffers similar concern, not?

it indeed question.as pointed ordered how registered eventhandlers.if manipulate via reflection @ runtime , change handlers order,it work expected.i prepared scenerio told above.

here defined attribute

[attributeusage(attributetargets.method, allowmultiple = false, inherited = true)] public class customattribute : attribute {     private int _value;      public int value     {         { return _value; }         set { _value = value; }     }      private string _eventname;      public string eventname     {         { return _eventname; }         set { _eventname = value; }     }       public customattribute()     {      } } 

andi created custom box extended textbox

public class custombox : textbox {     public custombox()     {         this.previewtextinput += custombox_textchanged;         this.previewtextinput += custombox_previewtextinput;     }      protected override void oninitialized(eventargs e)     {         base.oninitialized(e);          foreach (var item in typeof(custombox).getruntimemethods().tolist())         {             var = item.getcustomattributes();              // unsubscribe              foreach (var in a)             {                 if (i.gettype() == typeof(customattribute))                 {                     if (((customattribute)i).value > 0)                     {                         removeevent(((customattribute)i).eventname, item.name);                     }                 }             }         }         // subscribe according order          var methods = typeof(custombox).getruntimemethods()                   .where(m => m.getcustomattributes(typeof(customattribute), false).length > 0)                   .tolist();          foreach (var item in methods.orderby(m => ((customattribute)m.getcustomattribute(typeof(customattribute))).value))         {             addevent(((customattribute)item.getcustomattribute(typeof(customattribute))).eventname, item.name);         }      }     private void removeevent(string eventname, string methodname)     {         eventinfo ev = this.gettype().getevent(eventname);         type tdelegate = ev.eventhandlertype;         methodinfo mihandler = typeof(custombox).getmethod(methodname, bindingflags.nonpublic | bindingflags.instance);         delegate d = delegate.createdelegate(tdelegate, this, mihandler);         ev.removeeventhandler(this, d);     }      private void addevent(string eventname,string methodname)     {         eventinfo ev = this.gettype().getevent(eventname);         type tdelegate = ev.eventhandlertype;         methodinfo mihandler = typeof(custombox).getmethod(methodname, bindingflags.nonpublic | bindingflags.instance);         delegate d = delegate.createdelegate(tdelegate, this, mihandler);         ev.addeventhandler(this,d);     }      [customattribute(eventname = "previewtextinput",value = 2)]     private void custombox_textchanged(object sender, textcompositioneventargs e)     {         this.text = e.text;         e.handled = true;     }      [customattribute(eventname = "previewtextinput", value = 1)]     private void custombox_previewtextinput(object sender, textcompositioneventargs e)     {         if (e.text.contains("e"))         {             e.handled = true;         }         else e.handled = false;     } } 

above, if creates

this.previewtextinput += custombox_textchanged; handler manipulates textbox text , change unwilling text , blocks event e.handle = true;

just before this.previewtextinput += custombox_previewtextinput; created, reflection changes orders according how define.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -