Introduction to MicroStation

MicroStation® is a 3D computer-aided-design (CAD) application for personal computers and workstations. MicroStation is produced by Bentley Systems, Inc.

MicroStation can be customised in various ways. This article shows how to use a Microsoft Common Dialog Color Control with MicroStation Visual Basic for Applications (MVBA).

Choose a Colour with MicroStation VBA

Questions similar to these, posed by MDL and VBA developers, appear on the Bentley Discussion Groups, typically the MicroStation Programming Forum.

Q How can I ask a user to choose a colour using MicroStation VBA?

LA Solutions: Example Color Picker project

A You're probably familiar with MicroStation's built-in color picker or color chooser dialog items. Unfortunately those, in programming terms, are MDL dialog items that your VBA macro can't use. The VBA-compatible alternative is Microsoft's Common Dialogs, which includes a Color Dialog.

ActiveX ColorPicker control

The Bentley Systems ColorPicker was a V8-era ActiveX control that was unsupported. It is not delivered with MicroStation CONNECT.

Microsoft Common Color Dialog

Microsoft provide the Common Dialogs that let a developer show a user one of several common Windows dialogs. Those include a file open, a file save, a font chooser, a color picker dialog and others.

Because the Color Chooser is a common dialog, you will probably have seen something similar in other Windows applications …

Microsoft: Color Picker

MicroStation VBA Example Project

We've created an example MicroStation VBA project ColorChooser.mvba. It shows you how to use the Microsoft Common Color Dialog in your VBA code. This example shows how to apply a selected colour to one or more elements. We don't show all the code here, because it's spread over several VBA modules and not all modules are relevant to the colour chooser question. The example UserForm is simple …

LA Solutions: Example Color Picker project

For the purpose of the example, the code behind the Line Colour and File Colour buttons is important. When user clicks the Line Colour button (cmdLineColourPicker), we pop the Microsoft Color Dialog …

Private Sub cmdLineColourPicker_Click()
    If SelectColour(m_lineColour) Then
        cmdLineColourPicker.BackColor = m_lineColour
        If Not m_oLocator Is Nothing Then
            m_oLocator.ElementColour = m_lineColour
        End If
        Debug.Print "m_lineColour=" & CStr(m_lineColour)
    End If
End Sub

The code behind the Fill Colour button (cmdFillColourPicker) is similar …

Private Sub cmdFillColourPicker_Click()
    If SelectColour(m_fillColour) Then
        cmdFillColourPicker.BackColor = m_fillColour
        If Not m_oLocator Is Nothing Then
            m_oLocator.FillColour = m_fillColour
        End If
        Debug.Print "m_fillColour=" & CStr(m_fillColour)
    End If
End Sub

When user clicks the Fill Colour button we first set the background colour of the VBA command button to the chosen colour. In other words, we provide confirmation to the user that her selected colour will be used.

Note that either command button invokes the same subroutine SelectColour to let the user select a colour. That subroutine pops the Microsoft Color Dialog.

When user clicks the Pick Element button, we invoke a VBA class that Implements ILocateCommandEvents. For the purpose of this example that class is not interesting: it waits for the user to identify an element and then applies the chosen colour to that element.

Download Example Project

You can download the example VBA ColorPicker project.

Copy the .mvba file to a known good location where MicroStation looks for VBA macros. For example, ..\Organization\Standards\Macros or any folder that configuration variable MS_VBASEARCHDIRECTORIES includes.

Open the project in the VBA editor. Read the notes in module modMain. That module also contains the keyin to run the example …

vba run [ColorChooser]modMain.Main

Questions

Post questions about VBA to the MicroStation Programming Forum.