c# - Make form function accesible to all forms -


i have function on form1, , want use same function form2, form3 , on, instead of duplicate function on each form there way make accesible all? i've tried make new class : form , call function forms, not working...

public void tb_leave(object sender, eventargs e) {     if ((sender textbox).text.count() < (sender textbox).maxlength)         (sender textbox).text = (sender textbox).text.padleft((sender textbox).maxlength, '0'); } 

thank in advice.

update

thank answers, work fine, if want use same method x textboxes? (like doing tb_leave function)

i mean, old method select x textboxes , send leave event function, way mention need create method call method within helper class... still need create method inside every form, call class, right? although, answers helpful need create new .cs file helper classes :)

update 2 i'm having problems migrating method

public static void textboxkeydown(this textbox tb, keyeventargs e)     {         switch (e.keycode)         {             case keys.enter:             case keys.add:                 e.suppresskeypress = true;                 processtabkey(true);                 break;             case keys.decimal:                 if (tb.tag == "importe")                 {                     e.suppresskeypress = true;                     processtabkey(true);                 }                 break;             case keys.subtract:                 e.suppresskeypress = true;                 processtabkey(false);                 break;         }     } 

of course know processtabkey(); work on active form, how make working outside de form class? thanks

this simplified version of code. create method reuse everywhere create new utility.cs file containing simple static class

namespace myapp.utilities {     public static class myutility     {         public static void padfortextbox(textbox tb)         {             if (tb.text.length < tb.maxlength)                 tb.text = tb.text.padleft(tb.maxlength, '0');         }     } } 

now call method every form has reference namespace define class.

public void tb_leave(object sender, eventargs e) {     utility.padfortextbox(sender textbox); } 

another elegant method achieve same result through extension method textbox

namespace myapp.utilities {     public static class textboxextensions     {         public static void padfortextbox(this textbox tb)         {             if (tb.text.length < tb.maxlength)                 tb.text = tb.text.padleft(tb.maxlength, '0');         }     } } 

and call

public void tb_leave(object sender, eventargs e) {     (sender textbox).padfortextbox(); } 

by way, using these methods allow rid of ugly sequence of casts.

of course tb_leave method event handler , should linked textboxes.
if want have textbox event handler common every textboxes in apps indipendently form in textboxes created, cannot rely on winform designer, need add manually event handler textboxes in form constructor after initializecomponent call. in prefer leave task designer , add single line above when needed. example:

initializecomponent(); // connect leave event 3 textboxes same static method inside // myutility static class textbox1.leave+=myutility.padeventfortextbox; textbox2.leave+=myutility.padeventfortextbox; textbox3.leave+=myutility.padeventfortextbox;  .....  public static void padeventfortextbox(object sender, eventargs e) {     textbox tb=sender textbox;     if (tb.text.length<tb.maxlength)         tb.text=tb.text.padleft(tb.maxlength, '0'); } 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -