Click here to Skip to main content
13,765,472 members
Click here to Skip to main content
Add your own
alternative version


47 bookmarked
Posted 7 Dec 2006
Licenced CPOL

Drop-Down Calculator

, 7 Dec 2006
Rate this:
Please Sign up or sign in to vote.
In-Place Drop-Down calculator control in VB.NET

Sample Image - dropdowncalculator.gif


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"

' The property that can be used at design-time
' to set the control associated with the calculator
    Public Property AssociatedControl() As Control
            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.

' Procedure to reposition the drop-down calculator
' with respect to the associated control
    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)
        Me.Location = scrPt
    End Sub

Then transfering the calculated results to the associated control.

' Procedure to compute the result based on the active operator
' and transfer the result 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)
                    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 ;-)

Sample Image - maximum width is 600 pixels


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Raju Kandasamy
Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions framework 1.1??? Pin
gchar15-Oct-07 23:50
membergchar15-Oct-07 23:50 
GeneralPercent button doesn't work well Pin
sohst17-Aug-07 9:09
membersohst17-Aug-07 9:09 
QuestionUsing the PopupCalendar in my application Pin
philnad12-Feb-07 10:29
memberphilnad12-Feb-07 10:29 
QuestionButton image? Pin
charliewhit9-Feb-07 4:22
membercharliewhit9-Feb-07 4:22 
AnswerRe: Button image? Pin
Raju Kandasamy9-Feb-07 17:47
memberRaju Kandasamy9-Feb-07 17:47 
GeneralRe: Button image? Pin
philnad19-Feb-07 10:47
memberphilnad19-Feb-07 10:47 
GeneralReally nice Pin
kbsbng7-Feb-07 19:44
memberkbsbng7-Feb-07 19:44 
GeneralVery good! Pin
philnad18-Jan-07 7:51
memberphilnad18-Jan-07 7:51 
GeneralGood work Pin
sachubangera13-Dec-06 22:17
membersachubangera13-Dec-06 22:17 
GeneralVery Good Idea Pin
Paddy Wack8-Dec-06 20:54
memberPaddy Wack8-Dec-06 20:54 
QuestionNice idea. How to close it? Pin
SteveAbbott7-Dec-06 4:26
memberSteveAbbott7-Dec-06 4:26 
That's a neat control.

But I can't find a way to close the calculator when I've done the calculation. I assumed there was some clever idea that I was missing, but hunting through your nice code I can't see anything. But I'm sure you don't leave your calculators open all the time, so I must be missing something.

I also can't think of any clever way of avoiding opening multiple versions attached to the same control, nor of moving the pop-up if you move the form (such as your demo program) from which the pop-up came, so that the pop-up stays in the right position.

I'm sure you've thought of all these issues, so I'll await your reply before trying to come up with my own solutions.

Many thanks

Steve Abbott
AnswerRe: Nice idea. How to close it? Pin
Raju Kandasamy7-Dec-06 5:11
memberRaju Kandasamy7-Dec-06 5:11 
GeneralRe: Nice idea. How to close it? Pin
SteveAbbott7-Dec-06 5:24
memberSteveAbbott7-Dec-06 5:24 
GeneralRe: Nice idea. How to close it? Pin
The_Mega_ZZTer7-Dec-06 19:23
memberThe_Mega_ZZTer7-Dec-06 19:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181114.1 | Last Updated 7 Dec 2006
Article Copyright 2006 by Raju Kandasamy
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid