Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2005

Tip: Looking for answers? Try searching our database.

Showing Dialog form in Excel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
test.file@gmail.com - 13 Jun 2005 21:34 GMT
I'm working on a project (SDK 1.1) where we're automating Excel XP. We
have an ExcelService class which is responsible to maintaining Excel
object states and cleanup.  The class also acts as a sink for the click
events of several buttons that it creates in Excel Commandbars.

Some of these buttons are required to show WinForms as if they were
launched from Excel (replacement for UserForms).  These winforms need
to be modal because of other dependencies.

What is the best way to achieve this?  (A try-finally block with
EnableWindow on Excel while the WinForm is loaded as ShowDialog with
SetForegroundWindow from its activate?)
test.file@gmail.com - 13 Jun 2005 21:46 GMT
Please ignore... I should've tested my own theory.  This seems to work.

In the ExcelService class, I have
try
            {
                m_frm = new ContractSelectionForm( mode );
                m_frm.Closed +=new EventHandler(m_frm_Closed);
                EnableWindow( new IntPtr(base.GetExcelhWnd()), false);
                m_isWndEnabled = false;
                m_frm.ShowDialog();
            }
            catch (Exception exc)
            {
                throw new AdapterServiceException(exc);
            }
            finally
            {
                if (!m_isWndEnabled)
                {
                    EnableWindow( new IntPtr(base.GetExcelhWnd()), true);
                    m_isWndEnabled = true;
                }
            }

and in the form, I have

        private void ContractSelectionForm_Activated(object sender,
System.EventArgs e)
        {
            if( !GetForegroundWindow().Equals( this.Handle ) )
            {
                ShowWindow(this.Handle, SW_RESTORE);
                SetForegroundWindow(this.Handle);
            }
        }

However, this creates another problem.  While the dialog is open and
the user clicks around in the Excel instance, hte mouseClicks seem to
get queued up, only to take place when the dialog is dismissed. Is
there a workaround for this?

Thanks
Thanks
test.file@gmail.com - 14 Jun 2005 13:11 GMT
Well, there's a simpler workaround all this.  All I had to do was
implement IWin32Window in my ExcelService class and return Excel's
hWnd.

        public IntPtr Handle
        {
            get
            {
                return new IntPtr (base.GetExcelhWnd());
            }
        }

And in ShowDialog, pass the class as the owner.

m_frm.ShowDialog(this);

Even though this is not a form, the interface implementation was enough
to allow me to show the WinForm as modal to C# and also to Excel (and
displayed on top of Excel).

RTFM, I guess.

Thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.