How To Make A Custom Button In VB.NET

we all know how to make custom buttons using images or DLLs but this tutorial is totally different in this tutorial i will be teaching you how to make a custom button without using images or dlls .

so lets get started 

as usual open your visual basic 2008 or  visual basic 2010 and create a new windows application and name it whatever you want and go to the form and add a button and thats it with the GUI and go to code and lets add the custom button function

i am going to use a function , i call it "shimscustombuttonfunction"
 Public Sub shimscustombuttonfunction(ByVal aControl As Control, ByVal Color1 As Color, ByVal Color2 As Color, _
        Optional ByVal mode As System.Drawing.Drawing2D.LinearGradientMode = Drawing2D.LinearGradientMode.Vertical)

        Dim bmp As New Bitmap(aControl.Width, aControl.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim Rect1 As New RectangleF(0, 0, aControl.Width, aControl.Height)

        Dim lineGBrush As New System.Drawing.Drawing2D.LinearGradientBrush(Rect1, Color1, Color2, mode)
        g.FillRectangle(lineGBrush, Rect1)

        aControl.BackgroundImage = bmp
        g.Dispose()
    End Sub
now lets add two events for the button , go to the form and double click button and add this code . i am using green because thats my favourite color and you can just add any color to it simply change the (button1,color.lightgreen,color.green) and your done .

Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
        shimscustombuttonfunction(Button1, Color.LightGreen, Color.Green)
    End Sub

    Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
        Button1.BackgroundImage = Nothing
    End Sub
so finally the custom button will work like this


i have just added two events mouse enter and mouse leave and you can add many more events like mouse click,mouse down,mouse up,mouse hover and mouse move

here is the full source code
'Author : Mohamed Shimran
'Blog : http://ultimateprogrammingtutorials.blogspot.com
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub shimscustombuttonfunction(ByVal aControl As Control, ByVal Color1 As Color, ByVal Color2 As Color, _
        Optional ByVal mode As System.Drawing.Drawing2D.LinearGradientMode = Drawing2D.LinearGradientMode.Vertical)

        Dim bmp As New Bitmap(aControl.Width, aControl.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim Rect1 As New RectangleF(0, 0, aControl.Width, aControl.Height)

        Dim lineGBrush As New System.Drawing.Drawing2D.LinearGradientBrush(Rect1, Color1, Color2, mode)
        g.FillRectangle(lineGBrush, Rect1)

        aControl.BackgroundImage = bmp
        g.Dispose()
    End Sub


    Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
        shimscustombuttonfunction(Button1, Color.LightGreen, Color.Green)
    End Sub

    Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
        Button1.BackgroundImage = Nothing
    End Sub
End Class

happy coding !!!!!

1 comments:

nice one mate :)
keep up the good job.

Reply

Post a Comment

Note: Only a member of this blog may post a comment.