473,745 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.A ddRange(New DataElements() {New DataElements(1D , 1), New
DataElements(1. 15D, 2), New DataElements(1. 3D, 3)})

cbo = New ComboBox
cbo.DataSource = SelectionList
cbo.DisplayMemb er = "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 12509
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******** *****@TK2MSFTNG P15.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.A ddRange(New DataElements() {New DataElements(1D , 1), New
DataElements(1. 15D, 2), New DataElements(1. 3D, 3)})

cbo = New ComboBox
cbo.DataSource = SelectionList
cbo.DisplayMemb er = "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_DrawIte m(ByVal sender As Object, ByVal e As
System.Windows. Forms.DrawItemE ventArgs) Handles cboComp.DrawIte m

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

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

Dim strDisplayValue As String

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

'I am bound to an array of datarows, using cbo.datasource =
datatable.selec t("MyKey = SomeNumber")
'this handler will always treat the displayvalues as decimal and format
them as string currency
strDisplayValue = CType(CType(cbo Comp.DataSource ,
DataRow())(e.In dex)(cboComp.Di splayMember), Decimal).ToStri ng("c")

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

'More layers
e.DrawFocusRect angle()

End Sub

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:O9******** ******@TK2MSFTN GP14.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******** *****@TK2MSFTNG P15.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.A ddRange(New DataElements() {New DataElements(1D , 1),
New DataElements(1. 15D, 2), New DataElements(1. 3D, 3)})

cbo = New ComboBox
cbo.DataSource = SelectionList
cbo.DisplayMemb er = "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
9595
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 table. To understand the problem more, consider using the AdventureWorks2000 database. I want a simple form that shows FirstName, LastName, and Shift. Instead of the ShiftID, I want the shift name from the Shift table to be displayed. Here's...
3
4045
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 table of customers, first column is the key field, which is hidden with a 0" width, and the second column is the customer name. The SQL is a union query that also inserts Null in the first column, and "All" in the second column. That combobox gets...
6
7485
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 "System.Data.DataR..." is displayed inside the combobox is the amount of records in the dataset. On the other hand, if my query is "select emplcode from payemployee", the databind will work fine (but I don't want to limit the dataset to one field)....
2
4332
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 how to use this code? please help! http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20862953.html
1
1582
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 command: SELECT custnumb, fname, lname FROM custmstr ORDER BY lname, fname I've also tried: SELECT custnumb, fname+' '+lname as flname FROM custmstr ORDER BY lname, fname (and trying to set the display member to flname - but it wasn't
1
1735
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 member.
6
2882
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 and the coresponding 'Id' from the lookup table is to be inserted into the field of the new record. I have two simple tables. "tblPerson" is the data table. The lookup
4
35302
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 collection This works With ComboPaymentType .DataSource = dsComboItems.Tables("PaymentType") .DisplayMember = "PaymentType"
2
2576
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 the same dataset. I can start the form and cycle through existing records. If I click on one of the comboboxes it will display the text values but then I can no longer click on any other field. I can continue to click on the same combobox and the...
0
8970
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8795
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9486
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9344
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9277
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9214
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6057
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4572
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4827
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.