Custom Search
Logiclabz

Use Key Value pair in .Net ComboBox

  

//Add a public class for key value pair concept

public class KeyValuePair
    {
 
        public object Key; 
        public string Value
     
        public KeyValuePair(object NewValue,string NewDescription)
        {
             Key = NewValue
            Value = NewDescription
        }
     
        public override string ToString()
        {
            return Value
        }
    }


//add item to combo box using keyvalue pair class

cmbTopSites.Items.Add(new KeyValuePair("100","Top 100 sites";))

//key and values of the selected item can be accessed as below

KeyValuePair maxPages = (KeyValuePair)cmbTopSites.SelectedItem
string skey = maxPages.Key.ToString()
string svalue = maxPages.value.ToString()



  


Leave a reply


Comments

  • Freddy Nieto says:
    Apr 06, 10

    Si... excelente... justo lo que necesitaba... funciono a la perfección...

  • Erick says:
    May 16, 10

    Compañero esto resuelve muchos de mis problemas.... GRAAACIAASSS...

  • Byron says:
    May 27, 10

    Una vez hecho esto como hago para setear el combo mediante el Key creado en esa clase, es decir necesito que mi combo me muestre el item segun yo le asigne un valor Key

  • niluniverse says:
    Jun 06, 10

    Thanks ! We are working in a team known as "World" Regards Niluniverse

  • Alisha says:
    Jul 18, 11

    Thank you so much. This saved a ton of time for me.

  • Sergio says:
    Oct 31, 11

    EXELENTE!!! muchas gracias. Muy bueno.



Do you like this post?