Click here to Skip to main content
15,893,381 members
Articles / Programming Languages / Visual Basic

FlagColumn Outlook Style

Rate me:
Please Sign up or sign in to vote.
3.82/5 (5 votes)
3 Feb 2007CPOL 36.8K   551   35  
Creating a DataGridViewFlagColumn in Outlook style.
Imports System.Windows.Forms
Imports System.Drawing

Public NotInheritable Class DataGridViewFlagColumn
    Inherits DataGridViewImageColumn

    'A constructor is all we need in this class...
    Public Sub New()
        'Assigning my FlagColumn as the CellTemplate...
        Me.CellTemplate = New DataGridViewFlagCell

        'Setting some properties of my Flagcolumn...
        Me.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

        'The Flag is an enumaration, so we choose Integer as ValueType...
        Me.ValueType = GetType(Integer)

        Me.SortMode = DataGridViewColumnSortMode.Automatic
        Me.Width = 25
    End Sub

End Class

Public NotInheritable Class DataGridViewFlagCell
    Inherits DataGridViewImageCell

    Public Sub New()
        Me.ValueType = GetType(Integer)
    End Sub

    Protected Overrides Function GetFormattedValue(ByVal value As Object, ByVal rowIndex As Integer, ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal valueTypeConverter As System.ComponentModel.TypeConverter, ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object
        Return DataGridViewFlagCell.GetImage(value)
    End Function

    Public Overrides ReadOnly Property DefaultNewRowValue() As Object
        Get
            Return MyBase.DefaultNewRowValue
        End Get
    End Property

    Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal cellState As System.Windows.Forms.DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
        cellStyle.BackColor = DataGridViewFlagCell.GetBackColor(value, cellBounds, Me.DataGridView.PointToClient(Control.MousePosition))
        MyBase.Paint(graphics, _
                        clipBounds, _
                        cellBounds, _
                        rowIndex, _
                        cellState, _
                        value, _
                        DataGridViewFlagCell.GetImage(value), _
                        errorText, _
                        cellStyle, _
                        advancedBorderStyle, _
                        (paintParts And Not DataGridViewPaintParts.SelectionBackground))
    End Sub

    'Give the cell another color when mouse is entering...
    Protected Overrides Sub OnMouseEnter(ByVal rowIndex As Integer)
        MyBase.OnMouseEnter(rowIndex)
        Me.DataGridView.InvalidateColumn(Me.ColumnIndex)
    End Sub

    'Reset the cell its backcolor when the mouse leaves the cell...
    Protected Overrides Sub OnMouseLeave(ByVal rowIndex As Integer)
        MyBase.OnMouseLeave(rowIndex)
        Me.DataGridView.InvalidateColumn(Me.ColumnIndex)
    End Sub

    Private Shared Function GetImage(ByVal flag As Flag)
        Select Case flag
            Case flag.Blank
                Return My.Resources.Flag_Blank_Border
            Case flag.Red
                Return My.Resources.Flag_Red_Border
            Case flag.Blue
                Return My.Resources.Flag_Blue_Border
            Case flag.Yellow
                Return My.Resources.Flag_Yellow_Border
        End Select
        Return My.Resources.ErrorIcon.ToBitmap
    End Function

    Private Shared Function GetBackColor(ByVal flag As Flag, ByVal cellBounds As Rectangle, ByVal mouseLocation As Point) As Color
        If cellBounds.Contains(mouseLocation) Then
            Return Color.FromArgb(255, 238, 194)
        Else
            Select Case flag
                Case flag.Blank
                    Return SystemColors.ControlLight
                Case flag.Red
                    Return Color.FromArgb(207, 93, 96)
                Case flag.Blue
                    Return Color.FromArgb(92, 131, 180)
                Case flag.Yellow
                    Return Color.FromArgb(255, 207, 93)
            End Select
        End If

    End Function

End Class

Public Enum Flag As Integer
    Blank = 0
    Red = 1
    Blue = 2
    Yellow = 3
End Enum

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
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions