Full Version: Making a User Control
Dream.In.Code > Programming Tutorials > VB.NET Tutorials
AdamSpeight2008
Build a7 Segment Control

Start a new windows form project, call 7SegLed
In the Solution Explorer
Right click -> User Control
Call it Ctrl_7SegLed, the Ctrl being shorthand for Control

View Code of Ctrl_7SegLED
Delete everything inside the class tag
Rename the class to Ctrl_7SegLED
At this stage you should have something looking look this
CODE

Public Class Ctrl_7SegLED
End Class


Add to the start of class the follow protected variables.
CODE

Protected mbgColor As New System.Drawing.SolidBrush(Color.Black) 'Background color
Protected mOnColor As New System.Drawing.SolidBrush(Color.GreenYellow) ' Color of ON segment
Protected mOffColor As New System.Drawing.SolidBrush(Color.Black) 'Colour of OFF segement
Protected mDigit As Long = 8 ' Numerical value of LED (Note: 8 Is used as it uses all the segments)


Now for the properties
CODE

   Public Property LedBackground() As System.Drawing.Color
        Get
            Return mbgColor.Color
        End Get
        Set(ByVal value As System.Drawing.Color)
            mbgColor.Color = value
            Refresh()
        End Set
    End Property
   Public Property OnColor() As System.Drawing.Color
        Get
            Return mOnColor.Color
        End Get
        Set(ByVal value As System.Drawing.Color)
            mOnColor.Color = value
            Refresh()
        End Set
    End Property
   Public Property OffColor() As System.Drawing.Color
        Get
            Return mOffColor.Color
        End Get
        Set(ByVal value As System.Drawing.Color)
            mOffColor.Color = value
            Refresh()
        End Set
    End Property
Public Property Digit() As Long
        Get
            Return mDigit
        End Get
        Set(ByVal value As Long)
            If value < 0 Then Exit Property
            If value > 9 Then Exit Property
            mDigit = value
            Refresh()' Reflect the change as a change to visible UI segements
        End Set
    End Property


Adding the Magic functions

Converting the digit to a code that vand be used to draw the correct LED representation.
CODE

    Private Function To7Seg(ByRef number As Long) As Long
        If number < 0 Then Return 0
        If number > 9 Then Return 0
        Select Case number
            Case 0 : Return 1 + 2 + 4 + 16 + 32 + 64
            Case 1 : Return 4 + 32
            Case 2 : Return 1 + 4 + 8 + 16 + 64
            Case 3 : Return 1 + 4 + 8 + 32 + 64
            Case 4 : Return 2 + 4 + 8 + 32
            Case 5 : Return 1 + 2 + 8 + 32 + 64
            Case 6 : Return 2 + 8 + 16 + 32 + 64
            Case 7 : Return 1 + 4 + 32
            Case 8 : Return 1 + 2 + 4 + 8 + 16 + 32 + 64
            Case 9 : Return 1 + 2 + 4 + 8 + 32
        End Select
    End Function


Understanding the above code section
CODE

' The7 Segment
- A -
|     |
B     C
|     |
- D -
|     |
E     F
|     |
- G -

A to G represent the number 0-6. The LED can represent as single number
LedCode=2^A + 2^B + 2^C + 2^D + 2^E + 2^F + 2^G


If you having problem figuring them out the follow function will assist you, (Not used by this tutorial but you can add it you like)
CODE

    Private Function Get7SegCode(ByRef A As Boolean, ByRef B As Boolean, ByRef C As Boolean, ByRef D As Boolean, ByRef E As Boolean, ByRef F As Boolean, ByRef G As Boolean) As Long
        Dim code As Long = 0
        If A Then code += (1 << 0)
        If B Then code += (1 << 1)
        If C Then code += (1 << 2)
        If D Then code += (1 << 3)
        If E Then code += (1 << 4)
        If F Then code += (1 << 5)
        If G Then code += (1 << 6)
        Return code
    End Function


Draw in the LED segments
CODE

    Private Sub Draw_7Seg(ByRef g As Graphics, ByRef segs As Long)
        Dim posAt As New Point(0, 0)

        g.FillRectangle(mbgColor, posAt.X, posAt.Y, Width, Height)
        Dim hpoly(5) As PointF

        Dim sh As Double = Height / 28
        Dim sw As Double = Width / 16
        ' b0
        hpoly(0).X = posAt.X + 2 * sw : hpoly(0).Y = posAt.Y + 2 * sh
        hpoly(1).X = hpoly(0).X + sw : hpoly(1).Y = hpoly(0).Y - sh
        hpoly(2).X = hpoly(1).X + (10 * sw) : hpoly(2).Y = hpoly(1).Y
        hpoly(3).X = hpoly(2).X + sw : hpoly(3).Y = hpoly(2).Y + sh
        hpoly(4).X = hpoly(2).X : hpoly(4).Y = hpoly(3).Y + sh
        hpoly(5).X = hpoly(0).X + sw : hpoly(5).Y = hpoly(0).Y + sh
        ' Draw the segment with the corrent color
        g.FillPolygon(IIf(segs And (1 << 0), mOnColor, mOffColor), hpoly)
        '   0
        '  / \
        ' 5   1
        ' :   :
        ' 4   2
        '  \ /
        '   3
        ' b1
        hpoly(0).X = posAt.X + 2 * sw : hpoly(0).Y = posAt.Y + 2 * sh
        hpoly(1).X = hpoly(0).X + sw : hpoly(1).Y = hpoly(0).Y + sh
        hpoly(2).X = hpoly(1).X : hpoly(2).Y = hpoly(1).Y + (10 * sh)
        hpoly(3).X = hpoly(2).X - sw : hpoly(3).Y = hpoly(2).Y + sh
        hpoly(4).X = hpoly(3).X - sw : hpoly(4).Y = hpoly(2).Y
        hpoly(5).X = hpoly(4).X : hpoly(5).Y = hpoly(1).Y
        g.FillPolygon(IIf(segs And (1 << 1), mOnColor, mOffColor), hpoly)
        'b2
        hpoly(0).X = posAt.X + 14 * sw : hpoly(0).Y = posAt.Y + 2 * sh
        hpoly(1).X = hpoly(0).X + sw : hpoly(1).Y = hpoly(0).Y + sh
        hpoly(2).X = hpoly(1).X : hpoly(2).Y = hpoly(1).Y + (10 * sh)
        hpoly(3).X = hpoly(2).X - sw : hpoly(3).Y = hpoly(2).Y + sh
        hpoly(4).X = hpoly(3).X - sw : hpoly(4).Y = hpoly(2).Y
        hpoly(5).X = hpoly(4).X : hpoly(5).Y = hpoly(1).Y
        g.FillPolygon(IIf(segs And (1 << 2), mOnColor, mOffColor), hpoly)
        'b3
        hpoly(0).X = posAt.X + 2 * sw : hpoly(0).Y = posAt.Y + 14 * sh
        hpoly(1).X = hpoly(0).X + sw : hpoly(1).Y = hpoly(0).Y - sh
        hpoly(2).X = hpoly(1).X + (10 * sw) : hpoly(2).Y = hpoly(1).Y
        hpoly(3).X = hpoly(2).X + sw : hpoly(3).Y = hpoly(2).Y + sh
        hpoly(4).X = hpoly(2).X : hpoly(4).Y = hpoly(3).Y + sh
        hpoly(5).X = hpoly(0).X + sw : hpoly(5).Y = hpoly(0).Y + sh
        g.FillPolygon(IIf(segs And (1 << 3), mOnColor, mOffColor), hpoly)
        ' b4
        hpoly(0).X = posAt.X + 2 * sw : hpoly(0).Y = posAt.Y + 14 * sh
        hpoly(1).X = hpoly(0).X + sw : hpoly(1).Y = hpoly(0).Y + sh
        hpoly(2).X = hpoly(1).X : hpoly(2).Y = hpoly(1).Y + (10 * sh)
        hpoly(3).X = hpoly(2).X - sw : hpoly(3).Y = hpoly(2).Y + sh
        hpoly(4).X = hpoly(3).X - sw : hpoly(4).Y = hpoly(2).Y
        hpoly(5).X = hpoly(4).X : hpoly(5).Y = hpoly(1).Y
        g.FillPolygon(IIf(segs And (1 << 4), mOnColor, mOffColor), hpoly)
        'b5
        hpoly(0).X = posAt.X + 14 * sw : hpoly(0).Y = posAt.Y + 14 * sh
        hpoly(1).X = hpoly(0).X + sw : hpoly(1).Y = hpoly(0).Y + sh
        hpoly(2).X = hpoly(1).X : hpoly(2).Y = hpoly(1).Y + (10 * sh)
        hpoly(3).X = hpoly(2).X - sw : hpoly(3).Y = hpoly(2).Y + sh
        hpoly(4).X = hpoly(3).X - sw : hpoly(4).Y = hpoly(2).Y
        hpoly(5).X = hpoly(4).X : hpoly(5).Y = hpoly(1).Y
        g.FillPolygon(IIf(segs And (1 << 5), mOnColor, mOffColor), hpoly)
        'b6
        hpoly(0).X = posAt.X + 2 * sw : hpoly(0).Y = posAt.Y + 26 * sh
        hpoly(1).X = hpoly(0).X + sw : hpoly(1).Y = hpoly(0).Y - sh
        hpoly(2).X = hpoly(1).X + (10 * sw) : hpoly(2).Y = hpoly(1).Y
        hpoly(3).X = hpoly(2).X + sw : hpoly(3).Y = hpoly(2).Y + sh
        hpoly(4).X = hpoly(2).X : hpoly(4).Y = hpoly(3).Y + sh
        hpoly(5).X = hpoly(0).X + sw : hpoly(5).Y = hpoly(0).Y + sh
        g.FillPolygon(IIf(segs And (1 << 6), mOnColor, mOffColor), hpoly)

    End Sub

    Private Sub _7SegLed_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Draw_7Seg(e.Graphics, To7Seg(mDigit))
    End Sub


Build -> Build Ctrl_7SegLED

And thats it.

Usage:
Go to Form1, Look at the toolbox and your control should be available to use.
Drag one on to Form1
CODE

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Ctrl_7SegLed.Digit=2
End Sub


The profession bit
Add the follow short peice of code to the begining of each property.
CODE

<System.ComponentModel.Category("Led Appearance")>

This section of code allow the properties to be visible to Designers Property Window.

Tip: If you already have a 7SegLED control placed on a form and you alter the class code of the Ctrl_7SegLED, but those changes are not being reflected in to placed control. Rebuild the project.
jagatworld
Thank you AdamSpeight2008, This is really a master piece !

Regards,
Jagat.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.