When an item is selected, by default, its values will be displayed as comma separated items. The format with which the selected item's values are displayed is defined by the DisplayFormat property. The default display format is the name of each column, preceded and terminated with a percent (%), and separated by a comma (,).
For example, if the combobox contains 4 columns named "Country", "Population", "City", and "AverageTemp", the display format will be "%Country%, %Population%, %City%, %AverageTemp%". When an item is selected, the display format will be resolved to the values of the correspond columns. For example: "Germany, 83000000, Berlin, 25.1 ºc".
To change the format with which the selected item's text is displayed, the DisplayFormat property can be set to the desired format. For example, in the code below, the DisplayFormat property was changed to display only some of the selected item's values and display them as phrase.
VB.NET |
Copy Code |
---|---|
Dim combo As New WinComboBox() combo.Columns.Add( New ColumnInfo( "Country", GetType( String ) ) ) combo.Columns.Add( New ColumnInfo( "Population", GetType( Integer ) ) ) combo.Columns.Add( New ColumnInfo( "City", GetType( String ) ) ) combo.Columns.Add( New ColumnInfo( "AverageTemp", GetType( String ) ) ) combo.Items.Add( "Canada", 31500000, "Ottawa", "12.2 ºc" ) combo.Items.Add( "Switzerland", 7300000, "Bern", "23.3 ºc" ) combo.Items.Add( "France", 59500000, "Paris", "27.3 ºc" ) combo.Items.Add( "USA", 278000000, "Washington", "14.1 ºc" ) combo.Items.Add( "UK", 59700000, "London", "23.7 ºc" ) combo.Items.Add( "Belgium", 10300000, "Brussels", "21.8 ºc" ) combo.Items.Add( "Italy", 57700000, "Rome", "29.6 ºc" ) combo.Items.Add( "Spain", 40000000, "Madrid", "31.8 ºc" ) combo.Items.Add( "Germany", 83000000, "Berlin", "25.1 ºc" ) combo.Items.Add( "Japan", 126800000, "Tokyo", "17.2 ºc" ) combo.DisplayFormat = "%Country% has a population of %Population%" combo.Location = New Point( 10 , 10 ) Me.Controls.Add( combo ) |
C# |
Copy Code |
---|---|
WinComboBox combo = new WinComboBox(); combo.Columns.Add( new ColumnInfo( "Country", typeof( string ) ) ); combo.Columns.Add( new ColumnInfo( "Population", typeof( int ) ) ); combo.Columns.Add( new ColumnInfo( "City", typeof( string ) ) ); combo.Columns.Add( new ColumnInfo( "AverageTemp", typeof( string ) ) ); combo.Items.Add( "Canada", 31500000, "Ottawa", "12.2 ºc" ) ; combo.Items.Add( "Switzerland", 7300000, "Bern", "23.3 ºc" ); combo.Items.Add( "France", 59500000, "Paris", "27.3 ºc" ); combo.Items.Add( "USA", 278000000, "Washington", "14.1 ºc" ); combo.Items.Add( "UK", 59700000, "London", "23.7 ºc" ); combo.Items.Add( "Belgium", 10300000, "Brussels", "21.8 ºc" ); combo.Items.Add( "Italy", 57700000, "Rome", "29.6 ºc" ); combo.Items.Add( "Spain", 40000000, "Madrid", "31.8 ºc" ); combo.Items.Add( "Germany", 83000000, "Berlin", "25.1 ºc" ); combo.Items.Add( "Japan", 126800000, "Tokyo", "17.2 ºc" ); combo.DisplayFormat = "%Country% has a population of %Population%" ; combo.Location = new Point( 10 , 10 ); this.Controls.Add( combo ); |
If one of the variables used in the DisplayFormat is not resolvable, it will remain as is. For example, if "%Cntry% has a population of %Population%" is assigned to the DisplayFormat property (notice the spelling mistake in the first variable: "Cntry" rather than "Country"), when the selected item's text/value is displayed, the result will be: "%Cntry% has a population of 83000000".
In the case where the ComboBoxEditor is created using the constructor that accepts a DisplayFormat, only the column names specified by displayFormat and the valueMember parameters will be created in the dropdown. For example, setting displayFormat to "%FirstName% %LastName%" will result in only the "FirstName" and "LastName" columns being created in the dropdown and, for example, "Naomi Robitaille" when the item is selected. The column specified by valueMember will also be created; however, it will not visible unless it is also part of the displayFormat.