How do you pass the owner window to overload the Show () method?

I am working on adding Excel that opens winform after the user clicks a button on the ribbon bar. This button must be modeless so that the user can still interact with the parent window, but must also remain at the top of the parent window. To do this, I am trying to pass the parent window as a parameter to the Show () method. Here is my code:

Ribbon1.cs

private void button2_Click(object sender, RibbonControlEventArgs e) { RangeSelectForm newForm = new RangeSelectForm(); newForm.Show(this); } 

The problem with this code is that the word 'this' refers to the ribbon class, not the parent window. I also tried going into Globals.ThisAddIn.Application.Windows.Parent. This causes a runtime error, "The best overloaded method match for" System.Windows.Forms.Form.Show (System.Windows.Forms.IWin32Window) "has some invalid arguments." What is the correct way to pass the parent window of Show ()?

In case this is appropriate, this is an Office 2010 application written in .NET 4.0 using C #.

EDIT --- based on Slaks answer

  using Excel = Microsoft.Office.Interop.Excel; ... class ArbitraryWindow : IWin32Window { public ArbitraryWindow(IntPtr handle) { Handle = handle; } public IntPtr Handle { get; private set; } } private void button2_Click(object sender, RibbonControlEventArgs e) { RangeSelectForm newForm = new RangeSelectForm(); Excel.Application instance = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); newForm.Show(new ArbitraryWindow(instance.Hwnd)); } 
+6
source share
1 answer

You need to create a class that implements IWin32Window and returns the Excel Application.Hwnd property.

For instance:

 class ArbitraryWindow : IWin32Window { public ArbitraryWindow(IntPtr handle) { Handle = handle; } public IntPtr Handle { get; private set; } } newForm.Show(new ArbitraryWindow(new IntPtr(Something.Application.Hwnd))); 
+14
source

Source: https://habr.com/ru/post/904463/


All Articles