Step 3 - Putting It TogetherFirst make sure that the Start Mode in the Project Properties/Component dialogue is set back to Standalone. This allows the calculator form to be displayed on startup. Private Sub cmdAdd_Click() ' Reset the decimal point indicator mflgDecimalPoint = False ' Call the Number method of the CCalculator class passing in the last entered number If Not mflgEqualsPressed Then If Right$(Me.txtEntry.Text, 1) = "." Then mobjCalculator.Number = CDbl(Mid$(Me.txtEntry.Text, 1, Len(Me.txtEntry.Text) - 1)) Else mobjCalculator.Number = CDbl(Me.txtEntry.Text) End If End If ' Call the add method mobjCalculator.Add ' Reset the textbox display and equals flag Me.txtEntry.Text = "0." mflgEqualsPressed = False Me.cmdSign.Enabled = True End Sub Each of the Add, Subtract, Product and Divide command buttons have the above code added. Each time one of these buttons is pressed the value in the textbox is placed into the Number property of the CCalculator object and then the corresponding method is called (in the above example Add is called). An additional flag was created mflgEqualsPressed to prevent the result returned from the CCalculator object being returned back to the object.Private Sub cmdCancel_Click() ' Clear the text box by setting it to an empty string Me.txtEntry = "0." ' Reset the decimal point indicator and equals flag mflgDecimalPoint = False mflgEqualsPressed = False mobjCalculator.Cancel Me.cmdSign.Enabled = True End Sub The above code was added to the Cancel command button to call the Cancel method of the CCalculator object and to enable the Sign command button. Private Sub cmdEquals_Click() If Right$(Me.txtEntry.Text, 1) = "." Then mobjCalculator.Number = CDbl(Mid$(Me.txtEntry.Text, 1, Len(Me.txtEntry.Text) - 1)) Else mobjCalculator.Number = CDbl(Me.txtEntry.Text) End If If InStr(Str$(mobjCalculator.Result), ".") Then Me.txtEntry.Text = Str$(mobjCalculator.Result) Else Me.txtEntry.Text = Str$(mobjCalculator.Result) & "." End If mflgEqualsPressed = True Me.cmdSign.Enabled = False End Sub A click event was added to the Equals command button to allow the interim result to be displayed to the textbox and to disable the Sign command button. (We are not allowing the interim result to be returned to the object, so pressing sign when the result is displayed will have no effect except that of confusing the user.) I also added a CancelPressed event to the class by adding the line Event CancelPressed() to the General Declarations section of the class module and then calling the event from the Cancel method of the CCalculator class RaiseEvent CancelPressed Change the declaration of the object reference to the CCalculator class in the Calculator form to … Private WithEvents mobjCalculator As CCalculator will allow you to use the event. In the object drop down combobox in the code module you will find an additional entry for mobjCalculator with one event. You guessed it, the CancelPressed event. Adding the code MsgBox "Cancel Pressed!" to this event will display the message box each time you press Cancel on the calculator form. Pretty useless, but it shows how events can be generated from your classes. If an object exposes events you are not required to implement code for them but they can be very useful. In order to gain access to the events though you must declare the object using the WithEvents keyword or any events generated will not be accessible to your project. And that is pretty well it. As you can see there was not a lot of code required to provide the functionality of the CCalculator object and the object can now be re-used and enhanced. As long as the existing properties and methods are retained, you can add new ones or change the implementation of the old ones and applications using the object will continue to work. Once you have compiled your ActiceX project it is worth setting Version Compatibility in the Component tab of the Project properties dialogue box to Binary Compatibility and setting the filename to the compiled exe file. This will ensure that you are warned if you break the project by changing the external interface (changing the name or parameters of existing properties and methods). You should also call up any objects you create in the Object Browser (F2 from the IDE) and right click on each property and method to display the properties box for the object and add a description for each property and method to aid anyone using the object. I also like to add that there are many ways of achieving the same end in code and I am in no way suggesting that this method of implementing a calculator is the best or even the easiest way. It should be used purely as an example of what can be achieved using ActiveX to promote reusability of object code within your use of VB. I've enjoyed myself creating this project. I hope you enjoy tearing it apart. If you find any serious omissions or errors let me know and I'll hang my head in shame… Mark Kirkland
[Main Tutorial Page][ActiveX Calc Tutorial TOC] |
Quick searches: Site Search | Advanced Site Search |
|
By using this site you agree to its terms and conditions VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc. |