472,111 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

Format Combobox Display Member

Hello Everyone,

I would like to format the Display Members of a combobox's datasource. Is
there a way to apply a format without subclassing the original datasource?
For example, given a list of decimal values, format them as currency values
with a leading currency symbol and 2 decimal places.

TIA

Class Test

Dim SelectionList As ArrayList
Dim cbo As ComboBox

Public Sub New()
SelectionList = New ArrayList
SelectionList.AddRange(New DataElements() {New DataElements(1D, 1), New
DataElements(1.15D, 2), New DataElements(1.3D, 3)})

cbo = New ComboBox
cbo.DataSource = SelectionList
cbo.DisplayMember = "Value"
cbo.ValueMember = "Index"

End Sub

Public Class DataElements
Private m_Value As Decimal
Private m_Index As Integer

Public Sub New(ByVal Value As Decimal, ByVal Index As Integer)
m_Value = Value
m_Index = Index
End Sub

Public ReadOnly Property Value() As Decimal
Get
Return m_Value
End Get
End Property

Public ReadOnly Property Index() As Integer
Get
Return m_Index
End Get
End Property

End Class

End Class
Nov 21 '05 #1
2 12354
Hi,

I usually make my combobox ownerdrawn when I have to format the
display members. Here is a link to making an ownerdrawn combobox

http://www.windowsformsdatagridhelp....7-ecb5b73cbcae

Ken
-------------------------------
"AMDRIT" <am****@hotmail.com> wrote in message
news:Oc*************@TK2MSFTNGP15.phx.gbl...
Hello Everyone,

I would like to format the Display Members of a combobox's datasource. Is
there a way to apply a format without subclassing the original datasource?
For example, given a list of decimal values, format them as currency
values with a leading currency symbol and 2 decimal places.

TIA

Class Test

Dim SelectionList As ArrayList
Dim cbo As ComboBox

Public Sub New()
SelectionList = New ArrayList
SelectionList.AddRange(New DataElements() {New DataElements(1D, 1), New
DataElements(1.15D, 2), New DataElements(1.3D, 3)})

cbo = New ComboBox
cbo.DataSource = SelectionList
cbo.DisplayMember = "Value"
cbo.ValueMember = "Index"

End Sub

Public Class DataElements
Private m_Value As Decimal
Private m_Index As Integer

Public Sub New(ByVal Value As Decimal, ByVal Index As Integer)
m_Value = Value
m_Index = Index
End Sub

Public ReadOnly Property Value() As Decimal
Get
Return m_Value
End Get
End Property

Public ReadOnly Property Index() As Integer
Get
Return m_Index
End Get
End Property

End Class

End Class

Nov 21 '05 #2
Ken,

Thank you very much. Your link was enough to get me to think in the correct
direction.

Private Sub cboComp_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles cboComp.DrawItem

' Formats the display value of a combobox as
' a currency value.
'
' Assumptions: All display values are numeric
'

'set the combo box drawmode = OwnerdrawVariable
If e.Index = -1 Then Exit Sub

Dim strDisplayValue As String

'Seems there are layers in everything these days.
e.DrawBackground()

'I am bound to an array of datarows, using cbo.datasource =
datatable.select("MyKey = SomeNumber")
'this handler will always treat the displayvalues as decimal and format
them as string currency
strDisplayValue = CType(CType(cboComp.DataSource,
DataRow())(e.Index)(cboComp.DisplayMember), Decimal).ToString("c")

'Draw the data
'ToDo: Figure out how to right align using drawstring().
e.Graphics.DrawString(strDisplayValue, cboComp.Font,
System.Drawing.Brushes.Black, New RectangleF(e.Bounds.X, e.Bounds.Y,
e.Bounds.Width, e.Bounds.Height))

'More layers
e.DrawFocusRectangle()

End Sub

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:O9**************@TK2MSFTNGP14.phx.gbl...
Hi,

I usually make my combobox ownerdrawn when I have to format the
display members. Here is a link to making an ownerdrawn combobox

http://www.windowsformsdatagridhelp....7-ecb5b73cbcae

Ken
-------------------------------
"AMDRIT" <am****@hotmail.com> wrote in message
news:Oc*************@TK2MSFTNGP15.phx.gbl...
Hello Everyone,

I would like to format the Display Members of a combobox's datasource.
Is there a way to apply a format without subclassing the original
datasource? For example, given a list of decimal values, format them as
currency values with a leading currency symbol and 2 decimal places.

TIA

Class Test

Dim SelectionList As ArrayList
Dim cbo As ComboBox

Public Sub New()
SelectionList = New ArrayList
SelectionList.AddRange(New DataElements() {New DataElements(1D, 1),
New DataElements(1.15D, 2), New DataElements(1.3D, 3)})

cbo = New ComboBox
cbo.DataSource = SelectionList
cbo.DisplayMember = "Value"
cbo.ValueMember = "Index"

End Sub

Public Class DataElements
Private m_Value As Decimal
Private m_Index As Integer

Public Sub New(ByVal Value As Decimal, ByVal Index As Integer)
m_Value = Value
m_Index = Index
End Sub

Public ReadOnly Property Value() As Decimal
Get
Return m_Value
End Get
End Property

Public ReadOnly Property Index() As Integer
Get
Return m_Index
End Get
End Property

End Class

End Class


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Omar | last post: by
2 posts views Thread by pei_world | last post: by
1 post views Thread by Stewart Lane | last post: by
1 post views Thread by Sam | last post: by
4 posts views Thread by c_shah | last post: by
2 posts views Thread by =?Utf-8?B?SmFtZXMgTWFydGlu?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.