vb.net - how to find out which textbox is currenlty selected -
i have 3 text-boxes, how find out text-box selected (has focus). unable come anything.
public class form1 public activetextbox textbox = ctype(me.activecontrol, textbox) private sub button1_click(byval sender system.object, byval e system.eventargs) handles button1.click activetextbox.text = activetextbox.text & "this text 1" end sub private sub button2_click(byval sender system.object, byval e system.eventargs) handles button2.click activetextbox.text = activetextbox.text & "this text 2" end sub end class
i assuming windows forms application.
this similar question offers 2 possible suggestions:
me.activecontrol
or, can write method using windows api handle of control has focus. article on windowsclient.net has example.
however, each of these options work if textbox still has focus @ time method called.
within button event handler, trying find out last active textbox in order it. in situation, me.activecontrol
not use because textbox control lose focus click on button. sample code below shows how might use enter , leave events of textbox keep track of textbox active. have assumed textbox called textbox1.
public class form1 public activetextbox textbox private sub textbox_enter(byval sender system.object, byval e system.eventargs) handles textbox1.enter, textbox2.enter, textbox3.enter activetextbox = ctype(sender, textbox) end sub private sub button1_click(byval sender system.object, byval e system.eventargs) handles button1.click if (activetextbox isnot nothing) activetextbox.text = activetextbox.text & "this text 1" end if end sub private sub button2_click(byval sender system.object, byval e system.eventargs) handles button2.click if (activetextbox isnot nothing) activetextbox.text = activetextbox.text & "this text 2" end if end sub end class
Comments
Post a Comment