Page 1
Page 2
To makes things a little easier, l had added a table
listing below showing the various properties that we need to have assigned
to all of our command buttons (keypad buttons).
|
Key Pad Button
|
Button Name
|
BackColor
|
Caption
|
Style
|
Tag
|
|
1
|
cmdOne
|
&H0000FF00&
|
1
|
vbButtonGraphical
|
|
|
2
|
cmdTwo
|
|
2
|
|
|
|
3
|
cmdThree
|
|
3
|
|
|
|
4
|
cmdFour
|
|
4
|
|
|
|
5
|
cmdFive
|
|
5
|
|
|
|
6
|
cmdSix
|
|
6
|
|
|
|
7
|
cmdSeven
|
|
7
|
|
|
|
8
|
cmdEight
|
|
8
|
|
|
|
9
|
cmdNine
|
|
9
|
|
|
|
0
|
cmdZero
|
|
0
|
|
|
|
.
|
cmdPeriod
|
|
.
|
|
.
|
|
Clear
|
cmdClear
|
|
C
|
|
C
|
|
Swap Country
|
cmdSwap
|
|
Swap Country
|
|
|
Programming
Lets begin the programming part of our Currency Converter
Part II application (Creating a numeric keypad). To access the code,
either double click on the form in the visual form editor or select the
form in the project explorer and click on the View Code icon. We will
start by typing in all of the code to which l will then explain how each
line of the code operates.
Add the following lines of code into our existing
eVB Currency Converter application.
Private Function ButtonPress()
Dim cmdButton As Control
Set cmdButton = Screen.ActiveControl
Select Case cmdButton.Caption
Case "C"
txtFrom = ""
cmdPeriod.Tag = "."
Case "."
txtFrom = txtFrom & cmdButton.Tag
cmdPeriod.Tag = ""
Case Else
txtFrom = txtFrom & cmdButton.Caption
End Select
End Function
Private Sub cmdSwapCountry_Click()
Dim x As Integer
x = cboFrom.ListIndex
cboFrom.ListIndex = cboTo.ListIndex
cboTo.ListIndex = x
MyConversion
End Sub
Assign our function, ButtonPress to each
of our command buttons except for the Swap Country command button.
Please see below for a brief example.
Private Sub cmdOne_Click()
ButtonPress 'Run our "ButtonPress" function
End Sub
Private Sub cmdTwo_Click()
ButtonPress 'Run our "ButtonPress" function
End Sub
Private Sub cmdThree_Click()
ButtonPress 'Run our "ButtonPress" function
End Sub
How the code works
We start with our re-useable function ButtonPress.
The reason for using a function is to save time instead of typing same
lines of code repeatedly throughout the program. We will use a Private
Function that will be called repetitively within the program. Something
to keep in mind, a function is like a program within a program they make
coding easier, faster and more stable.
Private Function ButtonPress()
Dim cmdButton As Control
Set cmdButton = Screen.ActiveControl
After we dimensioned our variable as a control, our
next line makes the cmdButton variable a control name that was grabbed
using the Screen.ActiveControl statement. The Screen.ActiveControl simply
grabs the control name and it properties that currently has focus (in
our case, the keypad button that we have just pressed).
The lines of code listed below will make use of our
cmdButton control. This section of code will do something based on what
the current control (that has focus) making use of its caption/tag properties.
To do this type of conditioning, we will make use of the Select Case
statement making use of the Caption & Tag properties of the keypad
button.
Select Case cmdButton.Caption
Case "C"
txtFrom = ""
cmdPeriod.Tag = "."
Case "."
txtFrom = txtFrom & cmdButton.Tag
cmdPeriod.Tag = ""
Case Else
txtFrom = txtFrom & cmdButton.Caption
End Select
Case "C"
If the cmdButton.Caption contains a C, the
txtFrom text box is cleared and the next line assigns a .
to the tag property of cmdPeriod button, l will explain why we do this
a little later on.
Case "."
If the cmdButton.Caption contains a . ,
the txtFrom text box will contain a decimal point next to the existing
value that is currently assigned to txtFrom text box. The next line simply
clears the cmdPeriod.Tag property (this ensures that decimal point will
not appear more than once in the txtFrom text box).
Case Else
If none of the above conditions were detected, the
txtFrom text box will display the current controls caption property next
to the existing value of the txtFrom text box.
We have now finished putting in all of our conditions
into the Select Case statement. To finish off we use the End Select statement,
this also concludes our Function to which we use the End Function statement.
End Select
End Function
How the
code works (cont:)
To complete the remaining part of our application,
we use the following lines of code under cmdSwapCountry_Click event.
I have added this keypad button to simply swap the countries around in
our two combo boxes. This feature is very handy !! I wont go into explaining
why, however just try it out and you will see what l mean.
Dim x As Integer
x = cboFrom.ListIndex
cboFrom.ListIndex = cboTo.ListIndex
cboTo.ListIndex = x
MyConversion
After we have dimensioned x as an integer,
our second line simply assigns the current index number of the country
displayed in our combo box to our variable x.
The third line assigns the current index value from
the cboTo combo box to our cboFrom combo box. As soon as the cboFrom combo
receives an index number, the cboFrom will display the country name of
that index value.
For example
- Index = 0 combo box will display US Dollar
- Index = 1 combo box will display Australian Dollar
- Index = 2 combo box will display Austrian Shilling
- Index = 3 combo box will display Belgian Francs
The fourth line grabs the value that was originally
assigned to our variable x and assigns it to cbTos list index
property value of the combo box in return will display the country based
on the index value (please see above for an example).
The last line re-runs our Private Function My Conversion.
Please see my first tutorial
for a clear explanation on how this functions operates.
Conclusion
I hope this tutorial has helped to give you a better
understanding on some of the basic commonly used statements within eVB.
A couple of tricks that l have implemented into
this application when designing the code was making use of the Screen.ActiveControl.
Normally in Visual Basic, you would use what is called a Control Array.
Since eVB doesnt support this l had to find an alternative, the Screen.ActiveControl
was a close match. The other trick was ensuring that the decimal point
does not display more than once, Ive seen a lot of complicated ways of
doing this but found my code do handle this beautifully.

Previous Page