Introduction
I've been developing applications for Engineering and CAD/GIS for quite some time. Most of the applications were designed to get inputs from the user for engineering/CAD/GIS calculations. In this process, it has been obsorved that most of the inputs that we recieve from the user is either hand calculated or calculated with the use of standard windows calculator before he/she inputs the value into our application. And there have been requests from the user to give them an in-place calculator so that it can improve their productivity and at the same time can reduce the manual errors. So the result of it is this Drop-Down Calculator control which I'm going to share with you.
Background (optional)
The idea is to provide a drop-down button, which can visualize an in-place calculator when the user hits it. Since we receive inputs from the user from different user controls like textbox, updown, combobox etc. The Drop-Down calculator should be flexible enough to be associated with these standard input controls. Since all the UI Forms controls are inherited from System.Windows.Forms.Control, We can utilize the Text Property provided by the Control to communicate our results from calculator.
Using the code
The Control Design
The Control contains a dialog with calculator like buttons and appropriate code to compute the results based on the user input. This dialog box is displayed when the drop-down button is clicked. The user control to which the results are to be re-directed is set through property named "AssociatedControl"
Public Property AssociatedControl() As Control
Get
Return Me.ownerControl
End Get
Set(ByVal value As Control)
Me.ownerControl = value
End Set
End Property
The next task is to re-position the calculator dialog such that it appears like a Drop-Down from the button. This is achieved by a procedure named "RepositionCalc" in the calculator dialog.
Private Sub RepositionCalc()
Dim pt As Point
Dim scrPt As Point
pt = New Point(fControl.Left + fControl.Width, _
fControl.Top + fControl.Height)
scrPt = New Point(fControl.FindForm().Left + pt.X, _
fControl.FindForm().Top + pt.Y + 30)
CenterToScreen()
Me.Location = scrPt
End Sub
Then transfering the calculated results to the associated control.
Private Sub ProcessResult()
Select Case Me.fActiveOperator
Case OperatorType.Addition
fControl.Text = Val(flastResult) + Val(fControl.Text)
Case OperatorType.Division
If Val(fControl.Text) > 0 Then
fControl.Text = Val(flastResult) / Val(fControl.Text)
Else
MsgBox("Divide By Zero", MsgBoxStyle.OkOnly + _
MsgBoxStyle.Critical, "Error")
End If
Case OperatorType.Modulas
fControl.Text = Val(flastResult) Mod Val(fControl.Text)
Case OperatorType.Multiplication
fControl.Text = Val(flastResult) * Val(fControl.Text)
Case OperatorType.Subtraction
fControl.Text = Val(flastResult) - Val(fControl.Text)
End Select
flastResult = Val(fControl.Text)
fActiveOperator = OperatorType.None
RestartText = True
End Sub
Using the Control
You can add the Drop-Down calculator control to your toolbox by pointing to PopupCalculator.dll in "Choose Toolbox Items" dialog box. Once the Drop-down button is placed on the form, It can be associated with any control (not to self) which already exists in the form. Use the "Properties" window to set the "AssociatedControl" property of the Drop-down button. Now the Drop-Down calculator will take care of in-place calculator functionality ;-)