Windows Forms above Excel - c#

I have a form which spawns from an excel add-in written with VSTO.
I would like the form to remain always above the excel spreadsheet, but still allow the user to interact with the sheet below it.
The forms constructor includes this.TopLevel = true;, but the form still disappears behind the spreadsheet when I select a cell.
Has anyone else come across this problem and found a workaround?

A form already has TopLevel set to true. You want TopMost.
P/Invoking SetParent() would be best but getting the window handle you need might not be that easy. Maybe Process.GetCurrentProcess().MainWindowHandle

if you want to have an interactive form in Excel you'll need to do some pinvoking as Hans said in the original answer.
see this link - on MSDN
This example is for the refedit control (which is normally only available from VBA).
The example should give you the necessary information on IWin32Window and pinvoking to call the Show method of the form object with this parameter value.

Related

c# VSTO Host WPF form inside Excel sheet

I would like to embed a standalone WPF form inside an Excel sheet.
Microsoft provide information on how to host a WPF form inside an Excel Action Pane or Task Pane here: https://msdn.microsoft.com/en-us/library/bb772076.aspx
However, I'm unable to find information on how I can do this within the Excel sheet itself. Is this possible? Should I just load the form when the user clicks on the tab? Or is there a better way to do this?
I've never seen that anywhere so I guess it's not possible.
What I have done a few times (but never in production) is your second suggestion
Should I just load the form when the user clicks on the tab? Or is there a better way to do this?
Yes, this should work fine, there is something like WPF host you can use on standard form or create another project in your solution (WPF project) and call it from VSTO

Attach a .net control to a worksheet without VSTO (but Excel-DNA)

I'm trying to do something that I'm not sure is possible.
I want to use a .net 3rd party library that gives nice winform chart controls, to replace some (very complex and ugly) VBA generated charts in an Excel worksheet.
I'm using Excel-DNA (which is great BTW), and I managed to do all sorts of things with it, like displaying Winform forms in both sync and async way (thanks to https://github.com/Ron-Ldn/DotNetRefEdit)
So far, I managed to make my non modal form with the charts appear when I activate a particular worksheet, and by setting it's parent and topMost properties, it kinda have the expected behavior. But placing it is a real pain (different screen sizes) and I was wondering if there is a way to properly do it
So here's the question : Can I attach in some way a .Net form or a custom control to a worksheet, maybe even have it positionned relatively to a particular cell ? It must be non modal and it will display only if we are on a particular sheet.
I know with VSTO you could add a control to a worksheet, but I don't want to use VSTO. I also know that I could probably create my own ActiveX control and have it registered, and then place it in Excel like I would with another button control for example, but that seems hazardous according to what I read about it
Thank you for all ideas or suggestions
Consider creating a custom task pane (CTP) pinned to the right hand side of the Excel workbook (worksheets).
For more information on how to do this in ExcelDNA, see ExcelDNA GitHub Sample on CTP.

Graphical Failure with InputBox and MessageBox

In my WPF-project im popping up an Microsoft.VisualBasic.Interaction.InputBox to get a filename from the user. If this filename is already in use im popping up a System.Windows.MessageBox to show the hint, that its already in use. After i hit OK on the MessageBox and it closed, i still see parts of the InputBox mixed up with some elements(a button) from my main programm.
Why does this happen?
Thanks in advance
You generally don't want to use any Windows.Forms based controls in a WPF application, it's just somehow against the point. You can easily create your own input box though, with the advantage of total control over behavior and style. This link stackoverflow describes your problem very nicely.

c# : VSTO -> MS Office Apps loosing focus?

So, what is happening, that Im loosing the focus from the Excel App to the PowerPoint App ?
UPDATE:
Your answers are correct (+1 Point), however its my fault, for not explaining the complete problem (I thought it is much simple).
So, the problem statement expands to : From a MS PowerPoint, I open MS Excel, from where I open the ColorDialog. So, the problem is to gain the IWin32Window owner (or its handle) of the calling Excel App.
I used a modified solution from How do you pass the owner window to Show() method overload? to find the IWin32Window owner, and it works.
However, even doing so, the focus goes back to the parent PowerPoint App.
So, what is happening, that Im loosing the focus from the Excel App to the PowerPoint App ?
If you are launching the form from a different form, you should set that as the Owner of the ColorDialog:
if (dlg.ShowDialog(this) == DialogResult.OK) //"this" being the owner form

Get ActiveX Object properties

In short: I have a VB6 form with ActiveX control that contains buttons, and I would like to create automatic tool that can click this buttons using C#.
I tried to use reflection for this issue, but I cannot access the form.
The general direction I was suggested was to use window API to access the form, but it seem very "expensive" solution.
Does anyone familiar with this subject?
Thank you
Can you use AutomationElement from .net?
To test this:
download UiSpy.zip link taken from this question
Get the form open, activate the hover mode. If you "see" the button it should be easy to use ] AutomationElement to actually send a click to it.
How about sending/hooking window messages (e.g. sending WM_MOUSEDOWN or WM_MOUSEMOVE)? Short/simplified example can be found here.
If your goal is to automate the UI for testing, I would suggest TestComplete from SmartBear Software which can directly access your VB6 forms, controls and properties. They're basically hooking into the process and accessing the "Forms" object and "Screen" object to get access to the loaded forms.

Categories

Resources