Click here to Skip to main content
15,892,059 members
Articles / Multimedia / GDI+

Custom Button Control with Gradient Colors and Extra Image (VB.NET)

Rate me:
Please Sign up or sign in to vote.
4.91/5 (131 votes)
19 Mar 2015CPOL7 min read 818.3K   46.9K   232  
This is a simple to use custom button control, but with a lot of visual design options.
Public Class Form1

    Private Sub CButtonGDExit_ClickButtonArea(ByVal Sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CButtonGDExit.ClickButtonArea
        Me.Close()
    End Sub

    Private Sub CButtonMoveUp_ClickButtonArea(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CButtonMoveUp.ClickButtonArea
        Dim y As Integer = lblMove.Location.Y - 4
        If y < 0 Then y = 0
        lblMove.Location = New Size(lblMove.Location.X, y)
    End Sub

    Private Sub CButtonMoveDn_ClickButtonArea(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CButtonMoveDn.ClickButtonArea
        Dim y As Integer = lblMove.Location.Y + 4
        If y > 100 - lblMove.Height Then y = 100 - lblMove.Height
        lblMove.Location = New Size(lblMove.Location.X, y)
    End Sub

    Private Sub CButtonMoveRight_ClickButtonArea(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CButtonMoveRight.ClickButtonArea
        Dim x As Integer = lblMove.Location.X + 4
        If x > 100 - lblMove.Width Then x = 100 - lblMove.Width
        lblMove.Location = New Size(x, lblMove.Location.Y)
    End Sub

    Private Sub CButtonMoveLeft_ClickButtonArea(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CButtonMoveLeft.ClickButtonArea
        Dim x As Integer = lblMove.Location.X - 4
        If x < 0 Then x = 0
        lblMove.Location = New Size(x, lblMove.Location.Y)
    End Sub

    Private Sub CButtonMoveCenter_ClickButtonArea(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CButtonMoveCenter.ClickButtonArea
        lblMove.Location = New Size((100 - lblMove.Width) / 2, (92 - lblMove.Height) / 2)
    End Sub

    Private Sub chkEnabledExamples_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEnabledExamples.CheckedChanged
        CButtonBB.Enabled = chkEnabledExamples.Checked
        CButtonBob.Enabled = chkEnabledExamples.Checked
        CButtonBubble.Enabled = chkEnabledExamples.Checked
        CButtonDuhMan.Enabled = chkEnabledExamples.Checked
        CButtonJack.Enabled = chkEnabledExamples.Checked
        CButtonHHG.Enabled = chkEnabledExamples.Checked
        CButtonGDExit.Enabled = chkEnabledExamples.Checked
    End Sub

    Private Sub chkPanelBack_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPanelBack.CheckedChanged
        CButtonOverPanel1.Enabled = chkPanelBack.Checked
        CButtonOverPanel2.Enabled = chkPanelBack.Checked
        CButtonOverPanel3.Enabled = chkPanelBack.Checked

    End Sub

    Private Sub CButton10_ClickButtonArea(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CButton10.ClickButtonArea
        Dim bm As Bitmap = CButton10.SideImage
        bm.MakeTransparent(bm.GetPixel(0, 0))
    End Sub

    Private Sub CButtonCustom_ClickButtonArea(ByVal Sender As System.Object, _
      ByVal e As System.Windows.Forms.MouseEventArgs) Handles CButtonCustom.ClickButtonArea

        'How to change the Blend in RunTime
        CButtonCustom.ColorFillBlend = _
            New CButtonLib.cBlendItems( _
            New Color() { _
            Color.Red, _
            Color.White, _
            Color.Blue}, _
            New Single() {0, 0.5, 1})

    End Sub

    Private Sub cbutClickThrough_ClickButtonArea(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbutClickThrough.ClickButtonArea
        frmClickThrough.ShowDialog()
    End Sub

    Private Sub CButtonON_ClickButtonArea(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CButtonON.ClickButtonArea

        With CButtonON
            If .Text = "&On" Then
                .ColorFillSolid = Color.Red
                .Text = "&Off"
            Else
                .ColorFillSolid = Color.Green
                .Text = "&On"
            End If
        End With
    End Sub

    Private Sub cbutDialogResult_ClickButtonArea(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbutDialogResult.ClickButtonArea

        Dim res As DialogResult = frmDialog.ShowDialog

        MsgBox(String.Format("You pressed the {0} button.", res.ToString))

    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I first got hooked on programing with the TI994A. After it finally lost all support I reluctantly moved to the Apple IIe. Thank You BeagleBros for getting me through. I wrote programs for my Scuba buisness during this time. Currently I am a Database manager and software developer. I started with VBA and VB6 and now having fun with VB.NET/WPF/C#...

Comments and Discussions