C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Key-Value Functionality in Combo Box


Posted Date: 10 Jul 2008    Resource Type: Articles    Category: .NET Framework
Author: RohitMember Level: Gold    
Rating: Points: 15



Key-Value Functionality in Combo Box




Combo box control is a control which displays a list of items. It combines the features of a text box and a list box. For the item which is selected in the combo box, we can get the selected index, which is in order of 0, 1, 2..

Today, we will learn how we can assign a key value pair to a combo box. The value will be shown to the end user and on the basis of the value, you can get the key.

E.g.: Consider the following key, value pairs:

Key: 1, Value: Rohit
Key: 2, Value: Suraj

Now, the combo box will show the values to the end user as Rohit, Suraj and for the selected item, you can get the key as either 1 or 2.

It is a simple three step procedure and am explaining it with some code samples.

Step 1: Create a new public class, say, KeyValuePair as



public class KeyValuePair
{
public object m_objKey;
public string m_strValue;

public KeyValuePair(object NewKey,string NewValue)
{
m_objKey = NewKey;
m_strValue = NewValue;
}

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



Step 2: From your Main class, add the items to combo box as



comboBox1.Items.Add(new KeyValuePair("1", "Rohit"));
comboBox1.Items.Add(new KeyValuePair("2", "Suraj"));
comboBox1.Items.Add(new KeyValuePair("3", "Sandeep"));
comboBox1.Items.Add(new KeyValuePair("4", "Jagdeep"));
comboBox1.SelectedIndex = 0;



Step 3: Now you can fetch the key-value pair as



KeyValuePair objKeyValuePair = (KeyValuePair)comboBox1.SelectedItem;
string strkey = objKeyValuePair.m_objKey.ToString();
string strvalue = objKeyValuePair.m_strValue.ToString();
MessageBox.Show("Key: " + strkey + " || Value: " + strvalue);



That’s it. Put the code written in step 3 on some button click event, say button1Click_event. Now, whenever you will click the button, a message box will come that show you the Key and Value of the item which is currenlt selected in the combo box.

If you find any kind of issue/query, please do let me know.


Haapy coding
-Rohit






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Key-value functionality of a combo box  .  Key-Value Functionality in Combo Box  .  Combo Box's key-value functionality  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Reflection in C#
Previous Resource: Enumerations in C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use