winforms - c# - adding listBox data using list<Object> -


i want populate listbox list of objects properties , , want know how define listbox display property within object text, , other name of methods invoked while listbox items clicked (selectindexchanged)

hope helps.

public partial class form1 : form     {         public form1()         {             initializecomponent();              //create listbox, given height/width , top/left             var lstbox = new listbox             {                 width = 300,                 height = 300,                 top = 10,                 left = 10             };              //add listbox form             this.controls.add(lstbox);              //create list of customclass             var listcustomclass = new list<customclass>();              //populate list values             (int = 0; < 50; i++)             {                 //create instanze of customclass                 var customclass = new customclass();                  //set properties of class                 customclass.name = "name " + i;                 customclass.description = "description " + i;                  if (i % 2 == 0)                     customclass.methodname = "callmebaby";                 else                     customclass.methodname = "callmebabywithparameter";                  customclass.randomproperty1 = "randomproperty1 " + i;                  //add newly created customclass list                 listcustomclass.add(customclass);             }              //set listbox display or value need             lstbox.displaymember = "description"; //name of property inside class customclass             lstbox.valuemember = "name"; //name of property inside class customclass              //set datasource             lstbox.datasource = listcustomclass;              //register selectedindexchanged event             lstbox.selectedindexchanged += lstbox_selectedindexchanged;         }          private void lstbox_selectedindexchanged(object sender, eventargs e)         {             //get listbox sender             var lstbox = (sender listbox);              if (lstbox != null)             {                 //safe cast selecteditem customclass full access public property class definition                 var customclass = lstbox.selecteditem customclass;                  if (customclass != null)                 {                     //do ever want object , properties                     var name = customclass.name;                     var desription = customclass.description;                     var methodname = customclass.methodname;                     var randomproperty1 = customclass.randomproperty1;                      //call method based on string within object                     if (methodname == "callmebaby")                         callmebaby();                     else if (methodname == "callmebabywithparameter")                         callmebaby(name);                 }             }         }          //declare methods being called         private void callmebaby(string value)         {             //access parameter ,             if (value == "helloworld!")             {                 //do something...             }         }          //parameterless method show possibilities...         private void callmebaby()         {             //do something...         }           //define public class         public class customclass         {             //random properties, can extended have ever need              public string name { get; set; }             public string description { get; set; }             public string methodname { get; set; }             public string randomproperty1 { get; set; }         }     } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -