TWIAV - Tips & tricks

TWIAV - Tips & tricks

MapBasic Code Samples

On this page you will find some MapBasic code samples, both the program source and the compiled application. Everything on this page is offered "as is" without warranty of any kind, either expressed or implied, including limitation warranties of merchantability, fitness for a particular purpose, and noninfringement.

Unless otherwise stated MapBasic 9.0 has been used to compile the files - this means that the MBXs offered here will only run with MapInfo Professional 9.0 or higher. If you want to use the tools with another (an older) version of MapInfo Professional, you will have to compile the tool using the relevant version of the Mapbasic compiler.

The MapBasic Code Samples Project

The goal of this project is to provide some examples of how to use certain MapBasic functionality - not to offer full-blown applications. The applications below each focus on one 'trick'. You can study the source, modify it to suit your needs and implement it in your own apps.

The following code samples are available here:

MultiListBox Sample Code

This application demonstrates how to use a MultiListBox control in your MapBasic application.

This is done in 4 steps:

  • An array is populated with values to choose from (French regions)

  • An array is populated with values actually chosen from the MultiListBox

  • The array with the selection is used to populate a string to be used in an SQL query (to actually highlight the French regions selected on the map)

  • And last but not least:
    The array with the selection is used to highlight the previous selection when the dialog with the MultiListBox is opened again

Please see the file below for MultiListBox.MB and MultiListBox.MBX.

The table used in the example - FR_Départements2006.TAB - is also included in the zip file:

Custom Function

In this application a custom function is used. This function - TableIsOpen() - lets you check whether a certain table is open or not.

In this document you will find description of how to create and use a custom function in MapBasic. The function TableIsOpen() is used as an example.

Processing a MultiListBox control

To read what items the user selected from a MultiListBox control, assign a handler procedure that is called when the user dismisses the dialog box (for example, assign a handler to the OKButton control). Within the handler procedure, set up a loop to call the ReadControlValue( ) function repeatedly.

The first call to the ReadControlValue( ) function returns the number of the first selected item; the second call to the ReadControlValue( ) function returns the number of the second selected item; etc. When the ReadControlValue( ) function returns zero, you have exhausted the list of selected items. If the first call to the ReadControlValue( ) function returns zero, there are no list items selected.

More code samples will follow...;-)

What do you think of these code samples? If you have any , please let me know.