Detect when a control loses focus

Hi,

simple subject but i'm puzzled. A simple form with a toolbar and on that toolbar are some toolbarbuttons. On the form are some textboxes, labels and commandbuttons. The number of textboxes/commandbuttons is not constant. Some buttons on the toolbar must be enabled if the activecontrol is a textbox. If the user clicks on a commandbutton the toolbarbuttons should be disabled. I could add code for each control to check what type of control the activecontrol is and act on that. Is there an easy way to loop through the controls to determine the type of the activecontrol when a control loses focus Please let me know if it is not clear to you what I mean.

Cheers,

Frank



Answer this question

Detect when a control loses focus

  • W Wong

    Hi,

    To detect focus change I'd say you'll have to attach appropriate handlers to all controls' GotFocus or LostFocus events and check the form's ActiveControl property for currently focused control's type [each form has the ActiveControl property which references currently focused control on that form].

    Andrej



  • Biche

    Reading the MS documentation, they highly recommend NOT using LostFocus or GotFocus in VS 2003/2005. Rather, they recommend using the Enter and Leave events.

    You could wire up all of your event handlers to access one routine like this:

    Private Sub AddEventHandlers(ByVal ctrlContainer As Control)

    For Each ctrl As Control In ctrlContainer.Controls

    If TypeOf ctrl Is TextBox OrElse TypeOf ctrl Is ComboBox Then

    AddHandler ctrl.Enter, AddressOf ProcessEnter

    AddHandler ctrl.Leave, AddressOf ProcessLeave

    End If

    ' If the control has children,

    ' recursively call this function

    If ctrl.HasChildren Then

    AddEventHandlers(ctrl)

    End If

    Next

    End Sub

    (This just hooks up Textbox and ComboBox controls, but you could have it do all types of controls.)

    Your ProcessLeave could then use me.ActiveControl to determine what the new active control is.

    Hope this helps.


  • pinoyz

    This is what I needed. Thanks
  • sirpelidor

    When a control loses focus the LostFocus event will be fired so you could use that to detect when a control loses focus. The sender parameter of the EventHandler will tell you which control lost focus so you could use a single event handler method to handle the lost focus event for multiple controls orr you could have separate handlers for each control.

    Finding the active control is a bit trickier. Given any control at all, you can call FindForm to find the form the control is on and once you have that form, you can use the ActiveControl property to find that form's active control.

    You wouldn't necessarily need that though. You could enable buttons for a particular control on that control's GotFocus handler and disable them again in the control's LostFocus handler. Or, you could have a loop to disable all buttons in the GotFocus handler before enabling the buttons that apply to the focused control.


  • Detect when a control loses focus