Gambas/Output

From Wikibooks, open books for an open world
< Gambas
Jump to: navigation, search

Back to Gambas

Simple output[edit]

The interaction between the user and the computer consists of the input and output of data. You wouldn't know what the computer is doing without seeing or hearing anything. That's the importance of output.

Printing[edit]

Note: The word "printing" here means using the Print statement, it's not about using the printer or printing files. Printing is a fairly simply part of gambas, but also essential. Most often printing is used to output information to the user, and can also prove a valuable troubleshooting tool. Whenever printing, you need an object, followed by of course, something to print. Printing may be used with various objects, however, the most common in the terminal window.

Example: You need a command button on your form:

PUBLIC SUB Button1_Click()
    Print "Hello world!!!"      
END

This example prints a message in the terminal window, not on the current form like in VB

Output in Labels, Textlabels, Textboxes oder Textareas[edit]

You can also aim your output to different kind of text controls.

Example: You need a command button and a textbox on your form:

PUBLIC SUB Button1_Click()
    Textbox1.text = "Hello world!!!"      
END

Message boxes[edit]

One of the easiest way of an output is the message box. This is what the code of a normal message box should look like.

 Message.Info("Hallo")

Example:

PUBLIC SUB Button1_Click()
Message.Info("Hallo, this is your message", "OK")
END

Spacing[edit]

There is various way to alter how text is spaced when printing. The most common is the comma. A comma will go to the next print zone. Print zones are 15 characters long. You can think of it like pressing the tab key when typing something out. Remember that print zones are fixed, so if you've typed 1 letter, and then used a comma, then it will be a big space. If you type 13 characters and use a comma, it will not be a large space. For example:

      Private Sub Form_Click()
             Me.Print "Hello", "Next Zone"
      End Sub

Several new things are introduced in this example. The subroutine(which is a function that does not return a value; you'll learn more about them later) Form_Click is called when the user clicks on the current Form(Form1). 'Me' is the same as the current form (Form1). Don't be afraid to experiment. No matter what you do in VB, its always reversible. Now, the comma isn't all that versatile. Another feature is tab. Tab will move so many spaces from the BEGINNING of the line. Followed by tab in parentheses is the amount of characters spaces. For example:

             Form1.Print "Hello"; Tab(10); "Yay"

This will NOT print "yay" 10 spaces after the O of "Hello". Rather it will print 10 spaces from the beginning of the line. You may use as many tabs as you want in the same print command. Although tab is useful, sometimes it is better to spaces things in relation to what has already been printed. This is where the space function comes in. The syntax of space is identical to that of tab. Space will move the next printed text so many spaces over from its CURRENT location. For example:

     Pic.print "Hello"; Space(10); "Yay"

This will print the first Y of "Yay" 10 spaces to the right of the O in "Hello". It is important to note, if you write:

    Pic.Print "Hello"
    Pic.Print "Hello"

They will appear on separate lines as:

    Hello
    Hello

This can be easily dealt with in the need of having separate print statements print on the same line. You merely have to change the code to:

   Pic.Print "Hello";
   Pic.Print "Hello"

This will appear as:

   HelloHello

If you want to make a blank line in between the two hellos, then you may simply have a blank print statement WITHOUT a semicolon. For example:

   Pic.Print "Hello"
   Pic.Print
   Pic.Print "Hello"

This will print as:

   Hello
   
   Hello

It is important to remember that even if the first print has a semicolon at the end, often referred to as a trailing semicolon, the empty print will only reverse it, and print the second Hello on the next line, and no blank line will appear.