Determine previously focused control in winforms

A

adiel_g

I have a couple of textboxes and a button. I also have other controls
in the same form. When I click on the button, I want to know the
previously selected control, specifically I want to know if it was a
textbox. If I use the .Focused method for example:

If txtBox1.Focused then
.....never goes here
ElseIf txtBox2.Focused then
.....never goes here
End if

It will not work since the button will now have focused:

If btnCheck.Focused then
....it goes into here
End If

The reason being when the user clicked on the button, all the
textboxes lost focus and now the button has focus. Now my question
is, is there a way to programatically know which was the previosly
focused control? I cannot use the click event on the textbox and
store the last clicked textbox since the user might have click on
other controls and therefore that textbox would not be the previously
selected control.

Thanks Before Hand,

Adiel
 
G

Guest

I do similar to return focus to a control after the user pushes a button with
the mouse. The way I do it is as follows.

In the form, add
Private FocusControl As Control = Nothing
Private Sub FocusControlEvent(ByVal sender As Object, ByVal e As EventArgs)
FocusControl = CType(sender, Control)
End Sub

For each control that you want to track (not the buttons), arrange for
FocusControlEvent to handle the Enter event, eg
AddHandler MyCtl.Enter, AddressOf FocusControlEvent

Now, when a button is clicked, you can return focus to the previous control
by something like this:
If Not FocusControl Is Nothing Then FocusControl.Focus()
 
A

adiel_g

Thanks that was a big help! I have one more issue, I have other
controls on the form that can be selected such as a slider. When I
use the code above, it still thinks there is a textbox selected even
though now the slider is selected. I tried creating the following
handlers, modifiying your example in order to "unselect" the textbox:

AddHandler MyCtl.Leave, AddressOf UnFocusControlEvent

And then creating the following function:

Private Sub UnFocusControlEvent(ByVal sender As Object, ByVal e As
EventArgs)
FocusControl = Nothing
End Sub

So that once the user clicks on something else besides a textbox, the
FocusControl variable would no longer contain the value of the last
selected textbox. This did not work since by the time you get to
executing this line:

If Not FocusControl Is Nothing Then FocusControl.Focus()

The FocusControl variable is already Nothing even if the last control
you selected was a textbox, since the UnFocusControlEvent executes
before the button click code.

Any suggestions?

Thanks again,

Adiel
 
G

Guest

I would base everything on the Enter event. If the control being entered is
a textbox then set FocusControl to sender (as is being done now). If the
control being entered is a slider (and perhaps others), set FocusControl to
Nothing. You could implement this with two different Enter event handlers,
one for setting FocusControl to sender and one for setting it to Nothing.
Or, you could have one Enter event handler that determines sender's control
type and sets FocusControl appropriately.

If any buttons are getting their Enter event handled this way, then the
appropriate action is to do nothing. You will get the Enter event before the
Clicked event, and any update to FocusControl wouldn't work right. It is
probably simplest for you to not handle the Enter event for buttons.
 

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