Monday, September 27, 2010

Setting Objects in ComboBox and retrieving Object from ComboBox in C#


Let me demonstrate how to set your object in combo box, in my case for demonstration I have created a simple Class with the name ListValue. Then created a simple C# form having a ComboBox with the name “cmbListValues”, buttons with the name “button1” and text “Set Value”, “button2” with text “Get Value” and I have a label having “lblValue”.

My ListValue class looks like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListTestApplication
{
    class ListValue
    {
        private int id;
        private String value;
        public ListValue(int id_, String value_)
        {
            id = id_;
            value = value_;
        }
        public int ID { set { id = ID; } get { return id; } }
        public String Value
                { set { value = Value; } get { return value; } }
        public override string ToString()
        {
            return value;
        }
        public override bool Equals(object obj)
        {
            if (obj is ListValue)
            {
                ListValue l = obj as ListValue;
                if(l.id.Equals(id)) return true;
            }
            return false;
        }
    }
}


I have two variables in my ListValue class, a constructor with 2 arguments to set values for “id” and “value”, I am having getter and setter for these variables.

I have also overridden ToString method, for displaying my meaningful value in comboBox.

I have also overridden Equals method, it is important to override equals method for preselecting any value in combo.

In form load function I have
        private void Form1_Load(object sender, EventArgs e)
        {
            cmbListValues.Items.Add(new ListValue(1, "Value1"));
            cmbListValues.Items.Add(new ListValue(2, "Value2"));
            cmbListValues.Items.Add(new ListValue(3, "Value3"));
            cmbListValues.Items.Add(new ListValue(4, "Value4"));
        }

Button1 click, I have click event as follows

        private void button1_Click(object sender, EventArgs e)
        {
            ListValue v = new ListValue(3,null);
            cmbListValues.SelectedItem = v;
        }

When click on button “Set Value” the method will set value “Value3” in combo, it is very useful to set value in case of updating any record and you want to set previous selected combo, for example you are going to update an employee and in update screen employee’s country should be preselected.

Button2 click, I have button click event as follows

       private void button2_Click(object sender, EventArgs e)
        {
            ListValue v = cmbListValues.SelectedItem as ListValue;
            if( v != null )
            lblValue.Text = v.Value;
        }


The above method demonstrates how to retrieve the value from selected combobox. You can retrieve all the
information of selected object.

All to gather forms code looks like

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ListTestApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            cmbListValues.Items.Add(new ListValue(1, "Value1"));
            cmbListValues.Items.Add(new ListValue(2, "Value2"));
            cmbListValues.Items.Add(new ListValue(3, "Value3"));
            cmbListValues.Items.Add(new ListValue(4, "Value4"));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ListValue v = new ListValue(3,null);
            cmbListValues.SelectedItem = v;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            ListValue v = cmbListValues.SelectedItem as ListValue;
            if( v != null )
            lblValue.Text = v.Value;
        }
    }
}