By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 1,871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

Format Combobox Display Member

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.