MapBasic – Lists of tables and windows

MapBasic is as powerful as it is frustrating. There’s no built-in way to get a list of open tables, so I’ve written some code that does just that.

First I’ll explain how to populate an array with a list of table names and numbers, then how to select a table from this list using a dialogue box, then I’ll package it all nicely into a sub and a function so that you will only need a couple of lines of code. If you don’t want to understand and just want to DO things then just skip to the last section!

Getting a list of all open tables
This is fairly simple. NumTables() returns the total number of tables opened. All open tables are numbered sequentially, starting from 1. Therefore we just need to loop through, from 1 to NumTables() and put all the names into an array, making each name’s array index value correspond with its MapInfo table number. Like so:

    Dim i as integer
    Dim j as integer
    i = NumTables()
    Dim TableArray(1) as String
    ReDim TableArray(i)
    For j = 1 to i
        TableArray(j) = Tableinfo(j, TAB_INFO_NAME)
    Next

Choosing a table via a dialog
Now that you have a variable (TableArray) that contains a list of all open table names with corresponding numbers. This can then be used in dialog boxes, for example in a drop-down menu (or as MapBasic calls it, a pop-up menu):

    Dim TableNum as Integer
    Dim TableName as String
    Dialog
        Title "Choose table"
        Control PopupMenu
            Title From Variable TableArray
            Into TableNum
        Control OKButton
        Control Cancelbutton

    If CommandInfo(CMD_INFO_DLG_OK) Then
        TableName = TableArray(TableNum)
        Note "You chose: " & TableName
    End If

Choosing a table with a sub and a function
MapBasic functions cannot return arrays. Therefore you must create an array variable to store the list and then use a sub to insert the data into the array.

Here are is everything above packaged into a sub and a function.

Sub GetTableList (TableList() as String)
    Dim i as integer
    Dim j as integer
    i = NumTables()
    ReDim TableList(i)
    For j = 1 to i
        TableList(j) = Tableinfo(j, TAB_INFO_NAME)
    Next
End Sub

Function ChooseTable As String
    Dim TableNum As Integer
    Dim TableArray(1) As String
    
    Call GetTableList(TableArray)
    
    Dialog
        Title "Choose table"
        Control PopupMenu
            Title From Variable TableArray
            Into TableNum
        Control OKButton
        Control Cancelbutton
        
    If CommandInfo(CMD_INFO_DLG_OK) Then
        ChooseTable = TableInfo(TableNum, TAB_INFO_NAME)
    End If
End Function

…and here is how you would use them to choose a layer:

Dim TableName as String
TableName = ChooseTable()

…and here’s how you’d get an array of table names.

Dim TableArray(1) as String
Call GetTableList(TableArray)

You can’t get much easier than that… and if you can I’d like to know how you did it!

You can use exactly the same procedure to get a window name or a list of windows. You just need to swap “table” for “window” and “TAB” for “WIN” (literally – a search and replace will work).

Advertisements

About Mr Chimp

I make music, draw pictures, browse the internet, programme, and make sweet, sweet cups of tea until the early hours.
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s