Adding Text and Value to ComboBox

ComboBox text and value - C# , VB.Net

The following program demonstrates how to add Text and Value to an Item of a ComboBox without using any Binding DataSource.

Add combobox items with different text and value  in C# , VB.Net

In order to add Text and Value, here using a Dictionary Object to store text and values.

C#
Dictionary<string, string> comboSource = new Dictionary<string, string>(); comboSource.Add("1", "Sunday"); comboSource.Add("2", "Monday");
VB.Net
Dim comboSource As New Dictionary(Of String, String)() comboSource.Add("1", "Sunday") comboSource.Add("2", "Monday")

How To Add (Item and Value) Into ComboBox

After adding items to the Dictionary, next step is to set this Dictionary Object as a DataSource in the Combobox

C#
comboBox1.DataSource = new BindingSource(comboSource, null); comboBox1.DisplayMember = "Value"; comboBox1.ValueMember = "Key";
VB.Net
ComboBox1.DataSource = New BindingSource(comboSource, Nothing) ComboBox1.DisplayMember = "Value" ComboBox1.ValueMember = "Key"

How to add Text and Value in combobox

Now the first part is over, next is to retrieve these keys and values from the Combobox.

C#
string key = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Key; string value = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;
VB.Net
Dim key As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of String, String)).Key Dim value As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of String, String)).Value

Source Code | C#
using System; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Dictionary<string, string> comboSource = new Dictionary<string, string>(); comboSource.Add("1", "Sunday"); comboSource.Add("2", "Monday"); comboSource.Add("3", "Tuesday"); comboSource.Add("4", "Wednesday"); comboSource.Add("5", "Thursday"); comboSource.Add("6", "Friday"); comboSource.Add("7", "Saturday"); comboBox1.DataSource = new BindingSource(comboSource, null); comboBox1.DisplayMember = "Value"; comboBox1.ValueMember = "Key"; } private void button1_Click(object sender, EventArgs e) { string key = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Key; string value = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value; MessageBox.Show(key + " " + value); } } }

Source Code | Vb.Net
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim comboSource As New Dictionary(Of String, String)() comboSource.Add("1", "Sunday") comboSource.Add("2", "Monday") comboSource.Add("3", "Tuesday") comboSource.Add("4", "Wednesday") comboSource.Add("5", "Thursday") comboSource.Add("6", "Friday") comboSource.Add("7", "Saturday") ComboBox1.DataSource = New BindingSource(comboSource, Nothing) ComboBox1.DisplayMember = "Value" ComboBox1.ValueMember = "Key" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim key As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of String, String)).Key Dim value As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of String, String)).Value MessageBox.Show(key & " " & value) End Sub End Class

Autocomplete ComboBox

acomobobox key value c# vb.net

From the version Visual Studio 2005, some of the controls support Autocomplete feature including the ComboBox controls. From the following link you can see how to make .... Autocomplete ComboBox

ComboBox Control

Text and Value in combobox c# vb.net

A ComboBox displays a text box combined with a ListBox, which enables the user to select items from the list or enter a new value. You can learn more operations in ComboBox from the following link

C# ComboBox VB.Net ComboBox