Click here to Skip to main content
Click here to Skip to main content

MBColorPicker User Control Using VB.NET 2008

, 8 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
A graphical interface to select a color from a set of various colors.

Introduction

What is the use of MBColorPicker? It provides a graphical interface to select a color from a set of various colors. It is simple to use, just drop it on a form, adjust the design time properties, and use it like normal controls.

Background

MBColorPicker is a color picker control which provides various functionalities so the user can select the required color from a set of colors. The language used is VB.NET.

Control properties

Here is the list of properties available in MBColorPicker:

  • PickedColor: This property is used to get and set the color picked by MBColorPicker.
  • Arrow: This property is used to set the arrow on the MBColorPicker button.
  • BaseColor: This property is used to set the base color of the MBColorPicker button.
  • BaseStrokeColor: This property is used to set the base stroke color of the MBColorPicker button.
  • OnColor: This property is used to set the on color of the MBColorPicker button.
  • OnStrokeColor: This property is used to set the on stroke color of the MBColorPicker button.
  • PressColor: This property is used to set the press color of the MBColorPicker button.
  • PressStrokeColor: This property is used to set the press stroke color of the MBColorPicker button.
  • Radius: This property is used to set the corner radius of the MBColorPicker button.

Code

The concept for this MBColorPicker came from the Microsoft Office 2007 color picker. I organized my paint event into layers like this:

Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
    Dim g As Graphics = pevent.Graphics
    g.SmoothingMode = SmoothingMode.HighQuality
    g.InterpolationMode = InterpolationMode.High
    Dim r As Rectangle = New Rectangle(New Point(-1, -1), New Size(Me.Width + _radius, Me.Height + _radius))
    Dim path As GraphicsPath = New GraphicsPath
    Dim rp As Rectangle = New Rectangle(New Point(0, 0), New Size(Me.Width - 1, Me.Height - 1))
    DrawArc(rp, path)
    FillGradients(g, path)
    DrawImage(g)
    DrawText(g)
    DrawArrow(g)
End Sub

Following are some methods which provide a transparent look to MBColorPicker. This method draws the background for MBColorPicker.

''' <summary>
''' Draw the MBColorPicker Control.
''' </summary>
Protected Overrides Sub OnCreateControl()
    MyBase.OnCreateControl()
    A0 = BaseColor.A
    R0 = BaseColor.R
    G0 = BaseColor.G
    B0 = BaseColor.B
    _colorStroke = _baseStroke
    Dim r As Rectangle = New Rectangle(New Point(-1, -1), _
             New Size(Me.Width + _radius, Me.Height + _radius))
    If Me.Size <> Nothing Then
        Dim pathregion As GraphicsPath = New GraphicsPath
        DrawArc(r, pathregion)
        Me.Region = New Region(pathregion)
    End If
End Sub

This method draws the color boxes for MBColorPicker:

''' <summary>
''' Draw Color Boxes For MBColorPicker.
''' </summary>
''' <param name="graphics">Graphics As Graphics</param>
Private Sub DrawColorBoxes(ByVal graphics As Graphics)
    DrawColorBelt(graphics, 0, _TopBoxMargin1, True)
    For i As Int16 = 1 To _ColorMatrixY - 2
        DrawColorBelt(graphics, i, (i - 1) * _ColorBoxUnit + _TopBoxMargin2, False)
        Next
        For i As Int16 = 0 To _ColorMatrixX - 1
            graphics.DrawRectangle(New Pen(Color.Gray), i * _
              (_ColorBoxUnit + _ColorBoxMarginX) + _ColorBoxMarginX, _
              _TopBoxMargin2, _ColorBoxUnit, _ColorBoxUnit * 5)
        Next
        DrawColorBelt(graphics, _ColorMatrixY - 1, _TopBoxMargin3, True)
End Sub

''' <summary>
''' Draw Color Belt For MBColorPicker.
''' </summary>
''' <param name="graphics">Graphics As Graphics</param>
''' <param name="no">No As Int16</param>
''' <param name="yOffSet">yOffSet As Int16</param>
''' <param name="border">Border As Boolean</param>
''' <remarks></remarks>
Private Sub DrawColorBelt(ByVal graphics As Graphics, ByVal no As Int16, _
            ByVal yOffSet As Int16, ByVal border As Boolean)
    For i As Int16 = 0 To _ColorMatrixX - 1
        DrawColorBox(graphics, _ColorMatrix(i, no), _
           New Point(i * (_ColorBoxUnit + _ColorBoxMarginX) + _ColorBoxMarginX, yOffSet), border)
    Next
End Sub

This method handles the drop down color for MBColorPicker:

Protected Overrides Sub OnMouseUp(ByVal mevent As System.Windows.Forms.MouseEventArgs)
    R0 = OnColor.R
    G0 = OnColor.G
    B0 = OnColor.B
    _colorStroke = OnStrokeColor
    _showbase = MB_ShowBase.Yes
    i_mode = 1
    mouse = True
    If _splitlocation = MB_SplitLocation.Right And xmouse > _
             Me.Width - _splitdistance And _splitbutton = MB_SplitButton.Yes Then
        If (_arrow = MB_Arrow.ToDown) Then
            Dim _MBColorPicker As MBColorPickerHelper = New MBColorPickerHelper(Me)
            _MBColorPicker.Show(Me.Parent, New Point(Me.Location.X, Me.Height + Me.Location.Y))
        ElseIf (_arrow = MB_Arrow.ToRight) Then
            Dim _MBColorPicker As MBColorPickerHelper = New MBColorPickerHelper(Me)
            _MBColorPicker.Show(Me.Parent, New Point(Me.Location.X + Me.Width, _
                   (Me.Location.Y + Me.Height / 2) - _MBColorPicker.Height / 2))
        End If
    Else
        MyBase.OnMouseUp(mevent)
        If (_Keeppressed) Then
            _Ispressed = True
            For Each _control As Control In Me.Parent.Controls
                If _control.Name <> Me.Name And TypeOf (_control) Is MBColorPickerButton = True Then
                    CType(_control, MBColorPickerButton)._Ispressed = False
                    CType(_control, MBColorPickerButton).UpdateMouseLeave()
                End If
            Next
        End If
    End If
End Sub

Using the code

It is very easy to use the MBColorPicker in your application. Just add the reference of the provided DLL to your application and just drag and drop.

History

  • MBColorPicker version 1.0.

License

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

Share

About the Author

Manoj K Bhoir
Software Developer
India India
It always seems good to hear about me, but the thing I do is I code. I'm Interested in Designing Windows Based Application, Web Applications and building Mobile Applications. Currently restricting it to Android 4.0 applications, building Internet Based Applications using ASP.NET and contributing to bring the student community to a position which will help technology to reach the greatest heights ever. A very Big fan of Microsoft & Android..!!
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionColor Picker PinmemberJeff.Crawford23-Apr-12 2:05 
AnswerRe: Color Picker PinmemberManoj K Bhoir24-Apr-12 18:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 9 Sep 2013
Article Copyright 2012 by Manoj K Bhoir
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid