none
Custom modeless WPF Find and Replace dialog in Excel RRS feed

  • Question

  • Hi,

    In my project, we are creating a custom Find and Replace dialog for Excel using WPF window. Used WindowInteropHelper to set the Excel window as the parent of Find and Replace dialog. The issue what we are facing is, Excel displays the selection(including ActiveCell) only if the focus is on the Excel window, when the focus goes to WPF window, selected range is not displayed in the Excel Active window. We are trying to mimic the Excel behavior here, that is, in Excel if we launch Find and Replace dialog, though the focus is in Find and Replace dialog, the Excel active window still shows the selection. Please help me in achieving this behavior? We are using C# .Net 4.0

    Thanks in advance.


    • Edited by sree' Friday, January 29, 2016 11:29 AM
    Friday, January 29, 2016 11:29 AM

All replies

  • Hello,

    After displaying a non-modal form try to use the Activate method of the Worksheet class which allows to make the current sheet the active sheet.

    Friday, January 29, 2016 3:08 PM
  • Thank you Eugene.

    My requirement is to display the selection in worksheet even if the focus is on the dialog, similar to Excel's Find and replace dialog.

    When I try to edit in textbox in the dialog, the focus shifts to Excel and the inputs are entered in Excel's active cell instead of on my WPF textbox, I followed the below links to resolve this issue, that is, launching the non-modal WPF dialog in a separate thread. Is there any chance, this may cause the focusing issue?

    1. http://reedcopsey.com/2011/11/28/launching-a-wpf-window-in-a-separate-thread-part-1/
    2. http://stackoverflow.com/questions/5869359/wpf-modeless-dialog-from-ms-excel-add-in
    3. https://eprystupa.wordpress.com/2008/07/28/running-wpf-application-with-multiple-ui-threads/

    Thanks,

    -Sree

    Saturday, January 30, 2016 4:31 AM
  • You need to deal with any UI elements only on the main thread.
    Saturday, January 30, 2016 11:40 AM
  • Thank you Eugene.

    If I create the dialog in main thread, then as I said I was not able to enter text in the WPF control. Any idea, how to deal with this without creating a separate thread for dialog?

    Thanks,

    -Sree

    Sunday, January 31, 2016 3:02 AM
  • Hi,

    As part of our work, I want to launch a custom Find and Replace dialog from Excel. I'm able to  launch a WPF window as a modeless dialog, and used WindowInteropHelper to assign Excel as parent for the dialog. The issue what i'm facing is, if I enter text in dialog textbox, it is getting entered in the active cell in Excel. I found solution for this issue in the following links,

    1. http://reedcopsey.com/2011/11/28/launching-a-wpf-window-in-a-separate-thread-part-1/
    2. http://stackoverflow.com/questions/5869359/wpf-modeless-dialog-from-ms-excel-add-in
    3. https://eprystupa.wordpress.com/2008/07/28/running-wpf-application-with-multiple-ui-threads/

    Links are suggesting to create separate thread for the dialog. If I do so, then Excel is not displaying the selection in the worksheet when focus is in dialog. I want to mimic the same behavior as Excel Find and Replace dialog, that is, though the focus is in Excel Find and Replace dialog, the selection in the worksheet will be displayed. If I didn't use separate thread for the WPF dialog creation(creating dialog in the main thread) I could able to achieve the selection behavior as expected, but keyboard inputs are not reflecting in the dialog.

    Please help me in resolving the keyboard text entering issue with the modeless dialog without the usage of a separate thread for dialog creation.

    Using C# .Net 4.0

    Thanks in advance.


    • Edited by sree' Sunday, January 31, 2016 8:36 AM
    • Merged by Xavier Xie-MSFT Monday, February 1, 2016 6:18 AM the same question
    Sunday, January 31, 2016 8:33 AM
  • Since the Excel expects the UI stuffs to be handled is main thread to display the selection as expected, I thought the issue is related to WPF, so posted the issue in WPF forum. 

    I'm in need of a solution or a  workaruond for this issue. Please help me.

    Thanks

    -Sree

    Monday, February 1, 2016 12:18 PM