BE SOCIAL

Tuesday, November 25, 2014

Creating Custom Buttons in Visual Studio

A short YouTube video tutorial on creating custom graphical buttons for your .NET applications



In this tutorial you will learn how to use a PictureBox to work as a button in your application. By changing the PictureBox image you can achieve a graphical effects that are only limited by your creativity and graphic design skills. We also provide here full source code used in this demo.

FORM SOURCE CODE



Public Class grphButton



    Private Sub lbMsg_Click(sender As System.Object, e As System.EventArgs) Handles lbMsg.Click

        lbMsg.Text = ""

    End Sub


    Private Sub picHome_Click(sender As System.Object, e As System.EventArgs) Handles picHome.Click
        Try
            Dim tPic As PictureBox = DirectCast(sender, PictureBox)
            If Not IsNothing(tPic.Image) Then
                tPic.Image.Dispose()
                tPic.Image = Nothing
            End If
            tPic.Image = Global.CustomButton.My.Resources.btnDemoHome30_MOver
            lbMsg.Text = "HOME BUTTON CLICKED"
        Catch ex As Exception

        End Try
    End Sub

    Private Sub picHome_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles picHome.MouseDown
        Try
            Dim tPic As PictureBox = DirectCast(sender, PictureBox)
            If Not IsNothing(tPic.Image) Then
                tPic.Image.Dispose()
                tPic.Image = Nothing
            End If
            tPic.Image = Global.CustomButton.My.Resources.btnDemoHome30_MClick
        Catch ex As Exception

        End Try
    End Sub

    Private Sub picHome_MouseEnter(sender As Object, e As System.EventArgs) Handles picHome.MouseEnter
        Try
            Dim tPic As PictureBox = DirectCast(sender, PictureBox)
            If Not IsNothing(tPic.Image) Then
                tPic.Image.Dispose()
                tPic.Image = Nothing
            End If
            tPic.Image = Global.CustomButton.My.Resources.btnDemoHome30_MOver
            lbMsg.Text = "MOUSE OVER HOME BUTTON"
        Catch ex As Exception

        End Try
    End Sub

    Private Sub picHome_MouseLeave(sender As Object, e As System.EventArgs) Handles picHome.MouseLeave
        Try
            Dim tPic As PictureBox = DirectCast(sender, PictureBox)
            If Not IsNothing(tPic.Image) Then
                tPic.Image.Dispose()
                tPic.Image = Nothing
            End If
            tPic.Image = Global.CustomButton.My.Resources.btnDemoHome30
            lbMsg.Text = ""
        Catch ex As Exception

        End Try
    End Sub

    Private Sub picOK_Click(sender As System.Object, e As System.EventArgs) Handles picOK.Click
        Try
            Dim tPic As PictureBox = DirectCast(sender, PictureBox)
            If Not IsNothing(tPic.Image) Then
                tPic.Image.Dispose()
                tPic.Image = Nothing
            End If
            tPic.Image = Global.CustomButton.My.Resources.btnOK_CheckGreen30_MOver
            lbMsg.Text = "OK BUTTON CLICKED" 
       Catch ex As Exception
        End Try
    End Sub

    Private Sub picOK_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles picOK.MouseDown
        Try
            Dim tPic As PictureBox = DirectCast(sender, PictureBox)
            If Not IsNothing(tPic.Image) Then
                tPic.Image.Dispose()
                tPic.Image = Nothing
            End If
            tPic.Image = Global.CustomButton.My.Resources.btnOK_CheckGreen30_MClick
        Catch ex As Exception

        End Try
    End Sub

    Private Sub picOK_MouseEnter(sender As Object, e As System.EventArgs) Handles picOK.MouseEnter
        Try
            Dim tPic As PictureBox = DirectCast(sender, PictureBox)
            If Not IsNothing(tPic.Image) Then
                tPic.Image.Dispose()
                tPic.Image = Nothing
            End If
            tPic.Image = Global.CustomButton.My.Resources.btnOK_CheckGreen30_MOver
            lbMsg.Text = "MOUSE OVER OK BUTTON"
        Catch ex As Exception

        End Try
    End Sub

    Private Sub picOK_MouseLeave(sender As Object, e As System.EventArgs) Handles picOK.MouseLeave
        Try
            Dim tPic As PictureBox = DirectCast(sender, PictureBox)
            If Not IsNothing(tPic.Image) Then
                tPic.Image.Dispose()
                tPic.Image = Nothing
            End If
            tPic.Image = Global.CustomButton.My.Resources.btnOK_CheckGreen30
            lbMsg.Text = ""
        Catch ex As Exception

        End Try
    End Sub

    Private Sub picCancel_Click(sender As System.Object, e As System.EventArgs) Handles picCancel.Click
        Try
            Dim tPic As PictureBox = DirectCast(sender, PictureBox)
            If Not IsNothing(tPic.Image) Then
                tPic.Image.Dispose()
                tPic.Image = Nothing
            End If
            tPic.Image = Global.CustomButton.My.Resources.btnCancel_XRed30_MOver
            lbMsg.Text = "CANCEL BUTTON CLICKED" 
        Catch ex As Exception

        End Try
    End Sub

    Private Sub picCancel_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles picCancel.MouseDown
        Try
            Dim tPic As PictureBox = DirectCast(sender, PictureBox)
            If Not IsNothing(tPic.Image) Then
                tPic.Image.Dispose()
                tPic.Image = Nothing
            End If
            tPic.Image = Global.CustomButton.My.Resources.btnCancel_XRed30_MClick
        Catch ex As Exception

        End Try
    End Sub

    Private Sub picCancel_MouseEnter(sender As Object, e As System.EventArgs) Handles picCancel.MouseEnter
        Try
            Dim tPic As PictureBox = DirectCast(sender, PictureBox)
            If Not IsNothing(tPic.Image) Then
                tPic.Image.Dispose()
                tPic.Image = Nothing
            End If
            tPic.Image = Global.CustomButton.My.Resources.btnCancel_XRed30_MOver
            lbMsg.Text = "MOUSE OVER CANCEL BUTTON"
        Catch ex As Exception

        End Try
    End Sub

    Private Sub picCancel_MouseLeave(sender As Object, e As System.EventArgs) Handles picCancel.MouseLeave
        Try
            Dim tPic As PictureBox = DirectCast(sender, PictureBox)
            If Not IsNothing(tPic.Image) Then
                tPic.Image.Dispose()
                tPic.Image = Nothing
            End If
            tPic.Image = Global.CustomButton.My.Resources.btnCancel_XRed30
            lbMsg.Text = ""
        Catch ex As Exception

        End Try

    End Sub
End Class

No comments:

Post a Comment