Home All Groups Group Topic Archive Search About

Multiple columns in Combobox list

Author
9 Jan 2006 7:05 PM
Keith G
I am using Visual Studio 2003.
In the standard combobox control it would appear that only 1 column of data
can be displayed in the list (as stipulated in the DisplayMember property).
In VBA it was always possible to specify a column count and then set the
width for each column (setting 0 width for columns not to be displayed). The
Bound Column property was then specified to set the data to be retrieved from
the control (the ValueMember property being the equivalent).
I would like to display 2 columns of data in the list to make it easier for
the user to select the correct data (ID and description). Once the selection
is made then only the ID is shown in the control text.
I can't see how to do this in VS 2003. Does anybody have any suggestions?

Author
9 Jan 2006 8:35 PM
Ken Halter
Show quote Hide quote
"Keith G" <Kei***@discussions.microsoft.com> wrote in message
news:711CB42C-94F1-4EC5-A7C6-B5BCCEF47461@microsoft.com...
>I am using Visual Studio 2003.
> In the standard combobox control it would appear that only 1 column of
> data
> can be displayed in the list (as stipulated in the DisplayMember
> property).
> In VBA it was always possible to specify a column count and then set the
> width for each column (setting 0 width for columns not to be displayed).
> The
> Bound Column property was then specified to set the data to be retrieved
> from
> the control (the ValueMember property being the equivalent).
> I would like to display 2 columns of data in the list to make it easier
> for
> the user to select the correct data (ID and description). Once the
> selection
> is made then only the ID is shown in the control text.
> I can't see how to do this in VS 2003. Does anybody have any suggestions?

Looks like there's still nothing "built in" but......

This page....

Visual Basic Frequently Asked Questions
http://blogs.msdn.com/vbfaq/archive/2004/04/28/121869.aspx

.....leads to an excellent resource...

vbAccelerator IconComboBox Control
http://www.vbaccelerator.com/home/NET/Code/Controls/ListBox_and_ComboBox/Icon_ComboBox/article.asp

I've never tried anything "dotNet" from vbAccelerator but his VB5/6 stuff is
top notch (it's very important to read his instructions carefully)

--
Ken Halter - MS-MVP-VB (visiting from VB6 world) - http://www.vbsight.com
Please keep all discussions in the groups..
Author
10 Jan 2006 9:59 AM
Cor Ligthert [MVP]
Keith,

I don't know if it is the same as from Ken, I got the idea from not. It
seems to be excelent (I only tried it once).

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2934&lngWId=10

I hope this helps,

Cor
Author
10 Jan 2006 5:07 PM
Keith G
Thanks Cor,

This is not the same as the ones that Ken suggested.
I agree it is an excelent bit of coding!
It's very easy to configure. The 'LoadingType' property is a nice touch,
being able to change the way the list is loaded (either from a datatable or
combobox item) is very useful.

Show quoteHide quote
"Cor Ligthert [MVP]" wrote:

> Keith,
>
> I don't know if it is the same as from Ken, I got the idea from not. It
> seems to be excelent (I only tried it once).
>
> http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2934&lngWId=10
>
> I hope this helps,
>
> Cor
>
>
>
Author
10 Jan 2006 10:34 AM
Carlos J. Quintero [VB MVP]
Hi Keith,

In .NET is easy to paint the combobox items yourself, so you can make a
multicolumn combobox with this code:

Friend Class MultiColumnComboBox
Inherits ComboBox

Private m_chFieldSeparator As Char

Friend Sub New()

      Me.DrawMode = DrawMode.OwnerDrawFixed
  m_chFieldSeparator = ","c

End Sub

Protected Overrides Sub OnDrawItem(ByVal e As
System.Windows.Forms.DrawItemEventArgs)

      Dim iXPos As Integer
  Dim iYPos As Integer
  Dim sText As String
  Dim objSizeF As SizeF
  Dim sTextPartArray() As String
  Dim iTextPartIndex As Integer
  Dim sTextPart As String
  Dim objBrush As Brush
  Dim iMaxLength As Integer

  If e.Index >= 0 Then

   e.DrawBackground()

   sText = Me.Items(e.Index).ToString

   sTextPartArray = sText.Split(m_chFieldSeparator)

   objBrush = New SolidBrush(e.ForeColor)

   iXPos = e.Bounds.X

   For iTextPartIndex = 0 To sTextPartArray.Length - 1

    sTextPart = sTextPartArray(iTextPartIndex)

    objSizeF = e.Graphics.MeasureString(sTextPart, e.Font)

    If iTextPartIndex > 0 Then
     iMaxLength = GetMaxLength(e.Graphics, e.Font, iTextPartIndex - 1,
m_chFieldSeparator)
    Else
     iMaxLength = 0
    End If

    iXPos += iMaxLength
    iYPos = e.Bounds.Y

    e.Graphics.DrawString(sTextPart, e.Font, objBrush, iXPos, iYPos)

   Next

   objBrush.Dispose()

   Select Case e.State

    Case DrawItemState.NoFocusRect

    Case Else
     e.DrawFocusRectangle()

   End Select

  Else
   MyBase.OnDrawItem(e)
  End If

End Sub

Private Function GetMaxLength(ByVal objGraphics As Graphics, ByVal objFont
As Font, ByVal iTextPartIndex As Integer, ByVal chSeparator As Char) As
Integer

  Dim iResult As Integer
  Dim iIndex As Integer
  Dim sTextPartArray() As String
  Dim sTextPart As String
  Dim sText As String
  Dim objSizeF As SizeF
  Dim iWidth As Integer

  ' By default
  iResult = 10

  For iIndex = 0 To Me.Items.Count - 1

   sText = Me.Items(iIndex).ToString

   sTextPartArray = sText.Split(chSeparator)

   If iTextPartIndex > sTextPartArray.Length - 1 Then
    ' Out of range
   Else

    sTextPart = sTextPartArray(iTextPartIndex)

    objSizeF = objGraphics.MeasureString(sTextPart, objFont)
    iWidth = CType(objSizeF.Width, Integer)

    If iWidth > iResult Then
     iResult = iWidth
    End If

   End If

  Next

  Return iResult

End Function

End Class

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio 2005, Visual Studio .NET,
VB6, VB5 and VBA
You can code, design and document much faster in VB.NET, C#, C++ or VJ#
Free resources for add-in developers:
http://www.mztools.com




Show quoteHide quote
"Keith G" <Kei***@discussions.microsoft.com> escribió en el mensaje
news:711CB42C-94F1-4EC5-A7C6-B5BCCEF47461@microsoft.com...
>I am using Visual Studio 2003.
> In the standard combobox control it would appear that only 1 column of
> data
> can be displayed in the list (as stipulated in the DisplayMember
> property).
> In VBA it was always possible to specify a column count and then set the
> width for each column (setting 0 width for columns not to be displayed).
> The
> Bound Column property was then specified to set the data to be retrieved
> from
> the control (the ValueMember property being the equivalent).
> I would like to display 2 columns of data in the list to make it easier
> for
> the user to select the correct data (ID and description). Once the
> selection
> is made then only the ID is shown in the control text.
> I can't see how to do this in VS 2003. Does anybody have any suggestions?
>
Author
10 Jan 2006 5:44 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Keith G" <Kei***@discussions.microsoft.com> schrieb:
> In the standard combobox control it would appear that only 1 column of
> data
> can be displayed in the list (as stipulated in the DisplayMember
> property).
> In VBA it was always possible to specify a column count and then set the
> width for each column (setting 0 width for columns not to be displayed).
> The
> Bound Column property was then specified to set the data to be retrieved
> from
> the control (the ValueMember property being the equivalent).
> I would like to display 2 columns of data in the list to make it easier
> for
> the user to select the correct data (ID and description).

Multi Column ComboBox
<URL:http://www.codeproject.com/cs/combobox/multicolumncombo.asp>

Multi-Column ComboBox
<URL:http://www.codeproject.com/vb/net/multicolumncombo.asp>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>