473,322 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 12485
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: bds | last post by:
I'm new to .net 2.0 and I'm trying to figure out the best way to create a form that has a couple of controls and one of the controls is a combo box with a data bound list of items from another...
3
by: google | last post by:
This is something I've done plenty of times in '97, but I can't seem to get it to work correctly in Access 2003. Say, for example, I have a form with an unbound combobox, the data source is a...
6
by: Omar | last post by:
When I try to databind my comboBox (specifically field "emplcode") to a filled dataset , the contents of the comboBox displays a bunch of "System.Data.DataRowView". I assume the amount of times...
2
by: pei_world | last post by:
I want to implement a key hit with enter to dropdown a combobox that is in the datagrid. in this case I need to override its original behaviours. I found some codes from the web. Does anyone know...
1
by: Stewart Lane | last post by:
Can the display member of a combobox who's datasource is a dataset generated from a SQl command in a data adpater BE SET to the concatenation of two fields, like fname+' '+lname? This is my SQL...
1
by: Sam | last post by:
Hello, Framework: 2.0 IDE: VBExpress Problem: I have a datagrid that contains a combobox type column. The grid is bound to a datatable. The comboboxes have a value member and a display...
6
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
4
by: c_shah | last post by:
I am a beginner and learning VB.net There are two wasy to bind a combobox or a listbox First approach is to assign a datasource and displaymember Second approach is to iterate through datarow...
2
by: =?Utf-8?B?SmFtZXMgTWFydGlu?= | last post by:
I have a table that I dragged onto a form from a dataset. I changed some of the fields to comboboxes and set the data source, display member (text), and value member (int) to another table within...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.