Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / Visual Basic
Article

Drop-Down Calculator

Rate me:
Please Sign up or sign in to vote.
3.48/5 (17 votes)
6 Dec 2006CPOL2 min read 76.6K   3.3K   48   14
In-Place Drop-Down calculator control in VB.NET

Sample Image - dropdowncalculator.gif

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"

VB
' The property that can be used at design-time
' to set the control associated with the calculator
    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.

VB
' 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)
        CenterToScreen()
        Me.Location = scrPt
    End Sub

Then transfering the calculated results to the associated control.

VB
' 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)
                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 ;-)

Sample Image - maximum width is 600 pixels

License

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


Written By
Web Developer
India India
Working as a Techical Lead in a leading Geographical Information Systems development organisation based in Trichy (India).
Started learning .NET technologies through the various articles hosted in codeproject. And was looking for a day when I can post articles. I hope I did it Smile | :)
Visit my other sites
http://www.text2pdf.co.cc
http://www.pdfmaker.co.cc

Comments and Discussions

 
QuestionNice idea. How to close it? Pin
SteveAbbott7-Dec-06 3:26
SteveAbbott7-Dec-06 3: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 4:11
Raju Kandasamy7-Dec-06 4:11 
GeneralRe: Nice idea. How to close it? Pin
SteveAbbott7-Dec-06 4:24
SteveAbbott7-Dec-06 4:24 
GeneralRe: Nice idea. How to close it? Pin
The_Mega_ZZTer7-Dec-06 18:23
The_Mega_ZZTer7-Dec-06 18: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.