ARTICLE
SpringButton
This tutorial and the source code attached with it shows how to create nice looking buttons using VB.Net.
Download
Files:
Introduction
In this sample is shown how to build a simple button with a nice graphic interface. My control would be an example about using the drawings classes and the essentials key-words. I decide to draw an unusual button like this picture below:

First step
First of all I declare my new class : Public Class SpringButton Inherits Control
How to expose properties After that I expose some essential proprieties like the size in pixel of the triangles and the second color of the button. All the others proprieties that I need are yet behind the System.Windows.Form.Control class.
'this variable say if the
'mouse is over the contol
Private Sel As Boolean = False
Private BackColor2 As Color = Color.Gray
Public Property BackColorEnd() As Color
Get
Return BackColor2
End Get
Set
BackColor2 = value
Me.Invalidate()
End Set
End Property
Private _triangle As Integer = 25
'I add a proprety
'that's the lenght of
'a triangle rectangle (45)
Public Property Triangle() As Integer
Get
Return _triangle
End Get
Set
_triangle = value
'if lenght change I update the control
Me.Invalidate(True)
End Set
End Property
Mouse "lighting"
Another important step is to set the control as "selected" when the mouse is over. So if the mouse is over the control the back color and the border color are inverted. (See the code in the OnPaint override)
'set the button as "selected" on mouse entering
'and as not selected on mouse leaving
Protected Overrides Sub OnMouseEnter(e As EventArgs)
Sel = True
Me.Invalidate()
End Sub 'OnMouseEnter
Protected Overrides Sub OnMouseLeave(e As EventArgs)
Sel = False
Me.Invalidate()
End Sub 'OnMouseLeave
The core
The main step is this override of the OnPaint procedure. In this method I draw indirectly on the control, first the central rectangle, and then the two triangles in the opposite corners. I use this code:
Protected Sub PaintBut(e As PaintEventArgs)
'I select the rights color
'To paint the button...
Dim FColor As Color = Me.BackColorEnd
Dim BColor As Color = Me.BackColor
If Sel = True Then
FColor = Me.BackColor
BColor = Me.BackColorEnd
End If
End Sub 'PaintBut
The delegate
In the end I would to explain how to use delegate. So I declared this class that I used as EventArgs. In fact when clicked on the control I decide if the click has been on a triangle and if yes, I do a delegate with the TriangleEventArgs that say that the triangle has been clicked.
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
MyBase.OnClick(e)
' if the user use this delegate...
If Not (Me.TriangleClick Is Nothing) Then
'check if the user click on the left triangle
'or in the right with some geometrics rules...
'(is't possible to click all triangle at the same time )
Dim x As Integer = e.X
Dim y As Integer = e.Y
If x < _triangle AndAlso y <= _triangle - x OrElse(x > Me.ClientRectangle.Width - _triangle AndAlso y >= Me.ClientRectangle.Height - _triangle - x) Then
'try with right...
Dim te As New TriangleClickEventArgs(False)
'if not...
If x < _triangle AndAlso y <= _triangle - x Then
te = New TriangleClickEventArgs(True)
End If
Me.TriangleClick(Me, te)
End If
End If
End Sub 'OnMouseDown
Credits
You can find more about Spring button and my works at:
If You would see my other work please visit my home page: http://zeppaman.altervista.org
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/).