Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. ComboBox - MultiColumn

ComboBox - MultiColumn

Scheduled Pinned Locked Moved Visual Basic
csharpquestion
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikasa
    wrote on last edited by
    #1

    Hello, I am trying to find out if anyone has a ComboBox Control for Visual Basic .NET that will handle Multi-Columns. Also, I have seen a Drop-Down TreeView Control as well on this site...has anyone Implemented this into .NET yet? I could really use both of these ASAP!

    Richard DeemingR 5 Replies Last reply
    0
    • M mikasa

      Hello, I am trying to find out if anyone has a ComboBox Control for Visual Basic .NET that will handle Multi-Columns. Also, I have seen a Drop-Down TreeView Control as well on this site...has anyone Implemented this into .NET yet? I could really use both of these ASAP!

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      I've got multi-column data-bound ComboBox and ListBox controls, with support for column images and gridlines. They're very easy to do, so I don't think they're worth an article. Let me know if you want me to post the code here. :-D

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • M mikasa

        Hello, I am trying to find out if anyone has a ComboBox Control for Visual Basic .NET that will handle Multi-Columns. Also, I have seen a Drop-Down TreeView Control as well on this site...has anyone Implemented this into .NET yet? I could really use both of these ASAP!

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        I'll split this code up over several messages. First, define the Data column object:

        Imports System.ComponentModel
        Imports System.Drawing

        <ToolboxItem(""), DesignTimeVisible(False)> _
        Public Class DataColumn : Inherits Component

        Protected Shared \_StringFormatter As StringFormat
        Shared Sub New()
            \_StringFormatter = New StringFormat()
            With \_StringFormatter
                .Trimming = StringTrimming.EllipsisCharacter
                .FormatFlags = StringFormatFlags.NoWrap
                .LineAlignment = StringAlignment.Center
            End With
        End Sub
        

        #Region "Member Vars"
        Private _Image As Image
        Private _DataMember As String
        Private _Tag As Object
        Private _Width As Integer
        Private _Parent As DataColumnCollection
        #End Region

        #Region "Constructors"
        Public Sub New()
        _Image = Nothing
        _Width = 75
        _DataMember = String.Empty
        _Tag = Nothing
        End Sub
        Public Sub New(ByVal Image As Image)
        _Image = Image
        _Width = 22
        _DataMember = String.Empty
        _Tag = Nothing
        End Sub
        Public Sub New(ByVal DataMember As String)
        _Image = Nothing
        _Width = 75
        _DataMember = DataMember
        _Tag = Nothing
        End Sub
        Public Sub New(ByVal Width As Integer)
        _Image = Nothing
        _Width = Width
        _DataMember = String.Empty
        _Tag = Nothing
        End Sub
        Public Sub New(ByVal DataMember As String, ByVal Width As Integer)
        _Image = Nothing
        _Width = Width
        _DataMember = DataMember
        _Tag = Nothing
        End Sub
        Public Sub New(ByVal Image As Image, _
        ByVal DataMember As String, _
        ByVal Width As Integer)
        _Image = Image
        _Width = Width
        _DataMember = DataMember
        _Tag = Nothing
        End Sub
        #End Region

        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (\_Image Is Nothing) Then
                    Try
                        \_Image.Dispose()
                    Finally
                        \_Image = Nothing
                    End Try
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
        

        #Region "Properties"
        <Editor("System.Windows.Forms.Design.DataMemberFieldEditor", _
        "System.Drawing.Design.UITypeEditor"), _
        Category("Behavior")> _
        Public Overridable Property DataMember() As Strin

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        • M mikasa

          Hello, I am trying to find out if anyone has a ComboBox Control for Visual Basic .NET that will handle Multi-Columns. Also, I have seen a Drop-Down TreeView Control as well on this site...has anyone Implemented this into .NET yet? I could really use both of these ASAP!

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Next, define a collection of DataColumns:

          Imports System.ComponentModel
          Imports System.Collections
          Imports System.Windows.Forms

          Public Class DataColumnCollection : Inherits CollectionBase

          Private \_Parent As Control
          

          #Region "Constructors"
          Public Sub New()
          'Do Nothing
          End Sub
          Public Sub New(ByVal Value() As DataColumn)
          AddRange(Value)
          End Sub
          Public Sub New(ByVal Value As DataColumnCollection)
          AddRange(Value)
          End Sub
          #End Region

          #Region "Add"
          Public Function Add(ByVal Value As DataColumn) As Integer
          Value.Parent = Me
          Return List.Add(Value)
          End Function
          Public Sub AddRange(ByVal Value() As DataColumn)
          Dim oItem As DataColumn
          For Each oItem In Value
          oItem.Parent = Me
          List.Add(oItem)
          Next
          End Sub
          Public Sub AddRange(ByVal Value As DataColumnCollection)
          Dim oItem As DataColumn
          For Each oItem In Value
          oItem.Parent = Me
          List.Add(oItem)
          Next
          End Sub
          #End Region

          Public Function Contains(ByVal Value As DataColumn) As Boolean
              Return List.Contains(Value)
          End Function
          Public Sub CopyTo(ByVal array() As DataColumn, ByVal index As Integer)
              List.CopyTo(array, index)
          End Sub
          Public Overloads Function IndexOf(ByVal Value As DataColumn) As Integer
              Return List.IndexOf(Value)
          End Function
          Public Sub Insert(ByVal index As Integer, ByVal Value As DataColumn)
              List.Insert(index, Value)
          End Sub
          
          Default Public Property Item(ByVal index As Integer) As DataColumn
              Get
                  Return DirectCast(List.Item(index), DataColumn)
              End Get
              Set(ByVal Value As DataColumn)
                  List.Item(index) = Value
              End Set
          End Property
          
          
          Protected Overrides Sub OnInsert(ByVal index As Integer, \_
                                                      ByVal Value As Object)
          
              If TypeOf Value Is DataColumn Then
                  MyBase.OnInsert(index, Value)
                  DirectCast(Value, DataColumn).Parent = Me
              Else
                  Throw New ArgumentException()
              End If
          End Sub
          
          Protected Overrides Sub OnSet(ByVal index As Integer, \_
                                                  ByVal OldValue As Object, \_
                                                  ByVal NewValue As Object)
          
              If TypeOf NewValue Is DataColumn Then
                  MyBase
          

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • M mikasa

            Hello, I am trying to find out if anyone has a ComboBox Control for Visual Basic .NET that will handle Multi-Columns. Also, I have seen a Drop-Down TreeView Control as well on this site...has anyone Implemented this into .NET yet? I could really use both of these ASAP!

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            Next, a base for the ComboBox / ListBox:

            Imports System.Drawing
            Imports System.Windows.Forms
            Imports System.ComponentModel

            Public Class ComboBoxEx : Inherits ComboBox
            'Or - Public Class ListBoxEx : Inherits ListBox
            Protected HighlightBrush As Brush
            Protected HighlightPen As Pen

            #Region "Constructors"
            Public Sub New()
            DrawMode = DrawMode.OwnerDrawFixed
            ItemHeight += 1
            HighlightBrush = New SolidBrush(SystemColors.Highlight)
            HighlightPen = New Pen(SystemColors.HighlightText)
            End Sub
            #End Region

            #Region "Overrides"
            Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
            If Not (e.Index = -1) Then
            DrawListBoxItem(e)
            End If
            End Sub

            Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
                If disposing Then
                    HighlightBrush.Dispose()
                    HighlightPen.Dispose()
                End If
                MyBase.Dispose(disposing)
            End Sub
            
            <Browsable(False)> \_
            Public Shadows Property DrawMode() As DrawMode
                Get
                    Return MyBase.DrawMode
                End Get
                Set(ByVal Value As DrawMode)
                    MyBase.DrawMode = Value
                End Set
            End Property
            

            #End Region

            #Region "Implementation"
            Protected Sub DrawListBoxItem(ByVal e As DrawItemEventArgs)
            '// Draw List box item
            If Enabled AndAlso _
            ((e.State And DrawItemState.Selected) = DrawItemState.Selected) Then

                    DrawItemHighlight(e)
                Else
                    DrawItemClearHighlight(e)
                End If
                DrawItemText(e)
            End Sub
            
            Protected Overridable Sub DrawItemText(ByVal e As DrawItemEventArgs)
                Dim currentObject As Object = Items(e.Index)
                Dim item As String = GetItemText(currentObject)
                If Not (item Is Nothing) Then
                    Dim B As Brush
                    If Enabled Then
                        B = New SolidBrush(e.ForeColor)
                    Else
                        B = SystemBrushes.ControlDark
                    End If
            
                    e.Graphics.DrawString(item, \_
                            e.Font, \_
                            B, \_
                            New PointF(e.Bounds.Left + 2, e.Bounds.Top))
            
                    If Enabled Then B.Dispose()
                End If
            End Sub
            
            Protected Overridable Sub DrawItemHighlight(ByVal e As DrawItemEventArgs)
                Dim g As Graphics = e.Graphics
                Dim b As Rectangle = e.Bounds
                g.FillRectangle(HighlightBrush, b)
                g.DrawRectangle(Highli
            

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • M mikasa

              Hello, I am trying to find out if anyone has a ComboBox Control for Visual Basic .NET that will handle Multi-Columns. Also, I have seen a Drop-Down TreeView Control as well on this site...has anyone Implemented this into .NET yet? I could really use both of these ASAP!

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              Finally, define the List/ComboBox:

              Imports System.Drawing
              Imports System.Windows.Forms
              Imports System.ComponentModel

              <DesignerCategory("Component"), _
              DefaultProperty("DataSource"), _
              TypeConverter("System.Windows.Forms.Design.DataSourceConverter"), _
              ToolboxItem(True)> _
              Public Class DataComboBoxEx : Inherits ComboBoxEx
              'Or - Public Class DataListBoxEx : Inherits ListBoxEx

              Private \_Columns As DataColumnCollection
              Private \_GridLineColor As Color
              Private \_ShowHorizontalGridLines As Boolean
              Private \_ShowVerticalGridLines As Boolean
              
              Public Sub New()
                  \_ShowVerticalGridLines = True
                  \_ShowHorizontalGridLines = False
                  \_Columns = New DataColumnCollection()
                  \_Columns.Parent = Me
              
                  \_GridLineColor = SystemColors.ControlDark
                  ItemHeight = Math.Max(ItemHeight, 22)
              End Sub
              
              Protected Overrides Sub DrawItemText( \_
                      ByVal e As System.Windows.Forms.DrawItemEventArgs)
              
                  Dim rItem As Rectangle = e.Bounds
                  Dim g As Graphics = e.Graphics
                  Dim gPen As New Pen(\_GridLineColor)
                  Dim DS As Object
                  If DataSource Is Nothing Then
                      DS = Items
                  Else
                      DS = DataSource
                  End If
              
                  Dim rItemF As New RectangleF(rItem.X, rItem.Y, rItem.Width, rItem.Height)
                  Dim oItem As DataColumn
                  For Each oItem In \_Columns
                      rItemF.Width = oItem.Width
                      oItem.OnDraw(e, rItemF, DS, DisplayMember)
                      If \_ShowVerticalGridLines Then
                          g.DrawLine(gPen, rItemF.Right, rItemF.Y, rItemF.Right, rItemF.Bottom)
                      End If
                      rItemF.X += rItemF.Width
                  Next
                  If \_ShowHorizontalGridLines Then
                      g.DrawLine(gPen, rItem.X, rItem.Bottom - 1, rItem.Right, rItem.Bottom - 1)
                  End If
              
                  gPen.Dispose()
              End Sub
              
              <Category("Behavior"), \_
              DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> \_
              Public Property Columns() As DataColumnCollection
                  Get
                      Return \_Columns
                  End Get
                  Set(ByVal Value As DataColumnCollection)
                      \_Columns = Value
                      \_Columns.Parent = Me
                  End Set
              End Property
              
              <Category("Appearance")> \_
              Public Property GridLineColor() As Color
                  Get
                      Return \_GridLineColor
                  End Get
                  Set(ByVal Value As Color)
                      \_GridLineColor = Value
                  End Set
              End Property
              

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              M 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Finally, define the List/ComboBox:

                Imports System.Drawing
                Imports System.Windows.Forms
                Imports System.ComponentModel

                <DesignerCategory("Component"), _
                DefaultProperty("DataSource"), _
                TypeConverter("System.Windows.Forms.Design.DataSourceConverter"), _
                ToolboxItem(True)> _
                Public Class DataComboBoxEx : Inherits ComboBoxEx
                'Or - Public Class DataListBoxEx : Inherits ListBoxEx

                Private \_Columns As DataColumnCollection
                Private \_GridLineColor As Color
                Private \_ShowHorizontalGridLines As Boolean
                Private \_ShowVerticalGridLines As Boolean
                
                Public Sub New()
                    \_ShowVerticalGridLines = True
                    \_ShowHorizontalGridLines = False
                    \_Columns = New DataColumnCollection()
                    \_Columns.Parent = Me
                
                    \_GridLineColor = SystemColors.ControlDark
                    ItemHeight = Math.Max(ItemHeight, 22)
                End Sub
                
                Protected Overrides Sub DrawItemText( \_
                        ByVal e As System.Windows.Forms.DrawItemEventArgs)
                
                    Dim rItem As Rectangle = e.Bounds
                    Dim g As Graphics = e.Graphics
                    Dim gPen As New Pen(\_GridLineColor)
                    Dim DS As Object
                    If DataSource Is Nothing Then
                        DS = Items
                    Else
                        DS = DataSource
                    End If
                
                    Dim rItemF As New RectangleF(rItem.X, rItem.Y, rItem.Width, rItem.Height)
                    Dim oItem As DataColumn
                    For Each oItem In \_Columns
                        rItemF.Width = oItem.Width
                        oItem.OnDraw(e, rItemF, DS, DisplayMember)
                        If \_ShowVerticalGridLines Then
                            g.DrawLine(gPen, rItemF.Right, rItemF.Y, rItemF.Right, rItemF.Bottom)
                        End If
                        rItemF.X += rItemF.Width
                    Next
                    If \_ShowHorizontalGridLines Then
                        g.DrawLine(gPen, rItem.X, rItem.Bottom - 1, rItem.Right, rItem.Bottom - 1)
                    End If
                
                    gPen.Dispose()
                End Sub
                
                <Category("Behavior"), \_
                DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> \_
                Public Property Columns() As DataColumnCollection
                    Get
                        Return \_Columns
                    End Get
                    Set(ByVal Value As DataColumnCollection)
                        \_Columns = Value
                        \_Columns.Parent = Me
                    End Set
                End Property
                
                <Category("Appearance")> \_
                Public Property GridLineColor() As Color
                    Get
                        Return \_GridLineColor
                    End Get
                    Set(ByVal Value As Color)
                        \_GridLineColor = Value
                    End Set
                End Property
                
                M Offline
                M Offline
                mikasa
                wrote on last edited by
                #7

                Ok, this is good. It will probably take some time for me to evaluate everything here. Will this support "unbound" data as well? Thanks a lot! :-D

                Richard DeemingR 1 Reply Last reply
                0
                • M mikasa

                  Ok, this is good. It will probably take some time for me to evaluate everything here. Will this support "unbound" data as well? Thanks a lot! :-D

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  It should do - if you don't specify a data source, it will use the Items property, which implements IList, so it should pick up the items from that.

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    I've got multi-column data-bound ComboBox and ListBox controls, with support for column images and gridlines. They're very easy to do, so I don't think they're worth an article. Let me know if you want me to post the code here. :-D

                    M Offline
                    M Offline
                    mikasa
                    wrote on last edited by
                    #9

                    Hey, thanks for the help a while back! I was wondering if you could maybe send me the ".vb" file as an attachment to my email address. The formatting on this website when I copy and paste is horrendous! Greatly appreciated!

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups