mapbasic – lists of tables and windows

Upload: drio-rio

Post on 02-Jun-2018

230 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/11/2019 MapBasic Lists of Tables and Windows

    1/3

    ws

    Posted onJune 15th 2011byMr Chimp

    MapBasic is as powerful as it is frustrating. Theres no built-in way to get a list of open tables, so

    Ive written some code that does just that.

    First Illexplain 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 Ill package it all nicely into a sub and afunction so that you will only need a couple of lines of code. If you dont want to understand and

    just want to DO things then just skip to the last section!

    Getting a list of all open tablesThis 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 names array index value

    correspond with its MapInfo table number. Like so:

    1

    2

    3

    45

    6

    7

    8

    Dimi as integerDimj as integeri = NumTables()DimTableArray(1) as StringReDimTableArray(i)Forj = 1 to i

    TableArray(j) = Tableinfo(j, TAB_INFO_NAME)Next

    Choosing a table via a dialogNow 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):

    1

    23

    4

    5

    6

    78

    9

    1011

    12

    1314

    DimTableNum as IntegerDimTableName as StringDialog

    Title "Choose table"Control PopupMenu

    Title From Variable TableArray

    Into TableNumControl OKButtonControl Cancelbutton

    IfCommandInfo(CMD_INFO_DLG_OK) ThenTableName = TableArray(TableNum)Note "You chose: "& TableName

    EndIf

    http://chimpy.wordpress.com/2011/06/15/mapbasic-lists-of-tables-and-windows/http://chimpy.wordpress.com/2011/06/15/mapbasic-lists-of-tables-and-windows/http://chimpy.wordpress.com/2011/06/15/mapbasic-lists-of-tables-and-windows/http://chimpy.wordpress.com/2011/06/15/mapbasic-lists-of-tables-and-windows/http://chimpy.wordpress.com/2011/06/15/mapbasic-lists-of-tables-and-windows/http://chimpy.wordpress.com/author/chimpy/http://chimpy.wordpress.com/author/chimpy/http://chimpy.wordpress.com/author/chimpy/http://chimpy.wordpress.com/author/chimpy/http://chimpy.wordpress.com/2011/06/15/mapbasic-lists-of-tables-and-windows/http://chimpy.wordpress.com/2011/06/15/mapbasic-lists-of-tables-and-windows/
  • 8/11/2019 MapBasic Lists of Tables and Windows

    2/3

    Choosing a table with a sub and a functionMapBasic 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.

    1

    2

    34

    5

    67

    8

    9

    10

    1112

    1314

    15

    16

    1718

    19

    2021

    22

    23

    2425

    26

    2728

    SubGetTableList (TableList() as String)Dimi as integerDimj as integeri = NumTables()ReDimTableList(i)Forj = 1 to i

    TableList(j) = Tableinfo(j, TAB_INFO_NAME)Next

    EndSub

    FunctionChooseTable AsStringDimTableNum AsIntegerDimTableArray(1) AsString

    CallGetTableList(TableArray)

    DialogTitle "Choose table"Control PopupMenu

    Title From Variable TableArrayInto TableNum

    Control OKButtonControl Cancelbutton

    IfCommandInfo(CMD_INFO_DLG_OK) ThenChooseTable = TableInfo(TableNum, TAB_INFO_NAME)

    EndIfEndFunction

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

    1

    2DimTableName as StringTableName = ChooseTable()

    and heres how youd get an array of table names.

    1

    2DimTableArray(1) as StringCallGetTableList(TableArray)

    You cant get much easier than that and if you can Id like toknow how you did it!

  • 8/11/2019 MapBasic Lists of Tables and Windows

    3/3

    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).