Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2007

Tip: Looking for answers? Try searching our database.

Determine previously focused control in winforms

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
adiel_g@hotmail.com - 24 Oct 2007 16:20 GMT
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
AMercer - 24 Oct 2007 23:13 GMT
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()

> 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
[quoted text clipped - 24 lines]
>
> Adiel
adiel_g@hotmail.com - 26 Oct 2007 13:47 GMT
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
AMercer - 26 Oct 2007 23:15 GMT
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.

> 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
[quoted text clipped - 27 lines]
>
> Adiel

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.