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
"Keith G" <Ke****@discussions.microsoft.com> escribió en el mensaje
news:71**********************************@microsof t.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?