Saturday, December 8, 2012

enhance button vb

Give a new aspect to your entire button in your program simply by creating a derived class. It might be difficult to understand but I guarantee is very simple to put in practice.

Here is the classic button from the Framework 4 .NET

Nothing special, you use it, you change some initial settings. You put some events: left click, right click, …. The world seems great.

OK, now what happens if you want all your buttons to open a Process in Windows 7 or in Windows 8? What if you want all your buttons to change color to blue when the mouse is over? The problem is that you have to change each buttons to do exactly the same thing and do many copy a paste. This is mostly inefficient and heavy. Instead of using the basic Button from System.Windows.Forms, we will alter that button and reuse it.

First you have to create a new class anywhere in your problem but not in a other class.

Here is a sample:

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load

    End Sub

End Class

Public Class ButtonColor
    Inherits Button
End Class

Build your project and automatically, your new button will be available. In this project I named my button: ButtonColor.

It is important to put the line “Inherits Button” immediately after your first line of your class.

Now, if I put a simple member, a constructor methods a few events, here is what I have:

''' <summary>
''' Sample of Inherits Button / Derived Class Button
''' </summary>
''' <remarks>this is a sample</remarks>
Public Class ButtonColor
    Inherits Button

#Region "Members"
    Public mPathExe As String
    Public mArguments() As String

#End Region

#Region "constructor"
    Public Sub New()
        mPathExe = "Explorer.exe" 'default value
        ReDim mArguments(0)
    End Sub
    Public Sub New(sPathExe As String)
        mPathExe = sPathExe
        ReDim mArguments(0)
    End Sub

#End Region

#Region "Actions Events"
    Private Sub ButtonColor_Click(sender As Object, e As System.EventArgsHandles Me.Click

        Dim index1 As Integer
        Dim sArguments As String
        sArguments = ""

        For index1 = 0 To mArguments.Length - 1 Step 1
            sArguments = " " & mArguments(index1)
        If sArguments.Length > 0 Then
            Process.Start(mPathExe, sArguments)
        End If

    End Sub
#End Region

#Region "Color Events"
    Private Sub ButtonColor_MouseHover(sender As Object, e As System.EventArgsHandles Me.MouseHover
        Me.BackColor = Color.BlueViolet
    End Sub

    Private Sub ButtonColor_MouseLeave(sender As Object, e As System.EventArgsHandles Me.MouseLeave
        Me.BackColor = Color.LightGray
        Me.UseVisualStyleBackColor = True
    End Sub

#End Region
End Class

If you take a look to the code, if the person click on a button it calls the process.Start methods to run a fill or open something. That is if mPathExe as a valid path. The click event could also take arguments.
There are also 2 events exclusively intend for the color of the button. When the cursor is over the button, it will turn BlueViolet. When the mouse leaves the buttons, the button recovers his state.

This is a sample, so you could put more stuff in it such a error handling or more functions.  Whatever pleases you.

Use your Enhanced button

To use your derived class, is a good idea to remove the keywords WithEvents from your form. See image.

You won’t need the form (in this case Form1) to handle your button event since the button could handle himself. You may save a tiny little bit of execution time and help you concentrate your basic code on your button inside the button.

Click on the enhanced button: ButtonColor

Ok, I need to personalize the command in each button so when I click on it, it will call for his own instruction. Here is a sample, very simple to show you how it works.

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load

        ButtonColor1.Text = Environment.UserName.ToString
        ButtonColor1.mPathExe = "C:\Users\" & Environment.UserName.ToString

        ButtonColor2.Text = "Documents"
        ButtonColor2.mPathExe = "explorer.exe"
        ButtonColor2.mArguments(0) = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

        ButtonColor3.Text = "Images"
        ButtonColor3.mPathExe = "explorer.exe"
        ButtonColor3.mArguments(0) = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)

        ButtonColor4.Text = "Music"
        ButtonColor4.mPathExe = "explorer.exe"
        ButtonColor4.mArguments(0) = System.Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)

        ButtonColor5.Text = "Recents Items" 'need a little listView
        ButtonColor5.mPathExe = "explorer.exe"
        ButtonColor5.mArguments(0) = System.Environment.GetFolderPath(Environment.SpecialFolder.Recent)

        ButtonColor6.Text = "Computer"
        ButtonColor6.mPathExe = "explorer.exe"
        ButtonColor6.mArguments(0) = System.Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)

        ButtonColor7.Text = "Network" 'i don't know how to get the same windows
        ButtonColor7.mPathExe = "explorer.exe"
        ButtonColor7.mArguments(0) = System.Environment.GetFolderPath(Environment.SpecialFolder.NetworkShortcuts)

        ButtonColor8.Text = "Control Panel"
        ButtonColor8.mPathExe = "C:\Windows\System32\control.exe"

        ButtonColor9.Text = "Devices and Printers"
        ButtonColor9.mPathExe = System.Environment.GetFolderPath(Environment.SpecialFolder.PrinterShortcuts)

        ButtonColor10.Text = "Default Programs"
        ButtonColor10.mPathExe = "C:\Windows\System32\control.exe"
        ButtonColor10.mArguments(0) = "/name Microsoft.DefaultPrograms"

    End Sub

Ok, I know the code is a bit big and could be improved. Is only to show you have simple it could be.
Because if I works the old fashion way, I might have more lines because I will have a Click , a MouseHover and a MouseLeave methods for each button instead of 1 unique events for every button.

At the end, when you run the program. You will have something like this:

Mouse Over

Previous Post : Search a file

Next Post : Create searchBox using UserControl


I invite you to visit my blog for more articles and leave a comment. Check Technologies represents more than 10 years .... Computer and computer aided design.

Official Website Check Technologies


  1. We at COEPD glad to announce that we have introduced Dot Net Technologies Internship Programs (Self sponsored) for professionals who want to have hands on experience. This program is available in COEPD Hyderabad premises which is accompanied by IT Companies. It is intelligently dedicated to our firm participants predominantly acknowledging and appreciating the fact that they are on the path of making a career in Dot Net Technologies discipline. We assume Object-Oriented Programming concepts and teaches C#.NET, ADO.NET which helps the interns to build database-driven Web applications and Web Sites successfully. This internship is designed to gain theoretical knowledge and also hands-on practice and practical know-how to master the nitty-gritty of the Dot Net developer profession. More than a training institute, COEPD today stands differentiated as a mission to help you "Build your dream career" - COEPD way.