TextBox overlay placed over Excel cell doesn't receive TextChanged events — Excel captures keystrokes (VSTO)

Mansour_Dalir 2,036 Reputation points
2025-11-23T09:34:58.2666667+00:00

hi
I created a VSTO add-in that displays a WPF TextBox overlay positioned exactly over the currently selected Excel cell so users can edit inline. The overlay is visible and aligned correctly, but when I type, Excel captures all keystrokes and the TextBox does not receive TextChanged or KeyDown events.

VSTO TextChange_Event.txt

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Gade Harika (INFOSYS LIMITED) 1,870 Reputation points Microsoft External Staff
    2025-11-24T06:18:21.9966667+00:00

    Thanks for reaching out.
    A WPF TextBox placed over the active Excel cell shows correctly, but typing goes to Excel. The TextBox never receives TextChanged or KeyDown.

    you can use in a VSTO add‑in to make a WPF TextBox overlay over the active Excel cell receive KeyDown/TextChanged reliably. It addresses Excel’s focus‑stealing during selection changes and in‑cell edit mode.
    1) WPF overlay window (top‑level, focusable)
    please check below code

    CellOverlayWindow.xaml.txt

    2) VSTO wiring (subscribe, position, focus, commit/cancel)
    please check below code
    note 2 code.txt

    3. What this solves (and why it works)

    • Excel steals focus on selection changes and when entering in‑cell edit.
    • The overlay is a top‑level, focusable WPF window. We defer Activate() + Focus() using Dispatcher.BeginInvoke (ApplicationIdle) so it executes after Excel’s own activation finishes.
    • We temporarily disable Application.EditDirectlyInCell while the overlay is shown, so Excel doesn’t switch to in‑cell edit and monopolize the input pipeline.
    • Optional Win32 focus (SetForegroundWindow + SetFocus) is available for stubborn environments.
    • Enter/Esc commit/cancel, then we restore focus to Excel and revert EditDirectlyInCell.

    4) Usage notes

    • If you support IME/East Asian input, also wire PreviewTextInput and PreviewKeyDown on the TextBox.
    • Avoid AllowsTransparency=True/hit‑test transparent windows; they won’t own keyboard focus.
    • If your layout includes borders/margins, adjust overlay.Width/Height accordingly.

    Let us know if the issue persists after following these steps. I’ll be happy to assist further if needed.

    If the issue has been resolved, Kindly mark the provided solution as "Accept Answer", so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.