Click here to Skip to main content
Click here to Skip to main content

Drop-Down Calculator

, 6 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

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"

' 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.

' 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.

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

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)

About the Author

Raju Kandasamy
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? PinmemberSteveAbbott7-Dec-06 3:26 
AnswerRe: Nice idea. How to close it? PinmemberRaju Kandasamy7-Dec-06 4:11 
GeneralRe: Nice idea. How to close it? PinmemberSteveAbbott7-Dec-06 4:24 
GeneralRe: Nice idea. How to close it? PinmemberThe_Mega_ZZTer7-Dec-06 18:23 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 7 Dec 2006
Article Copyright 2006 by Raju Kandasamy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid