How to create multiple colunms combobox and custom drop down columns width - TOP10

How to create multiple colunms combobox and custom drop down columns width



Step 1: Create Class Override Method by inherit from combobox controll class to create new combobox.

Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Drawing
Imports System.Globalization
Public Class ComboxMultiColumn
    Inherits ComboBox
    Public _padding As Integer
    Public colIndex As Integer = 0
    Public columnWidths As Single() = New Single(-1) {}

    Public Sub New()
        DrawMode = DrawMode.OwnerDrawVariable
    End Sub

    Public Shadows Property DrawMode() As DrawMode
        Get
            Return MyBase.DrawMode
        End Get
        Set(value As DrawMode)
            If value <> DrawMode.OwnerDrawVariable Then
                Throw New NotSupportedException("Needs to be DrawMode.OwnerDrawVariable")
            End If
            MyBase.DrawMode = value
        End Set
    End Property
    
    Public Property columnPadding As Integer
        Get
            Return _padding
        End Get
        Set(value As Integer)
            _padding = value
        End Set
    End Property
     
    Public Shadows Property DropDownStyle() As ComboBoxStyle
        Get
            Return MyBase.DropDownStyle
        End Get
        Set(value As ComboBoxStyle)
            If value = ComboBoxStyle.Simple Then
                Throw New NotSupportedException("ComboBoxStyle.Simple not supported")
            End If
            MyBase.DropDownStyle = value
        End Set
    End Property

    Protected Overrides Sub OnDataSourceChanged(e As EventArgs)
        MyBase.OnDataSourceChanged(e)

        InitializeColumns()
    End Sub

    Protected Overrides Sub OnValueMemberChanged(e As EventArgs)
        MyBase.OnValueMemberChanged(e)

        InitializeValueMemberColumn()
    End Sub

    Protected Overrides Sub OnDropDown(e As EventArgs)
        MyBase.OnDropDown(e)
        Me.DropDownWidth = CInt(Math.Truncate(CalculateTotalWidth()))
    End Sub


    Private columnNames As [String]() = New [String](-1) {}
    Private valueMemberColumnIndex As Integer = 0

    Private Sub InitializeColumns()
        Dim propertyDescriptorCollection As PropertyDescriptorCollection = DataManager.GetItemProperties()

        columnWidths = New Single(propertyDescriptorCollection.Count - 1) {}
        columnNames = New [String](propertyDescriptorCollection.Count - 1) {}

        For colIndex As Integer = 0 To propertyDescriptorCollection.Count - 1
            Dim name As [String] = propertyDescriptorCollection(colIndex).Name
            columnNames(colIndex) = name
        Next
    End Sub

    Private Sub InitializeValueMemberColumn()
        Dim colIndex As Integer = 0
        For Each columnName As [String] In columnNames
            If [String].Compare(columnName, ValueMember, True, CultureInfo.CurrentUICulture) = 0 Then
                valueMemberColumnIndex = colIndex
                Exit For
            End If
            colIndex += 1
        Next

    End Sub

    Private Function CalculateTotalWidth() As Single

        Dim totWidth As Single = 0
        For Each width As Integer In columnWidths
            totWidth += (width + columnPadding)
        Next
        Return totWidth + SystemInformation.VerticalScrollBarWidth
    End Function
    
    Protected Overrides Sub OnMeasureItem(e As MeasureItemEventArgs)
        MyBase.OnMeasureItem(e)

        If DesignMode Then
            Return
        End If
        Dim totWidth As Single = CalculateTotalWidth()
        e.ItemWidth = CInt(Math.Truncate(totWidth))
    End Sub
    Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
        MyBase.OnDrawItem(e)

        If DesignMode Then
            Return
        End If

        e.DrawBackground()

        Dim boundsRect As Rectangle = e.Bounds
        Dim lastRight As Integer = 0

        Using linePen As New Pen(SystemColors.GrayText)
            Using brush As New SolidBrush(ForeColor)
                If columnNames.Length = 0 Then
                    e.Graphics.DrawString(Convert.ToString(Items(e.Index)), Font, brush, boundsRect)
                Else
                    For colIndex As Integer = 0 To columnNames.Length - 1
                        Dim item As String = Convert.ToString(FilterItemOnProperty(Items(e.Index), columnNames(colIndex)))

                        boundsRect.X = lastRight
                        boundsRect.Width = CInt(Math.Truncate(columnWidths(colIndex))) + columnPadding
                        lastRight = boundsRect.Right

                        If colIndex = valueMemberColumnIndex Then
                            Using boldFont As New Font(Font, FontStyle.Regular)
                                e.Graphics.DrawString(item, boldFont, brush, boundsRect)
                            End Using
                        Else
                            e.Graphics.DrawString(item, Font, brush, boundsRect)
                        End If

                        If colIndex < columnNames.Length - 1 Then
                            e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom)
                        End If
                    Next
                End If
            End Using
        End Using
        e.DrawFocusRectangle()
    End Sub
End Class

Step 2: Debug your project to create new combobox controll  



Step 3: Drag drop it to use  

Step 4: Binding Data Source to it 

Step 5: Custom columns drop down width (Write Code on event form load)

            CbBr.columnWidths(0) = Math.Max(CbBr.columnWidths(0), 295)

            CbBr.columnWidths(1) = Math.Max(CbBr.columnWidths(1), 400)

            CbBr.columnWidths(2) = Math.Max(CbBr.columnWidths(2), 60)

            CbBr.columnWidths(3) = Math.Max(CbBr.columnWidths(3), 100)  

Step 6 : Enjoy your work 






Previous
Next Post »

1 comments:

Write comments