Main Content

Integrate COM Component into Visual Basic Application

R2026b

Overview

In this example, you create a COM (Component Object Model) component using a MATLAB® function that takes a single input and creates a magic square of that size. Next, you integrate the component into a standalone Microsoft® Visual Basic® application. The application accepts the magic square size as input and displays the matrix in a ListView control box.

Note

ListView is a Windows® Form control that displays a list of items with icons. You can use a list view to create a user interface like the right pane of Windows Explorer. For more information about Windows Form controls, see the Microsoft Developer Network (MSDN) Library.

Create COM Component

Build the COM Component with the COM Component Compiler app or compiler.build.comComponent using the following information:

FieldValue
COM Component Name MagicSquareComp
Class Name Class1
File to Compile makesquare.m

For instance, if you are using compiler.build.comComponent, type:

buildResults = compiler.build.comComponent("makesquare.m",...
"ComponentName","MagicSquareComp",...
"ClassName","Class1");

For more details, see the instructions in Create a COM Component with MATLAB Code.

Create the Microsoft Visual Basic Project

Note

This procedure assumes that you are using Microsoft Visual Basic 6.0.

  1. Start Visual Basic.

  2. In the New Project dialog box, select Installed > Templates > Other Languages > Visual Basic > Windows Form Application as the project type and click Open. This creates a new Visual Basic project with a blank form.

  3. From the main menu, select Project > References to open the Project References dialog box.

  4. Select MagicSquareComp 1.0 Type Library from the list of available components and click OK.

  5. Returning to the Visual Basic main menu, select Project > Add Component... to open the Add New Item dialog box.

Create User Interface

After you create the project, add a series of controls to the blank form to create a form with the following settings.

Control TypeControl NamePropertiesPurpose

Frame

Frame1

Caption = Magic Squares Demo

Groups controls

Label

Label1

Caption = Magic Square Size

Labels the magic square text box.

TextBox

edtSize

 

Accepts input of magic square size.

CommandButton

btnCreate

Caption = Create

When pressed, creates a new magic square with current size.

ListView

lstMagic

GridLines = True

LabelEdit = lvwManual

View = lvwReport

Displays the magic square.

When the form and controls are complete, add the following code to the form. This code references the control and variable names listed above. If you have given different names for any of the controls or any variable, change this code to reflect those differences.

Public Class magicvb
    Private sizeMatrix As Double 'Holds current matrix size
    Private theMagic As MagicSquareComp.Class1 'MagicSquareComp object instance 

    Private Sub magicvb_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'This function is called when the form is loaded.
        'Creates a new MagicSquareComp class instance.
        On Error GoTo Handle_Error
        theMagic = New MagicSquareComp.Class1
        sizeMatrix = 0
        Exit Sub
Handle_Error:
        MsgBox(Err.Description)
    End Sub


    Private Sub ShowMatrix(matrixMagic As Object)
        'This function populates the ListView with the contents of 
        'y. y is assumed to contain a 2D array.
        Dim szSquare As Long
        Dim indxRow As Long
        Dim indxCol As Long
        Dim nLen As Long
        On Error GoTo Handle_Error
        'Get array size
        If IsArray(matrixMagic) Then
            szSquare = UBound(matrixMagic, 1)
        Else
            szSquare = 1
        End If
        lstMagic.Clear()
        lstMagic.Columns.Add("")
        For cIndx = 1 To szSquare
            lstMagic.Columns.Add(CStr(cIndx))
        Next
        lstMagic.View = View.Details
        For indxRow = 1 To szSquare
            Dim item As New ListViewItem(CStr(indxRow))
            For indxCol = 1 To szSquare
                item.SubItems.Add(Format(matrixMagic(indxRow, indxCol)))
            Next
            lstMagic.Items.Add(item)
        Next


        Exit Sub

Handle_Error:
        MsgBox(Err.Description)
    End Sub


    Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
        'This function is called when the Create button is pressed.
        'Calls the makesquare method, and displays the magic square.
        Dim matrixMagic As Object
        If sizeMatrix <= 0 Or theMagic Is Nothing Then Exit Sub
        On Error GoTo Handle_Error
        Call theMagic.makesquare(1, matrixMagic, sizeMatrix)
        Call ShowMatrix(matrixMagic)
        Exit Sub
Handle_Error:
        MsgBox(Err.Description)
    End Sub

    Private Sub edtSize_TextChanged(sender As Object, e As EventArgs) Handles edtSize.TextChanged
        'This function is called whenever the contents of the
        'Text box change. Sets the current value of Size.
        On Error Resume Next
        sizeMatrix = CDbl(edtSize.Text)
        If Err.Number &gt; 0 Then
            sizeMatrix = 0
        End If
    End Sub

End Class

Create Executable in Microsoft Visual Basic

After the code is complete, create the standalone executable magic.exe:

  1. Reopen the project by selecting File > Save Project from the main menu. Accept the default name for the main form and enter magic.vbp for the project name.

  2. Return to the File menu. Select File > Make magic.exe to create the finished product.

Testing the Application

Run the magic.exe executable. When the main dialog box opens, enter a positive value in the input box and click Create. A magic square of the input size appears.

The ListView control automatically implements scrolling when the magic square is larger than 4-by-4.