Click here to Skip to main content
15,886,541 members
Articles / Programming Languages / Visual Basic

Drop-Down Calculator

Rate me:
Please Sign up or sign in to vote.
3.48/5 (17 votes)
6 Dec 2006CPOL2 min read 77.2K   3.3K   48  
In-Place Drop-Down calculator control in VB.NET
'___________________________________________________________________________________________
' Module Name: CalculatorButton.vb
' Description: Custom button that behaves like a Drop-Down
' Author     : Raju.K
' Mail       : raju.kandasamy@gmail.com
'____________________________________________________________________________________________

Imports System.Drawing.Drawing2D
Public Class CalculatorButton
    Inherits Button
    ' To hold the control that is to be associated with the Drop-Down Calculator
    Private ownerControl As Control
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        AddHandler MouseEnter, AddressOf OnMouseEnters
        AddHandler MouseLeave, AddressOf OnMouseLeaves
        AddHandler MouseUp, AddressOf OnMouseUps
    End Sub

    ' 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
    Sub OnMouseEnters(ByVal sender As Object, ByVal e As EventArgs)
        Invalidate()
    End Sub
    Sub OnMouseLeaves(ByVal sender As Object, ByVal e As EventArgs)
        Invalidate()
    End Sub
    Sub OnMouseUps(ByVal sender As Object, ByVal e As MouseEventArgs)
        Invalidate()
    End Sub
    Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(pe)
        Dim g As Graphics = pe.Graphics
        Dim r As Rectangle = ClientRectangle
        Dim pen As New Pen(Color.Black)
        'draw the arrow
        Dim p1 As New Point(5, r.Height / 4 - 1)
        Dim p2 As New Point(r.Width - 5, r.Height / 4 - 1)
        Dim p3 As New Point((p1.X + p2.X) / 2, r.Height - 6)
        Dim xpoints(2) As Point
        xpoints(0) = p1
        xpoints(1) = p2
        xpoints(2) = p3
        g.FillPolygon(Brushes.Black, xpoints)

    End Sub

    ' Create and visualize the drop-down calculator
    Private Sub CalculatorButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        If Not Me.ownerControl Is Nothing Then
            Dim CalcForm As CalculatorForm
            CalcForm = New CalculatorForm
            CalcForm.OwnerControl = Me.ownerControl
            CalcForm.Show()
        End If
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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