Responding to Menu Item Selection

The following exercise adds code to respond to user selection of the menu items added in the previous exercise.

Add the following method to the MyAddIn class:

Private Function IsNameUsed(szName As String, _

    col As Collection) As Boolean

  On Error GoTo IsNameUsed_Err

  Dim vTmp As Variant

  Set vTmp = col(szName)

  IsNameUsed = True

  Exit Function

IsNameUsed_Err:

  IsNameUsed = False

  Err.Clear

  Exit Function

End Function

Add the following code to the IOlapAddIn_ExecuteMenuItems method:

Private Function IOlapAddIn_ExecuteMenuItem( _

    CurrentNode As DSSAddInsManager.OlapTreeNode, _

    MenuItem As DSSAddInsManager.OlapMenuItem) _

    As DSSAddInsManager.RefreshTreeTypes

  On Error GoTo IOlapAddIn_ExecuteMenuItem_Err

  Dim frmSample As SampleForm

  Dim szFormCaption As String

  Dim szNodeCaption As String

  Dim iFormIndex As Integer

  

  szNodeCaption = CurrentNode.Caption

  

  Select Case MenuItem.Key

    Case mnuactRename

      Dim szName As String

      Dim tmpForm As SampleForm

      Set tmpForm = m_SampleForms(szNodeCaption)

      

      m_SampleForms.Remove szNodeCaption

      

      Do

        szName = InputBox("Please enter the new name:", _

            "Rename a Form", szNodeCaption)

        If Len(szName) = 0 Then

          MsgBox _

              "The name must not be a zero length string", _

              vbExclamation, "Invalid Name"

        Else

          Exit Do

        End If

      Loop

      

      tmpForm.Caption = szName

      

      If tmpForm.Index <= m_SampleForms.Count Then

        m_SampleForms.Add tmpForm, szName, tmpForm.Index

      Else

        'This is the only item in the list

        'or it was at the end of the list.

        'No need to specify a before value

        m_SampleForms.Add tmpForm, szName

      End If

      

      'Manually tell OLAP Add-In Manager to refresh the tree

      IOlapAddIn_ExecuteMenuItem = reftreeCurrentAndBelow

      

    Case mnuactShowTop

      Set frmSample = m_SampleForms(szNodeCaption)

      frmSample.Move (Screen.Width - frmSample.Width) / 2, 0

      frmSample.Show vbModal

      

    Case mnuactShowCenter

      Set frmSample = m_SampleForms(szNodeCaption)

      frmSample.Move (Screen.Width - frmSample.Width) / 2, _

          (Screen.Height - frmSample.Height) / 2

      frmSample.Show vbModal

      

    Case mnuactShowBottom

      Set frmSample = m_SampleForms(szNodeCaption)

      frmSample.Move (Screen.Width - frmSample.Width) / 2, _

          Screen.Height - frmSample.Height

      frmSample.Show vbModal

      

    Case mnuactAddNewForm

      Set frmSample = New SampleForm

      iFormIndex = m_SampleForms.Count

      

      Do

        iFormIndex = iFormIndex + 1

        szFormCaption = "Sample Form " & iFormIndex

      Loop While IsNameUsed(szFormCaption, m_SampleForms)

      

      frmSample.Caption = szFormCaption

      frmSample.Index = iFormIndex

      

      m_SampleForms.Add frmSample, szFormCaption

      

      Set frmSample = Nothing

      

      'Tell OLAP Add-In Manager to refresh the tree

      IOlapAddIn_ExecuteMenuItem = reftreeCurrentAndBelow

 

    Case mnuactDeleteSampleForm

      m_SampleForms.Remove szNodeCaption

      

    Case mnuactRefreshList

      IOlapAddIn_ExecuteMenuItem = reftreeCurrentAndBelow

      

  End Select

  Exit Function

IOlapAddIn_ExecuteMenuItem_Err:

  Debug.Print Err.Number, Err.Description, Err.Source

  Debug.Assert False

  MsgBox "Execute Menu Item Failed"

  Err.Clear

  Exit Function

End Function

Run the application.

With the Microsoft® Visual Basic® project executing, start the OLAP Manager, and then browse the tree view. Right-click one of the new tree nodes, and then click a menu item.

(c) 1988-1998 Microsoft Corporation. All Rights Reserved.