Textbox not executin Leave or LostFocus events when clicking on toolstrip button

Yves Mellet
Yves Mellet used Ask the Experts™
on
I have a windows form with textboxes that supposed to trigger events after being updated in order to be saved in the form object before it saves to the database. When I finish updating the textbox I click the button for save which is on a toolstrip but the Leave or Lost_Focus events don't trigger because when I use the mouse the focus seems to stay on the last control. Anybody knows a clean way to get around this issue?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shaun KlineLead Software Engineer

Commented:
What is the rational behind having your controls store their values in form objects? Could you not just have your save routine retrieve all of the values from your controls before saving to the database?

Author

Commented:
Actually it is a component class that has all the objects from the form. The program used to use structure variables in vb6 and I have replace it with this class.

Commented:
did you try using the textbox1.textchanged event instead?
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Author

Commented:
The form is running slow as it is. The textchanged event works but triggers at every single keystroke running code on the textchanged and the form's KeyDown events. If I don't have any alternative I will use it but I wanted to make sure that's all I have to work with
Commented:
I see. Try using the textbox.validated event. When user clicks on the saave button do a form.Validate to force the textbox.validated event to fire up.
Commented:
in the event toolstrip.click, set the focus to some other control

if control1.Focused then
      control2.focus
else
   control1.focus
end if

Now, the lostfocus event should always be triggered
Top Expert 2010
Commented:
Similar to the Den_HBR's solution, when clicking on Save button in the toolstrip, set the focus to the toolstrip control, like this:


    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
        ToolStrip1.Focus()
        'update database below

    End Sub

Open in new window

Author

Commented:
Thank you all for your good answers. At the end I chose the solution that was easiest for me so even though 13598 solution works, it would have been a lot of work for me. The idea Den HBR is great and Zhaolai made it so simple as to just add one line of code to make it all work. Thanks again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial