1.
This file is primarily for reference when converting Visual Basic 6
source code to B4A.
2.
VB6
B4A
3.
===
===
4. controls
Views
(button, edittext, label, etc.)
5. In the VB6 code window, the top left drop-down list contains all
6. the controls you have placed in the current form and the right list
7. contains all the events for each control.
can
The equivalent in B4A
8. be found by clicking on Designer - Tools - Generate Members. Once
9. you have created Subs in the program coding window, the tab
"Modules"
10.
on the right side will list each of the Subs.
11.
In B4A, you start by typing "Sub [viewName]" followed by a
space and
12.
follow the prompts, pressing Enter after each selection until
B4A
13.
ends with "EventName" highlighted. This is where you would
type in
14.
the name of the Sub.
15.
editBox = string
editBox.Text = string
16.
In VB6, you can leave ".Text" off the names of controls when
assigning
17.
text to them, but this is not allowed in B4A.
18.
19.
Dim/ReDim:
20.
---------
21.
Dim Array(n)
Dim Array(n+1)
22.
While "n" is the last index number in VB6, it indicates the
number
23.
of array elements when used in B4A. For example, to Dim an
array
24.
with 0-32 elements in VB6, you would say Dim A(32), while to
convert
25.
this to B4A, you need to change it to Dim A(33), yet index
#33 is
26.
never used (doing so would cause an out-of-range error).
27.
ReDim Preserve
28.
ReDim Array()
Dim it again.
Use a List.
Dim Array(n+1) -- to clear an array, just
29.
[Dim a Int: Dim b as Boolean]
30.
If Not b Then...
If Not(b) Then...
31.
If b Then...
same
32.
If b = True Then
same
33.
If a Then...
If a > 0 Then...
34.
B4A does not treat any non-zero value
as True like VB6.
35.
a = a + b
36.
If b = True Then a = a - 1
Boolean's value cannot be used in a
math function in B4A.
37.
Global Const x=1
function.
B4A does not have a Global Const
38.
In Sub Globals, you can say
Dim x as
Int: x = 1
39.
but x is not a constant (it's value
can be changed).
40.
41.
Loops, If-Then, Select Case:
42.
---------------------------
43.
Do [Until/While]
same
44.
Loop [Until/While]
Loop
45.
For - Next
same
46.
For i... - Next i
The loop variable (i) is not allowed with
Exit Do/For
Exit
[Until/While not allowed.]
Next.
47.
48.
If - Then - Else
B4A; ditto EndIf
49.
50.
--For i = 1 to 6
51.
If i <> 4 Then
52.
...code...
53.
End If
54.
Next
55.
Select Case [expr]
56.
57.
Colors:
58.
------
same, except VB's ElseIf is "Else If" in
Continue [Skips to Next in For-Next loop]
For i = 1 to 6
If i = 4 Then Continue
...code...
...
Next
Select [value]
59.
L1.BackColor =
60.
vbRed
61.
L1.ForeColor =
62.
vbBlack
L1.Color = Colors.Red
L1.TextColor = Colors.Black
63.
64.
Calling a sub:
65.
-------------
66.
SubName x, y
SubName(x, y)
67.
Sub SubName()
Sub SubName() As Int/String/etc. -- a
Global variable cannot be
68.
a parameter, so say that "player" is a
Global variable, you
69.
cannot say: PlayCard(player). Instead
you have to say:
70.
71.
i=player: PlayCard(i)
Function FName()
As [var.type]
Function by adding a
Sub FName() As [var.type]
72.
In B4A, any Sub can be used like a
73.
variable type such as
74.
Sub CheckX(x As Int) As Boolean
75.
...optional code...
76.
If x = [desired value] Then
Return True
77.
78.
...optional code...
End Sub
79.
If no Return is given, then
zero/False/"" is returned.
80.
The calling code does not have to
reference the returned
81.
value, so that while "If CheckX(x) =
True..." is valid,
82.
so is just "CheckX(x)"
83.
Exit Sub
Return
84.
Exit Function
Return [value]
85.
86.
General:
87.
-------
88.
CInt
89.
DoEvents
90.
same, except that Erel says:
"Calling DoEvents in a loop consumes a
lot of resources and
91.
it doesn't allow the system to process all
waiting messages
92.
properly." This was in response to my
pointing out that while
93.
in a Do Loop with DoEvents in it, WebView
could not be loaded
94.
or if loaded, would not process a
hyperlink click. And Agraham
95.
says: "Looping is bad practice on mobile
devices. The CPU will
96.
be constantly executing code and using
battery power as the
97.
code will never get back to the OS idle
loop where the hardware
98.
power saving measures are invoked."
99.
NumberFormat & NumberFormat2 [see
Format()
documentation]
100.
InputBox($)
InputList(Items as List, Title,
CheckedItem as Int) as Int
101.
can list multiple
buttons. Returns index.
Shows list of choices with radio
102.
choices for which
CheckedItem is the default.
103.
List
the user enters a
InputMultiList(Items as List, Title) As
104.
number to choose.
checkboxes.
User can select multiple items via
105.
boxes checked.
Returns list with the indexes of
106.
MsgBox "text"
107.
i=MsgBox()
Negative, Icon) as Int
108.
display for buttons
109.
MsgBox("text", "title")
MsgBox2(Message, Title, Positive, Cancel,
Displays three buttons with text to
(Positive, Cancel, Negative)
110.
is specified like:
Icon is displayed near the title and
111.
"[filename].gif")
LoadBitmap(File.DirAssets,
112.
--for long duration]
ToastMessageShow(text, b) [where b=True
113.
Following is a Sub from Pfriemler which acts like InputBox:
114.
Sub InputBox(Prompt As String, Title As String, Default As
String, Hint As String) As String
115.
Dim Id As InputDialog
116.
Id.Hint = Hint
117.
Id.Input = Default
118.
ret = Id.Show(Prompt, Title, "OK", "","Abbrechen", Null)
119.
If ret = -1 Then Return Id.Input Else Return ""
120.
End Sub
121.
122.
Rnd is < 1
Rnd(min, max) is integer >= min to < max
123.
Rnd(-#)
RndSeed(#) - causes Rnd to generate the
same random number series
124.
125.
for the same # entered.
Randomize
126.
Round(n)
decimal places
Not needed in B4A to randomize Rnd.
same, or Round2(n, x) where x=number of
127.
128.
i = Val(string)
i = 0 --
If IsNumber(string) Then i = string Else
129.
An attempt to use i=string "throws an
exception" if the string is
130.
not numbers, even if the string starts
with numbers. For example,
131.
t = "10:45"
132.
in B4A.
i = Val(t) sets i=10 but causes an error
133.
Instead,
134.
i = t.SubString2(0, 2) = 10
135.
control.SetFocus
136.
n / 0 : error
an exception" for
view.RequestFocus
n / 0 = 2147483647 -- B4A does not "throw
137.
2147483647 no matter
division by 0, but it does return
138.
what the value of "n" is.
139.
x = Shell("...")
replacement, but allows
140.
B4A forum (by Erel):
141.
See "Intent". This is not a complete
code such as the following from the
Dim pi As PhoneIntents
142.
StartActivity
(pi.OpenBrowser("file:///sdcard/yourfile.html"))
143.
t = Timer
t = DateTime.Now ' Ticks are number of
milliseconds since 1-1-70
144.
145.
TabIndex:
146.
--------
147.
In VB6, TabIndex can be set to control the order in which
controls get focus
148.
when Tab is pressed. According to Erel, in B4A:
149.
"Android handles the sequence according to their position.
You can set
150.
EditText.ForceDone = True in all your EditTexts. Then
catch the
151.
EditText_EnterPressed event and explicitly set the focus
to the next
152.
view (with EditText.RequestFocus)."
153.
154.
Setting Label Transparency:
155.
--------------------------
156.
Properties - Back Style
Designer - Drawable - Alpha
157.
158.
Constants:
159.
---------
160.
""
Quote = Chr$(34)
161.
vbCr
CRLF = Chr$(10)
162.
vbCrLf
none
163.
164.
String "Members":
165.
----------------
166.
VB6 uses a character position pointer starting with 1.
167.
B4A uses a character Index pointer starting with 0.
168.
VB6
B4A
169.
Mid$("abcde", 1, 1) = "a" = letter array index 0 -- "a" =
"abcde".CharAt(0)
170.
Mid$("abcde", 2, 1) = "b" = letter array index 1
171.
Mid$("abcde", 3, 1) = "c" = letter array index 2
172.
Mid$("abcde", 4, 1) = "d" = letter array index 3
173.
Mid$("abcde", 5, 1) = "e" = letter array index 4
174.
VB6
B4A
175.
===
===
176.
Mid$(text, n, 1)
text.CharAt(n-1)
177.
Mid$(text, n)
text.SubString(n-1)
178.
Mid$(text, n, x) [x=length wanted]
1) [n+x-1=end position]
text.SubString2(n-1, n+x-
179.
Mid$(text, n, x) = text2
n-2) & _
text = text.SubString2(0,
180.
text2.SubString2(0, x-1) & _
181.
+ z)
text.SubString(n-1
where...
182.
text2.length)
183.
Left$(text, n)
z = Min(x,
[n=num.of chars.]
text.SubString2(0, n)
184.
Right$(text, n)
text.SubString(text.Length - n + 1)
185.
If a$ = b$...
If a.CompareTo(b)...
186.
If Right$(text, n) = text2...
text.EndsWith(text2)...
If
187.
If Left$(text, n) = text2...
text.StartsWith(text2)...
If
188.
If Lcase$(text) = Lcase$(text2)...
text.EqualsIgnoreCase(text2)...
If
189.
Following are some subs from NeoTechni which take the place
of VB6
190.
string functions:
191.
Sub Left(Text As String, Length As Long)As String
192.
If length>text.Length Then length=text.Length
193.
Return text.SubString2(0, length)
194.
End Sub
195.
Sub Right(Text As String, Length As Long) As String
196.
If length>text.Length Then length=text.Length
197.
Return text.SubString(text.Length-length)
198.
End Sub
199.
Sub Mid(Text As String, Start As Int, Length As Int) As
String
200.
Return text.SubString2(start-1,start+length-1)
201.
End Sub
202.
Sub Split(Text As String, Delimiter As String) As String()
203.
204.
Return Regex.Split(delimter,text)
End Sub
205.
206.
x = Len(text)
x = text.Length
207.
text = Replace(text, str, str2)
text.Replace(str, str2)
208.
Lcase(text)
text.ToLowerCase
209.
Ucase(text)
text.ToUpperCase
210.
Trim(text)
text.Trim
211.
212.
(no LTrim or RTrim in B4A)
Instr(text, string)
text.IndexOf(string)
213.
int)
Instr(int, text, string)
text.IndexOf2(string,
214.
found.
Returns -1 if not
215.
not position.
Returns char. index,
216.
"int".
Starts search at
217.
InStrRev(text, str, start, case)
218.
start)
Searches from end of string,
text.LastIndexOf(string)
text.LastIndexOf(string,
219.
optionally from "start".
sensitivity.
220.
case = 0 = case-sensitive
221.
case = 0 = case-insensitive
Cannot specify case
222.
is -
A boolean form of IndexOf
223.
= True Then...
If text.Contains(string)
224.
If Lcase$(x) = Lcase$(y)...
x.EqualsIgnoreCase(y)...
If
225.
text.Insert(n, s)
226.
227.
text = Left$(text, n) & s &
Right$(Text, y)
Asc(s) [where s = a character]
228.
229.
Error Trapping:
230.
--------------
same
231.
VB6:
232.
===
233.
Sub SomeSub
234.
On [Local] Error GoTo ErrorTrap
235.
...some code...
236.
On Error GoTo 0 [optional end to error trapping]
237.
...optional additional code...
238.
Exit Sub [to avoid executing ErrorTrap code]
239.
ErrorTrap:
240.
...optional code for error correction...
241.
Resume [optional: "Resume Next" or "Resume [line label]".
242.
End Sub
243.
B4A:
244.
===
245.
Sub SomeSub
246.
247.
Try
...some code...
248.
Catch [only executes if error above]
249.
Log(LastException) [optional]
250.
...optional code for error correction...
251.
End Try
252.
...optional additional code...
253.
End Sub
254.
WIth B4A, if you get an error caught in the middle of a large
subroutine, you can
255.
NOT make a correction and resume within the code you were
executing. Only the code
256.
in "Catch" gets executed, plus any code following "End Try".
257.
Try-Catch in place of GoTo:
258.
--------------------------
259.
Try-Catch can be used as a substitute for GoTo [line label]
for forward, but not
260.
backward, jumps. It cannot be used to replace GoSub, for
which B4A has no equivalent.
261.
Start the code with "Try" and replace the [line label] with
"Catch".
262.
Replace "GoTo [line label]" with code which will create an
exception, which causes
263.
a jump to "Catch", such as OpenInput("bad path", "bad
filename").
264.
265.
"Immediate Window" vs "Logs" Tab
266.
--------------------------------
267.
Comments, variable values, etc., can be displayed in VB6's
Immediate
268.
Window by entering into the code "Debug.Print ...".
269.
In the B4A environment, the Logs tab on the right side of the
IDE is a
270.
way to show the values of variables, etc., while the code is
running.
271.
Both VB6 and (now) B4A allow single-stepping through the code
while it
272.
is running and viewing the values of variables. VB6 also
allows changing
273.
the value of variables, changing the code, jumping to other
lines from
274.
the current line, etc. Because B4A runs on a PC while the app
runs on
275.
a separate device, B4A is currently unable to duplicate all
of these
276.
VB6 debug features.
http://leandroascierto.com/foro/index.php?topic=1350.0