Skip to content
Fix Code Error

ComboBox: Adding Text and Value to an Item (no Binding Source)

March 13, 2021 by Code Error
Posted By: Anonymous

In C# WinApp, how can I add both Text and Value to the items of my ComboBox?
I did a search and usually the answers are using “Binding to a source”.. but in my case I do not have a binding source ready in my program…
How can I do something like this:

combo1.Item[1] = "DisplayText";
combo1.Item[1].Value = "useful Value"

Solution

You must create your own class type and override the ToString() method to return the text you want. Here is a simple example of a class you can use:

public class ComboboxItem
{
    public string Text { get; set; }
    public object Value { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

The following is a simple example of its usage:

private void Test()
{
    ComboboxItem item = new ComboboxItem();
    item.Text = "Item text1";
    item.Value = 12;

    comboBox1.Items.Add(item);

    comboBox1.SelectedIndex = 0;

    MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
}
Answered By: Anonymous

Related Articles

  • Is CSS Turing complete?
  • Combining items using XSLT Transform
  • sql query to find priority jobs
  • What are the undocumented features and limitations…
  • How to filter a RecyclerView with a SearchView
  • Binding a WPF ComboBox to a custom list
  • What is a NullReferenceException, and how do I fix it?
  • Form field border-radius is not working only on the…
  • Prevent invoking the softkeyboard after tapping a…
  • Getting selected value of a combobox
  • ComboBox.SelectedItem giving Null value
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Recursive compose element in Aurelia
  • In CSS Flexbox, why are there no "justify-items" and…
  • How to change the background of the textfield in a…
  • Vue.JS & Spring Boot - Redirect to homepage on 404
  • How do I merge two dictionaries in a single…
  • Pure JavaScript equivalent of jQuery's $.ready() -…
  • Centering in CSS Grid
  • Search code inside a Github project
  • ComboBox SelectedItem vs SelectedValue
  • Can't install via pip because of egg_info error
  • Ember.js + AddThis error "Uncaught TypeError: Cannot…
  • Javax.net.ssl.SSLHandshakeException:…
  • Binding ComboBox SelectedItem using MVVM
  • How to make all controls resize accordingly…
  • Сombobox closes when I try to click on the elements…
  • What is your most productive shortcut with Vim?
  • Java - Regex, nested recursion match
  • Accessing query values in Next.js
  • Identifying and solving…
  • How to "properly" create a custom object in JavaScript?
  • What is an optional value in Swift?
  • Smart way to truncate long strings
  • Playing HTML5 video on fullscreen in android webview
  • Vuetify change checkbox icon in v-select / v-combobox
  • How do I print my Java object without getting…
  • Creating a SearchView that looks like the material…
  • Vuetify Combobox Not Returning id instead of value
  • Ukkonen's suffix tree algorithm in plain English
  • ComboBox- SelectionChanged event has old value, not…
  • I need to extract working hour breaks out of a Time…
  • Select Tag Helper in ASP.NET Core MVC
  • Bounce effect in a ComboBox QML
  • C# - Fill a combo box with a DataTable
  • Logging best practices
  • I'm trying to use a stored procedure or function…
  • How to process the emitted event from the component…
  • How to use Servlets and Ajax?
  • How to combine the data from two different…
  • How to end C++ code
  • Active tab issue on page load HTML
  • Why does C++ code for testing the Collatz conjecture…
  • What does "Fatal error: Unexpectedly found nil while…
  • How does PHP 'foreach' actually work?
  • "Thinking in AngularJS" if I have a jQuery background?
  • How to add an icon in front of every VuetifyJS…
  • The definitive guide to form-based website authentication
  • Fix top buttons on scroll of list below
  • String concatenation with Groovy
  • jQuery Mobile: document ready vs. page events
  • Memcached vs. Redis?
  • creating triggers for After Insert, After Update and…
  • Usage of __slots__?
  • Best way to sort through components Vue JS
  • Item position in RecyclerView only changing when…
  • Vue JS - How do I get WebGL to render via Vue…
  • How to download and save an image in Android
  • Vue.js v-for loop from an object key and nested array
  • What is an IndexOutOfRangeException /…
  • ExpressJS How to structure an application?
  • Convert Java Date to UTC String
  • Vuetify combobox - How to disable typing in…
  • Android ListView headers
  • How can I find the product GUID of an installed MSI setup?
  • Polymer 1.0 'array-style' path accessors,…
  • Implementing SearchView in action bar
  • How to find out client ID of component for ajax…
  • Polymer 1.x: Observers
  • Polymer 1.0: Sorting iron-list
  • Write Datatable to a Excel file
  • vaadin combobox load wrong custom style
  • Set value to vaadin combobox(polymer)
  • I want to create a SQLite database like in the…
  • How can I manually compile a svelte component down…
  • Adding items to a JComboBox
  • C++ template,typename and operator
  • center 3 items on 2 lines
  • Postman gives 401 Unauthorized - Spring Boot & MYSQL
  • How to set Default Value in Ember js
  • What's the best way of scraping data from a website?
  • Getting the closest string match
  • How to most easily render both i18n text AND model…
  • How can I access and process nested objects, arrays or JSON?
  • Changing Icon Color behind ListTile in an…
  • Download a file with Android, and showing the…
  • What's the difference between eval, exec, and compile?
  • How do I create a search box like on…
  • Using StringWriter for XML Serialization
  • Filtering DataGridView without changing datasource

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to detect pressing Enter on keyboard using jQuery?

Next Post:

How to filter object array based on attributes?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error