XLL+ Class Library (7.0)

ExUIPopupCLR Sample

Demonstrates a popup editor within the Excel Formula Wizard (using .NET WinForms)

Overview

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.

Project setup

In order to make this project build and run, the following steps were required:

CFileNamePopupProvider

CFileNamePopupProvider::Edit does the 3 steps required of an editor.

  1. Creates a dialog (in this case by initializing a SaveFileDialog object) and initializes the values in the dialog (in this case, by setting the file name to be the value of strValue passed to Edit).
  2. Shows the dialog.
  3. If the user selects a file, sets 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.

CopyC++
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;
}

Registering the popup provider

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.

Classes and functions used

CXlWizExUIPopupProviderBase | CXlWizExUIPopupProviderBase::Edit | CXlWizExUIArgumentPopupCreator | CXlWizExUIArgumentPopupCreator::CXlWizExUIArgumentPopupCreator | WinWrapper

Sample project

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.

See Also

List of Sample Projects | Popup Editors (User Guide) | ExUIPopup sample | ExUIPopupMFC sample