Grzenio
Grzenio

Reputation: 36649

WinForms: Textbox Leave event doesn't get fired after going to main menu

I have a TextBox control on my Form. I use the Leave event on the control to process user input. It works fine if the user clicks on some other control on the form, but the even doesn't get fired when the user goes straight to the main menu. Any ideas which event should I use to get it fired everytime?

Upvotes: 7

Views: 12848

Answers (5)

TcKs
TcKs

Reputation: 26632

You should use "Validating" and "Validated" events for checking user's input. Then if user go to another control "A", and the control "A" has property "CausesValidation" set to "true" ( its default value ) the "Validating" and "Validated" event will be fired.

The menu has "CausesValidation" property too.

Edit: Sorry, I forgot the "CausesValidation" in menu strip is our functionality and not built-in. But the check for validating is pretty simple:

private void menuStrip1_MenuActivate( object sender, EventArgs e ) {
    bool ret = this.Validate( false );
    if ( false == ret ) {
        // user's input is wrong
    }
}

Use any ContainerControl instead of "this", if you want check the validation in another control than the "this" form. For example in MDI Child window.

Upvotes: 2

Grzenio
Grzenio

Reputation: 36649

I found a reasonable workaround, I set the focus on the main menu manually:

EDIT: As suggested by @TcKs, I changed the event from ItemClicked to MenuActivate. Thanks very much for help!

    private void menuStrip1_MenuActivate( object sender, EventArgs e )
    {
        menuStrip1.Focus();
    }

Upvotes: 6

Fredou
Fredou

Reputation: 20120

you need a label 1 pixel per 1 pixel

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
    MsgBox("yes")
End Sub

Private Sub MenuStrip1_MenuActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuStrip1.MenuActivate
    CType(sender, MenuStrip).Tag = ActiveControl
    Label1.Focus()
End Sub

Private Sub MenuStrip1_MenuDeactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuStrip1.MenuDeactivate
    If CType(sender, MenuStrip).Tag Is Control AndAlso CType(CType(sender, MenuStrip).Tag, Control).CanFocus Then
        CType(CType(sender, MenuStrip).Tag, Control).Focus()
    End If
    CType(sender, MenuStrip).Tag = Nothing
End Sub

Upvotes: -1

Stefan
Stefan

Reputation: 11509

There are some cases when Lostfocus is not fired, for example clicking toolbar buttons and menu items. I use to work around this with a local "LastControl" variable and handle it myself when the menu got focus.

There are reasons that menu click does not loses the textbox focus. If you want to have for example an "Edit" menu with "Paste" in, the "Paste" should act against the control that has focus and because of that it must not steal the focus from any controls on the form.

So the menu can be seen as a context menu that not steal the focus from the control.

Upvotes: 1

Fredou
Fredou

Reputation: 20120

when playing with a menu, you have to simulate, somehow, a lostfocus and maybe remembering where it was in case you close the menu without doing anything so the focus would return into the textbox

Upvotes: 0

Related Questions