Working with Events and Properties
Using "built-in" events
Jabaco compiles VB6-jabaco-modified source code, hence making Jabaco capable of handling event listening as well.
As reserved keywords such as Dim, Public, Erase etc perform preset actions, so do reserved SUBs: Public Sub Form_Load() for example.
The event of Load when the form is loaded will fire the procedure Public Sub Form_Load() if exists in code. This event will fire only once when the form is loaded, but different events might fire more frequently, depending on the condition(s) for those to be executed.
How to unfold an object's events
Upon opening the source code of a form or class you may wish to let Jabaco add the requested event procedure with all of its parameters. Switching entries on the two combo boxes above the source code window either sets focus onto an existing event procedure or appends the required procedure and sets focus onto it. Many events can be intuitively understood: Form_Click(), Form_Move() and so on. The western combo box specifies what control on the form to analyze and select events from.
How to create an object and assign custom events to it
A Class module is an object created similarly like a "variable" (DIMed as New, for that matter). In order for us to have an object which returns an entry, fire an event when a condition is fulfilled and 'remember' an entry we would have just changed, we shall examine an example.
- We would begin by adding a new class module to our new project by clicking Add File... in the Project section on the right, choosing Class Module from the popup menu and switching to its source code window by double clicking on Class1.
- This note applies to coding in visual basic and jabaco in general, in the first line of every form, module or class, "Option Explicit" should be inserted for more "correct" and clean programming. Option Explicit forces declaring of new variables whereas in default variables are created on-the-fly, and in many cases it may be more difficult to debug an application having many informally declared variables: variables should be created formally. However, in some cases omitting Option Explicit can be convenient.
- We will build a class which remembers text, and fires commands of your choosing when a new text is assigned to it (meaning that the text will have changed). Included below all explanations is the example project so many copy-pastes aren't needed.
- We declare a variable of the type String to store the text sent:By declaring the strText as String variable Private, we limit direct access to the variable to within the class only. Internal variable should be changed by "formal" procedures we write. This declaration must be placed in the General Declarations portion of the source code - above all function and procedures and under Option Explicit.
Private strText As String
- Like a Form has different properties of specific types (Caption-string, Enabled-boolean), every property must be properly declared:
Public Property Get Text() As String Text = strText End Property Public Property Let Text(NewValue As String) strText = NewValue DoRaiseChange NewValue End Property
- Here Public does the opposite of Private, but with an exception from normal global variables: an object's public variables, methods (procedure or function) and properties belong to the object only, and to relate to them from code, the name of the new declared object followed by a dot must precede the desired public variable or method: as with MyForm.Caption = "Hello World!", we will change our object's property in the same manner: MyObject.Text = "Hello World!".
- Public Property Get Text() As String will return the requested property value we program to return like a function. Get refers to the requesting code: MsgBox MyObject.Text will show what we return here, and so we return our private strText string.
- Public Property Let Text(NewValue as String) is fired when we "remotely" change the Text property. Actually the first parameter in this case, its name is not important, but the name Value should usually be used to avoid confusion.
- DoRaiseChange is a procedure which will fire the Event of the text being changed. You may replace the DoRaiseChange call with the contents of the function, but this way keeps the code more clean and separate to subroutines\functions.
Private Sub DoRaiseChange(NewText As String) RaiseEvent TextChanged(NewText) End Sub
- The Keyword RaiseEvent fires or in Jabaco\VB terms Raises an event. We must declare the Public event.
Public Event TextChanged(ByVal Text As String)
- This Event we've just called TextChanged will be fired if its procedure exists where the object was created, such as in the code of a form.
- If a non global object is created within a procedure or function, it will be lost when the function terminates.
How to create a new object with events
- Switch to the form source code, and under Option Explicit insert:
Dim WithEvents TextObject As New Class1
- Please Avoid creating new objects with the same names of existing ones, such as Form1, Class1, or if a new object was given the name of abc do not create new objects sharing the same name. Jabaco is not case sensitive.
- According to the second title How to unfold an object's events do something when the text changes.
Private Sub TextObject_TextChanged(Text As String) txtStatus.AppendText Now & " set " & Text & VbNewLine End Sub
Create a TextBox with the name of txtStatus and clear it from Text. Every time the Text Property of the Object changes, the new text will be appended to the text box.
- The class module contains events of its own as well, one important event is Initialize. Insert this to your class code:
Public Sub Class_Initialize() strText = "" End Sub
- An important remark: New variables, as strText declared in Class1, must be initialized with a first value. A string value should be empty "", and a numeric should be 0 in sum operations or 1 in product operations.
Example Project Image:Event Jabaco Example.zip
Working with Variables
Working with Constants
Working with Formatting Functions
Working with String Manipulation Functions
Working with Arrays
A variable is a placeholder for a specific type of value. An array is a collection of variables.
Dim|Public|Private ArrayName(max) As DataType
Dim|Public|Private ArrayName(min To max) As DataType
Public Sub Form_Load() Dim number(3) As String number(0) = "zero" number(1) = "one" number(2) = "two" number(3) = "three" End Sub
Although you usually set the number of elements in an array when you declare it, it's possible to alter the size of the array. When you change the number of elements in an existing array, you redimension it. To do so, use the ReDim keyword in the following syntax:
ReDim [Preserve] ArrayName(Subscript) As DataType
Public Sub Form_Load() Dim number(3) As String number(0) = "zero" number(1) = "one" number(2) = "two" number(3) = "three" ReDim Preserve number(5) number(4) = "two" number(5) = "three" End Sub
Working with Files
Handling files in Jabaco slightly differs from VB6 - it is replaced with an object.
Declaring a File object and assigning a file to it
The fourth parameter controls should the file be locked to reading\writing\both\none=shared, by external applications.
Dim File As VBFileHandler = Open(App.Path & "\text.txt",Random,ReadAndWrite,Shared)
All functions and more are bundled within the object instead of separate functions.
MsgBox File.Length While Not File.EOF Text1.AppendText File.ReadLine & vbcrlf Wend File.writeString "Hello World." File.Close
Working with Forms
Showing a Form
Every form is represented as a class, which must be assigned to a new variable, for a new form to be shown for instance.
Example: Start a new project, add a command button and insert this code into the Form's code section.
Private Sub Command1_Click() Dim frm as New Form1 frm.Caption=Now() frm.Show End Sub
It is recommended to name new variables differently than the class names to avoid confusions.
Public Sub Main(ByJava args() As String)
Please notice the automatically generated Module, in which Form1 is set to be the primary default form: when closed, the rest of the forms will be unloaded as well. Also notice the initial creation of Form1 using the New Keyword.
Public Form1 As New Form1 Public Sub Main(ByJava args() As String) 'Jabaco BETA 1.5.2 Dim myArgs() As String myArgs = args Form1.SetDefaultClose() Form1.Show() End Sub
Closing a form
Unloading a form is done as with VB6: Unload(Form as Form), alternatively you may use Form.Close().