Step 1 - The user interfaceThis project will be an ActiveX exe. This allows the program to display the calculator form if it is ran in standalone mode (by double-clicking the icon from the desktop) or to expose its properties and methods if it is ran as a component. The usefulness of this is questionable but but it will do for the purpose of the tutorial.
To keep this simple the calculator will only take its input from the command buttons on the form, and not from the keyboard, so the textbox that accepts input will be locked. If you add a code module to your project and add the following code... Option Explicit Sub main() ' This procedure will be set to be the start up and will run once when the ' component is first used. Therefore we need to ascertain whether we are ' running in standalone mode or not. If we are then we want to display a user ' interface ' We will instantiate the calculator object in the form load event ' of our form if running in standalone mode If App.StartMode = vbSModeStandalone Then ' Display the calculator form frmCalculator.Show End If End Sub Then set the startup object in the General Tab of the Project Properties dialogue box to Sub Main, this procedure will run once only when your object is created. It checks the mode that the component is running in and displays the form if the component is running in standalone mode. The next step is to sort out the user interface so that it displays correctly as each key is pressed. This requires very little code. Create a new project selecting new ActiveX exe from the new project selection screen. Create a form in your project. Call the form frmCalculator. Set the border style to fixed dialogue. Enter the following code into General Declarations section of the form module… Option Explicit Private mobjCalculator As CCalculator ' This variable tracks whether the decimal point has been entered or not Private mflgDecimalPoint As Boolean Option explicit ensures explicit declaration of variables. This means that you can’t create and use a variable without defining it first with the Dim, Private or Public statements. This is good practice. Trust me it can save a lot of confusion once you start to add code to your project. Note Option Explicit will be entered automatically into all your code modules if you set Require Variable Declaration from the Editor Tab of the Tools..Options menu. The following controls need to be added to the form. Inputting the numbers A control array of command buttons with the following properties Name cmdNumber Caption 0 through to 9 with the button captions corresponding to their index number. (So button caption 1 should have index 1. Refer to the Visual Basic documentation for information on how to create control arrays. Using a control array which share a single event procedure makes it easy to output the number entered to the textbox display. Add the following code to the Click event for the control array Private Sub cmdNumber_Click(Index As Integer) If Me.txtEntry.Text = "0." Then 'Me.txtEntry.Text = varOutput & "." Me.txtEntry.Text = Index & "." ElseIf mflgDecimalPoint Then Me.txtEntry.Text = Me.txtEntry.Text & Index Else Me.txtEntry.Text = Mid$(Me.txtEntry.Text, 1, InStr(1, Me.txtEntry.Text, ".") - 1) & Index & "." End If End Sub The first part of the If statement checks whether the text displayed in the textbox is the default (zero and a decimal point). If so then the zero is replaced by the index of whichever command button was pressed. This also has the effect of suppressing leading zeros from the display. The Elseif checks the flag which indicates whether the decimal point button has been pressed. If it has then the number is appended to the end of the textbox’s display. And finally once we reach the Else we know that the text displayed is not the default and the decimal point button hasn’t been pressed so the number is added to the end of the string being displayed (after removing the decimal point temporarily) and the decimal point is then re-added. Cancelling Input A command button for the Cancel feature Name cmdCancel Caption C Add the following code to the Click event for the command button. ' Clear the text box by setting it to an empty string Me.txtEntry = "0." ' Reset the decimal point indicator mflgDecimalPoint = False This simply sets the textbox back to its default display. It also resets the flag which indicates whether the decimal point flag has been set or not. Later we will write code to call the CCalculator object to cancel its operations. The add, subtract, divide and multiply buttons Four command buttons for each of the add, subtract, divide and product functionality. Names cmdDivide, cmdProduct, cmdSubtract and cmdAdd Captions /, *, - and + The Click event for each of these buttons currently contains the following code ' Reset the decimal point indicator mflgDecimalPoint = False ' And the textbox display Me.txtEntry.Text = "0." No action is taken beyond resetting the flag which indicates whether a decimal point has been entered and resetting the display on the text box back to the default. The sign of the number A command button for the sign of the number Name cmdSign Caption +/- Add the following code to the Click event for the command button Private Sub cmdSign_Click() If Left$(Me.txtEntry.Text, 1) = "-" Then ' If the sign is negative then remove it to make it display ' as positive Me.txtEntry.Text = Mid$(Me.txtEntry.Text, 2) Else ' Otherwise the sign is positive so display it as negative Me.txtEntry.Text = "-" & Me.txtEntry.Text End If End Sub This code simply adds a minus sign to the beginning of the number displayed if there isn't one already. Otherwise it removes the minus sign making the number display as positive. A textbox to display the output Name txtEntry Text 0. (Zero and a decimal point) Set the Locked property on the textbox to TRUE since for this example we won’t be adding the functionality to allow the user to enter information from the keyboard. (You could always try adding this feature yourself) This is all we need to create a simple calculator. Further functionality can always be added later. This approach is OK for the purpose of this example but in a real project you would probably want to consider all the functionality in your design before you start coding otherwise you could find yourself up against the proverbial brick wall later when you come to adding additional features. If you run the project you will see that the calculator form allows the inputting of numbers, and behaves as you would expect a calculator to behave except that it currently displays no results. This is fine since our CCalculator object will be providing this functionality and the calculator form will be able to call it with very little extra code. This also has the benefit of allowing other developers access to our calculator object enabling them to use it in different interfaces and applications like Word, Excel or Internet Explorer. There is no error handling in the form module and I should make it clear that this is not good practice. But since I am inherently lazy, I'll leave it to you to add should you feel the need. Production applications should contain error handling as a matter of course.
[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. |