Example: Creating a Dialog Box Launched from a Menu

You can create a dialog box when you need to enter parameters during OLE automation through VB scripts.

In this example, we will create a new dialog box that will be launched from a new "Export command in the contextual menu of extended objects, and which allows you to enter a path to where the extended object should be exported.

  1. Right-click the ExtendedObject metaclass in the resource editor and select New > Form.
  2. Type Export in the Name box, and select Dialog Box in the type list.
  3. Click the Edit Field tool to add an edit field control, and name it "Filename".
  4. Right-click the ExtendedObject metaclass and select New > Method. This creates a new method, you can call it Export.
  5. Name the method "Export", click the Method Script tab and enter the following code:
    Sub %Method%(obj)
     ' Exports an object in a file
     
     ' Create a dialog to input the export file name
     Dim dlg
     Set dlg = obj.CreateCustomDialog
    		("%CurrentTargetCode%.Export")
     If not dlg is Nothing Then
      
      ' Initialize filename control value
      dlg.SetValue "Filename", "c:\temp\MyFile.txt"
      
      ' Show dialog
      If dlg.ShowDialog() Then
       
       ' Retrieve customer value for filename control
       Dim filename
       filename = dlg.GetValue("Filename")
       
       ' Process the export algorithm...
       Output "Exporting object " + obj.Name + " to file " + filename
       
      End If
      
      ' Free dialog object
      dlg.Delete
      Set dlg = Nothing
      
     End If
     
    End Sub
  6. Right-click the ExtendedObject metaclass and select New > Menu to create a new menu entry in its contextual menu (see Menus (Profile)).
  7. Enter the name "Export" and then click the Add Commands from Methods and Transformations tool and select the Export method.
  8. Click OK to save your changes and close the Resource Editor.
  9. Right-click an extended object and select the Export command in its contextual menu to launch the Export dialog box.