vb.net - How can I use the same code without having to retype it every time in different subs? -


so have textbox want allow numbers in, have code , works fine. want use in few textboxes, how can use same code without having retype in each textbox's keydown , keypress subs?

the code i'm using in keydown subs is

if e.keycode = keys.back         backspace = true     else         backspace = false     end if 

and in keypress subs i'm using

if backspace = false         dim allowedchars string = "0123456789"         if allowedchars.indexof(e.keychar) = -1             e.handled = true         end if     end if 

i'm using code few textboxes , wondering how can clean bit. can i? thank , time!

you make own version of textbox control inherits base textbox , extends custom code.

for example:

public class clevertextbox     inherits textbox      private backspace boolean = false      private sub clevertextbox_keydown(sender object, e keyeventargs) handles me.keydown         if e.keycode = keys.back             backspace = true         else             backspace = false         end if     end sub      private sub clevertextbox_keypress(sender object, e keypresseventargs) handles me.keypress         if backspace = false             dim allowedchars string = "0123456789"             if allowedchars.indexof(e.keychar) = -1                 e.handled = true             end if         end if     end sub end class 

then recompile project, , should see new clevertextbox control has been added toolbox panel, , can drag , drop on form.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -