Multiple columns in Combobox list

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
Keith G said:
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)
 
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
 
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.
 
Keith G said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top