focus

J

jdrott1

I'm trying to input letters from a button into a couple different
textboxes. fields such as: firstname, lastname, address, phone
number.
so the focus can't be set to that textbox always. if they want to
type into the lastname box, then i would have to find where the focus
is?

a friend of mine wrote:

"when you "click" the button, the focus shifts to that button so
you're
going to have to manually figure out which textbox had focus before
you button click (maybe set a variable whenever a textbox gets focus
as there is an OnFocus event you can use)"

i'm not quite sure how to do this, can someone help me?
 
C

Charlie Brown

In the forms load event, you could loop through all the controls on
the page and add an event handler to the GotFocus event of each
control to watch for the last focused control

Private lastFocusedControl As Control

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
For Each ctrl As Control In Me.Controls
AddHandler ctrl.GotFocus, AddressOf OnControlFocus
Next
End Sub

Protected Sub OnControlFocus(ByVal sender As Object, ByVal e As
EventArgs)
lastFocusedControl = CType(sender, Control)
End Sub

Then in your button click event, just check the lastFocusedControl for
the type and if it is a textbox, update accordingly

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If TryCast(lastFocusedControl, TextBox) IsNot Nothing Then
Dim textboxcontrol As TextBox = CType(lastFocusedControl,
TextBox)
textboxcontrol.Text = ""
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top