tags:

views:

2192

answers:

5

Hi,

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?

+1  A: 

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 "CausesValidating" set to "true" ( its default value ) the "Validating" and "Validated" event will be fired.

The menu has "CuasesValidating" property too.

Edit: Sorry, I forgot the "CuasesValidating" 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
    }
}

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

TcKs
I cant find the CausesValidating property on the Toolstripmenuitem or the menustrip...
Stefan
I cant find the CausesValidating property on the Toolstripmenuitem as well
Grzenio
A: 

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

Fredou
+1  A: 

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.

Stefan
It's fair enough that the menu shouldn't steal focus from the control - but on the other hand there is no mechanism to handle my scenario in a reasonably simple way. Your method is OK in simple cases, but I have a hierarchy of user controls used on different forms, so I can't really have LastControl
Grzenio
Yeah, I was ready to post an example that dynamically forwarded a InvokeLostFocus(LastControl) when the Menu was activated. But then I saw your workaround with menustrip1.focus() so I didnt. ;)
Stefan
+2  A: 

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();
    }
Grzenio
It works only for mouse, but not for keyboard. Please see my edit.
TcKs
A: 

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
Fredou
I know it's VB.Net code but it should be the same logic under C#
Fredou
Doesn't seem to work though - ActiveControl is the menu item I clicked on :(
Grzenio