0

Update: Discusion revealed that the problem is only arising, when you have the custom control text box hosted within a wpf application that is again hosted via elementhost within a winforms application.

I have a WPF-CustomControl inherting from TextBox. I override the OnLostKeyBoardFocus method.

As part of this method I raise an event. One event handler is showing a MessageBox (this is not under my control). When the user closes the MessageBox the KeyBoardFocus is returned to the TextBox, directly. Even though, OnLostKeyboardFocus(...) has still not returned.

The automatic (re)focussing on my TextBox control, causes a whole range of problems for me. Can I circumvent this behavior in some way other than dispatching the event with Dispatcher.BeginInvoke().

class MyTextBoxCustomControl : TextBox {

    public event EventHandler<EditCompletedEventArgs> EditCompleted;

    private void OnEditCompleted(EditCompletedEventArgs e)
    {
        var handler = EditCompleted;
        if (handler != null) handler(this, e);
    }

    protected override OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e){

        base.OnLostKeyboardFocus(e);  

        OnEditCompleted(new EditCompletedEventArgs())

        //Before this point is reached OnGotKeyboardFocus(...) is called
    }

    protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
    {
        base.OnGotKeyboardFocus(e);

        //Is called twice, directly after MessageBox is closed and
        //after OnLostKeyboardFocus(...) returns
    }
}

class MyEventHandler {

    private void Test(){

        var myTBCC = new MyTextBoxCustomControl();

        //Closing the message box will return focus to myTBCC, which directly 
        //causes OnGotKeyboardFocus to be called
        myTBCC.EditCompleted += (a, b) =>  
                                    MessageBox.Show("PressOk");                    
    }
}
user1182735
  • 764
  • 9
  • 21

1 Answers1

1

You could try call .MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); on sender right after MessageBox.Show.

EDIT:

...

MessageBox.Show("PressOk");
((MyTextBoxCustomControl)a).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

...
majk86
  • 125
  • 8
  • I tried this. Resulting in a problem in the follwing case, when the text box is in focus and then user clicks in other window: OnLostKeyboard is called, messagebox is shown, user clicks ok, MoveFocus is called; this results in (re)focussing on the wpf window, which results in (re)focussing of my text box control, resulting in OnGotKeyboardFocus getting called, then, finally, focus is moved to next element in my wpf window. – user1182735 Jun 20 '13 at 13:56
  • 1
    Tried your code and it works even without `MoveFocus`. Right after leaving your custom control `MessageBox` is shown and focus moves to the next control. – majk86 Jun 20 '13 at 14:12
  • My problem is that OnGotKeyboardFocus on my text box custom control is called, when the OnLostKeyboardFocus has not yet returned. Where the focus at the very end is, does not matter to me that much. Can you please check this as well, when you have the code running already? So that I can rule out that smth. completely unrelated causes this behavior. In my code OnGotKeyboardFocus still gets called. – user1182735 Jun 20 '13 at 14:23
  • hmmm, I am wondering. Maybe it has indeed to do with smth. "unrelated". I have to host my wpf application inside a winforms application via ElementHost. Maybe this is causing the problem. – user1182735 Jun 20 '13 at 14:42
  • Just a question. Is there specific reason to use `...KeyboardFocus`? Tried with `OnFocusLost` and `OnGotFocus` and methods are called once. – majk86 Jun 20 '13 at 14:43
  • Ok, I did some pure wpf tests and the behavior differs. I this case I only get the OnGot... called, when I click in another window, when my text box has focus. When I only click within my wpf everything is ok. – user1182735 Jun 20 '13 at 14:55
  • Yeah, there were specific reasons. But all this implementation was some while ago and I kind of forgot why... I still remember that I had a reason for using KeyBoardFocus, though. I even provided some comments in code to explain unintuitive stuff, but this explanation I did not write down... – user1182735 Jun 20 '13 at 14:58
  • Ok, I came to the conclusion that the problematic behavior is only visible when hosting wpf in winforms via elementhost. Sorry, for the incomplete problem description. Still, you helped me identifying the root problem. – user1182735 Jun 20 '13 at 15:03