C# - SPLessons

How add a item to combobox

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

How add a item to combobox

How add a item to combobox

 

Description :

Hi every body, today i would like to demonstrate how add a item to combobox in C#.

 

  1. A Combobox stores objects, we can store our own object by just overriding the ToString() method to generate the text the user will see.

 

 

 

[csharp]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ComboboxItem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

//Add Items to combobox control.
comboBox1.Items.Add(new ComboboxItem("SPLessons", "SP"));
comboBox1.Items.Add(new ComboboxItem("SPLessons1", "SP1"));

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

ComboboxItem comboBox1_selected_value = (ComboboxItem)comboBox1.SelectedItem;

//Get the Selected Value from Combobox.

label2.Text = comboBox1_selected_value.Value.ToString();

}

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

public override string ToString()
{
return Text;
}

public ComboboxItem(string text, string value)
{
Text = text; Value = value;
}

}
}
}

[/csharp]

Comments