Thursday, 19 June 2014

Mapbasic Progress Bar Example 1: How to step progress bar for each row in a table

The progress bar that is provided within Mapbasic is not very flexible. It is limited to calling sub routines and is designed in a way that it continues to call a the specified sub routine until you end it (ProgressBar = -1). This means you can't parse a variable and you can't just show the progress bar and update it's progress and any point in your code. 

It took me a while to harness it, and this example and Example 2 demonstrates how I use the Mapbasic progress bar in my mbx's. 

In this example I will demonstrate how to iterate through a table fetching each row. I use global variables to track progress and range and I create a selection table that I step through each row and print out the column 1 value. I would suggest you open a table with more than 1000 rows when you run this example mbx.



Include "Mapbasic.def"

Declare Sub Main
Declare Sub
StepProgress 


Dim iProgress as Integer
Dim iProgressRange as Integer

'----------------------------------------------------------------
Sub Main
   Print Chr$(12)

   Set Window Message Position(1, 1) Width 3 Height 1
   Print "*** Started Progress Bar Example ***"

   If NumTables() < 1 then
      Note "This Progress Bar Example needs a table open to demonstrate"
      Exit Sub
   End If

   Select * from TableInfo(1,TAB_INFO_NAME) Where str$(Col1) <> "" Into Temp
   iProgressRange = TableInfo("Temp",TAB_INFO_NROWS)

   Fetch First From Temp
   iProgress = 1

   ProgressBar "Processing...."
      Calling StepProgress
      Range iProgressRange

   Close Table Temp

   If CommandInfo(CMD_INFO_STATUS) Then
      Note "Progress Bar Example Complete!"
   Else
      Note "You clicked the cancel button on the Progress Bar!"
   End If
End Sub
'----------------------------------------------------------------
Sub StepProgress

   Print "Row " & iProgress & " Value " & Temp.Col1
   'Do Something here
   'EG: Update Temp Set Col1 = "A New Vaule" Where RowID = iProgress

   iProgress = iProgress + 1
   Fetch Rec iProgress From Temp

   If iProgress <= iProgressRange Then
      ProgressBar = iProgress
   Else
      ProgressBar = -1
   End If
End Sub
'----------------------------------------------------------------

No comments:

Post a Comment