Getting Started
Introduction
Sample Programs
IDEs
History
Advice
Mini-Tutorial
Tutorials
Code Snippets

Resources
Web Sites
More Tutorials
Forums
Vendors/Tools
Books
Magazines
Newsletters
NewsGroups
User Groups
Talk Shows
Blogs

Controls
Overview
Button
Check3State
Checkbox
ComboBox
Frame
Graphic
Image
ImageX
ImgButton
ImgButtonX
Label
Line
ListBox
ListView
Option
Progress Bar
Scrollbar
StatusBar
TAB
TextBox
Toolbar
TreeView

GBIC >> PowerBASIC >> Tutorials >> Templates

PowerBASIC Information Center Tutorials
These tutorials were written to help you get a quick, but thorough, understanding of PowerBASIC - the scope of the language as well as it's specific capabilities.

Introduction       Projects       Language           Messages       Functions           Advanced
  • Overview
  • Examples
  • IDE
  • Compilation
  • Distribution
  • Project Files
  • DDT Windows
  • Controls
  • Menus
  • Dialogs  
  • Help Files
  • Resources  
  • Templates  
  • Project Shell  
  • Syntax
  • Operators
  • Data Types
  • Variables
  • Scope
  • Declarations  
  • Procedures
  • Flow Control
  • Windows
  • Messages
  • Callbacks
  • Mouse
  • Keyboard
  • Dialogs
  • Controls
  • Subclassing
  • Arithmetic
  • Trig  
  • Strings
  • Arrays
  • Date/Time
  • Printing
  • Files
  • Folders
  • Keyboard
  • Mouse
  • Sound
  • System
  • Error Traps
  • Debugging
  • Objects
  • Graphics
  • Databases
  • API
  • DLLs
  • ASM
  • Threads
  • Templates
    Rewriting code is a waste of time, and that's where PowerBASIC templates come in. Code put in a *.pbtpl file and placed in the PB\bin directory will be listed in the New File As ... IDE menu - ready for opening without having to retype anything!

    Especially with new PowerBASIC programmers, typing in test code is a very common activity. I've found that there are a few code structures (the dialogs, controls, and callback functions) that get used over and over as part of testing new code. Saving those structures as templates can really speed up trying out new code!

    Template Format
    Any code you have can be a template, just put 3 lines at the top and save it into the PB\bin folder with a .pbtpl extension. Here's the 3 lines of code to use at the top of your template file.

        1
        .bas
        Any Title - This is displayed in the PB IDE
    

    That's it! Put these 3 lines at the top of your code and save the file in the PB\bin folder with a .pbtpl extension. The next time you open the IDE, the new templates will be there, ready for instant use!

    Tips For Using Templates
    Here are four tips for using templates:

    1. Sorting Templates
      Templates are displayed in the IDE sorted by the template file name, not by the title you give the template! I like mine in a certain order, so I renamed the files accordingly.

    2. Compiling a Template #1
      When you load a template, you can't just press Compile to see what happens. You have to make a change to the loaded code (even as simple as pressing a space bar) before the IDE will compile the template code.

    3. Compiling a Template #2
      The IDE also requires that you save the code to a file before it will compile. I created a file called "_.bas". With that name it will always be at the top of the Save As dialog and is ready-at-hand to select as the file name to be used for saving the modified template code. I treat it like a temp file, never putting anything of value in it and always overwriting it whenever I test new code snippets.

    4. Starting Position for Editing
      The pipe symbol | is used in a template to tell the IDE where the cursor is to be placed when the template is loaded. However, the IDE doesn't seem to be willing to place a cursor at all depths. So stick with 1-3 indentations and you should be fine.

    Sample Templates
    Here are the three templates I have in my PB IDE. I'm sure as I get more experience with PowerBASIC, that I'll find myself using templates with more and more code.

    Example 1: Simple Model Dialog
    Here's a very simple template for loading a modal dialog and its callback procedure. That are no children and no other code. I use this when I want to write test code starting from scratch, but would like a dialog already in the code. download

        1
        .bas
        .Code Test - Modal Dialog, Empty
        #Compile Exe
        #Dim All
        #Include "win32api.inc"
        Global hDlg As Dword
        Function PBMain () As Long
           Dialog New Pixels, 0, "Test",300,300,200,200, %WS_OverlappedWindow To hDlg
           '... test code goes here
           Dialog Show Modal hdlg Call DlgProc
        End Function
    
        CallBack Function DlgProc() As Long
            '... test code goes here
        End Function
    

    Example 2: Code Tester - Use Button Press
    Typically, I want to test code by pressing a button. This template loads a dialog and a button, along with the callback procedure where I can put the code that will execute when the button is pressed.

    Also, it creates two label controls and shows the code for inserting two results into labels. download

        1
        .bas
        .Code Test - Modal Dialog, Press Button
        #Compile Exe
        #Dim All
        #Include "win32api.inc"
        Global hDlg As Dword
        Function PBMain() As Long
            Dialog New Pixels, 0, "Button Test",300,300,200,200, %WS_OverlappedWindow To hDlg
            Control Add Button, hDlg, 100,"Push", 50,10,100,20
            Control Add Label, hDlg, 101,"results", 50,40,100,20, %WS_Border
            Control Add Label, hDlg, 102,"results", 50,70,100,20, %WS_Border
            Dialog Show Modal hDlg Call DlgProc
        End Function
    
        CallBack Function DlgProc() As Long
           Select Case Cb.Msg
              Case %WM_Command
                 Select Case Cb.Ctl
                    Case 100
                       If Cb.CtlMsg = %BN_Clicked Then
                          Local iResult1&, iResult2&
                          '... test code here - place results in labels 100 & 101
                          Control Set Text hDlg, 101, Str$(iResult1&)
                          Control Set Text hDlg, 102, Str$(iResult2&)
                       End If
                    End Select
            End Select
        End Function
    

    Example 3: Code Tester - Use Doevents
    Sometimes I want code to run repeatedly, without have to continually press a button. This template opens a modeless dialog and uses a Doevents Loop to run test code over and over.

    Also, it creates two label controls and shows the code for inserting two results into labels. download

        1
        .bas
        .Code Test - Modeless Dialog, Doevents Loop
        #Compile Exe
        #Dim All
        #Include "win32api.inc"
        Global hDlg As Dword
        Function PBMain() As Long
           Local Count& 
           Dialog New Pixels, 0, "Test",300,300,200,200, %WS_OverlappedWindow To hDlg
           Control Add Label, hDlg, 100,"results", 50,50,100,20, %WS_Border
           Control Add Label, hDlg, 101,"results", 50,80,100,20, %WS_Border
           Dialog Show Modeless hDlg
           Do
              Dialog DoEvents 0 To Count&
              MySub
           Loop While Count&
        End Function
    
        Sub MySub
           Local iResult1&, iResult2&
           '... test code here - place results in labels 100 & 101
           Control Set Text hDlg, 100, Str$(iResult1&)
           Control Set Text hDlg, 101, Str$(iResult2&)
        End Sub 
    

    If you have any suggestions or corrections, please let me know.