Format Combobox Display Member

A

AMDRIT

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
 
A

AMDRIT

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
 

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

Top