HighTechTalks DotNet Forums  

Owned forms in Excel add-in

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss Owned forms in Excel add-in in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Tom
 
Posts: n/a

Default Owned forms in Excel add-in - 06-15-2007 , 03:41 AM






Hi,

I want to use forms in my Excel application-level add-in that have the
main Excel window set as their owner. Could someone who knows these
thins tell me, am I disposing the handle correctly?

public partial class ExcelForm : Form
{
NativeWindow nativeWindow;

public void Show(IntPtr handle)
{
nativeWindow = new NativeWindow();
nativeWindow.AssignHandle(handle);
base.Show(nativeWindow);
}

protected override void Dispose(bool disposing)
{
if (disposing && (components != null)) // Windows Form
Designer generated code
{
components.Dispose();
}

// Is this the correct way and place?
if (nativeWindow != null)
{
nativeWindow.ReleaseHandle();
}

base.Dispose(disposing);
}
}

Usage:
ExcelForm excelForm = new ExcelForm();
excelForm.Show(new IntPtr(Application.Hwnd));


Reply With Quote
  #2  
Old   
Ben Voigt [C++ MVP]
 
Posts: n/a

Default Re: Owned forms in Excel add-in - 06-15-2007 , 01:50 PM







"Tom" <tom.kostiainen (AT) saunalahti (DOT) fi> wrote

Quote:
Hi,

I want to use forms in my Excel application-level add-in that have the
main Excel window set as their owner. Could someone who knows these
thins tell me, am I disposing the handle correctly?

public partial class ExcelForm : Form
{
NativeWindow nativeWindow;

public void Show(IntPtr handle)
{
nativeWindow = new NativeWindow();
nativeWindow.AssignHandle(handle);
base.Show(nativeWindow);
}

protected override void Dispose(bool disposing)
{
if (disposing && (components != null)) // Windows Form
Designer generated code
{
components.Dispose();
}

// Is this the correct way and place?
if (nativeWindow != null)
{
nativeWindow.ReleaseHandle();
Are you trying to leave the window open after your .NET form is disposed?
That's what ReleaseHandle does, detaches the Windows object from the .NET
object. If you want the window to close just do nothing, base.Dispose()
below will cleanup the window.

Quote:
}

base.Dispose(disposing);
}
}

Usage:
ExcelForm excelForm = new ExcelForm();
excelForm.Show(new IntPtr(Application.Hwnd));



Reply With Quote
  #3  
Old   
Tom
 
Posts: n/a

Default Re: Owned forms in Excel add-in - 06-21-2007 , 05:47 AM




The native window I'm passing is the main window of Excel.
So I guess the answer is yes, I need the window to stay open after
my .NET child form closes.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.