Demonstrates a popup editor within the Excel Formula Wizard (using .NET WinForms)
This project demonstrates how to show a popup editor for an argument
in the Excel Formula Wizard, using the .NET Common Language Runtime,
including WinForms.
It contains a single worksheet function, SimulateLife
.
A class CFileNamePopupProvider
is defined, which is derived
from CXlWizExUIPopupProviderBase,
and implements the virtual function Edit
.
In order to make this project build and run, the following steps were required:
System.dll
and System.Windows.Forms.dll
,
which can be found in the ".NET" page of the "Add New Reference..."
dialog.
(You can add an assembly reference by opening the
"Common Properties/References" page of the Project Properties dialog
and clicking "Add New Reference...".)
using namespace System::Windows::Formws;
#include <xlpclrforms.h>
.
CFileNamePopupProvider::Edit
does the 3 steps required of an editor.
strValue
passed to Edit
).
strValue
and returns true
.
The file name is surrounded by quotes, so that it is a legal Excel string value.
If the user presses Cancel, then Edit
returns false
.
Note that CFileNamePopupProvider
contains a data member,
m_strTitle
, which will be displayed in the title of the file dialog.
This member can be set to be different for each of the arguments for which
this editor is provided.
The helper class WinWrapper is used to create a temporary parent form for the dialog.
virtual bool Edit(CString& strValue, HWND hwndParent) { try { SaveFileDialog^ fd = gcnew SaveFileDialog(); fd->FileName = toClr(strValue)->Replace(_T("\""), _T("")); fd->Filter = toClr(_T("Text files (*.txt)|*.txt|All files|*.*")); fd->OverwritePrompt = false; if (m_strTitle.GetLength() > 0) fd->Title = toClr(m_strTitle); if (fd->ShowDialog(gcnew XllPlus::Forms::WinWrapper(hwndParent)) == DialogResult::OK) { strValue = toXl(toClr(_T("\"")) + fd->FileName + toClr(_T("\""))); return true; } } catch(System::Exception^) { } return false; }
To register the popup editor, an instance of CXlWizExUIArgumentPopupCreator
is declared at global scope.
The arguments to the constructor include (i) the name of the add-in function for which the
editor is being registered, (ii) the index of the argument to which it applies and
(iii) an instance of the popup provider, CFileNamePopupProvider
.
Note that a specialized title is provided in the constructor of
CFileNamePopupProvider
, which applies only to the popup editor
for this specific argument (DataFile
in SimulateLife
).
In this case the title is "Select a data file".
Note that the developer is responsible for constructing the instance of
CFileNamePopupProvider
, but the framework is responsible for
destroying it.
For this reason, if a destructor is provided for a popup provider class,
it must be declared as virtual
.
CXlWizExUIPopupProviderBase | CXlWizExUIPopupProviderBase::Edit | CXlWizExUIArgumentPopupCreator | CXlWizExUIArgumentPopupCreator::CXlWizExUIArgumentPopupCreator | WinWrapper
Each sample project is located in a sub-directory of the Samples directory of the XLL+ installation. To use the sample project, open the solution file ExUIPopupCLR.sln or the project file ExUIPopupCLR.vcproj.
You can enable debugging under Excel by using the Setup Debugging command in the XLL+ ToolWindow.
When delivered, the help files are excluded from the build.
You can enable the help build by selecting the files
ExUIPopupCLR.help.xml
and
ExUIPopupCLR.chm
in the Solution Explorer,
and using the right-click menu to view Properties.
Select the page "Configuration Properties/General" and
set the "Excluded from build" property to "No".
See Generating help
in the User Guide for more information.
List of Sample Projects | Popup Editors (User Guide) | ExUIPopup sample | ExUIPopupMFC sample