468,771 Members | 1,859 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

Multiple columns in Combobox list

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?

Jan 9 '06 #1
5 29259
"Keith G" <Ke****@discussions.microsoft.com> wrote in message
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?


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

This page....

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

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

vbAccelerator IconComboBox Control
http://www.vbaccelerator.com/home/NE...ox/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..
Jan 9 '06 #2
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...2934&lngWId=10

I hope this helps,

Cor
Jan 10 '06 #3
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?

Jan 10 '06 #4
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.

"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...2934&lngWId=10

I hope this helps,

Cor

Jan 10 '06 #5
"Keith G" <Ke****@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/>

Jan 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Therese A. Sorna | last post: by
1 post views Thread by TheMadHatter | last post: by
1 post views Thread by mshenoud | last post: by
6 posts views Thread by =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.