国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Displaying ListView items - with class! by Rob Bi

2024-07-21 02:16:48
字體:
來源:轉載
供稿:網友
displaying listview items - with class!  by rob birdwell
i don't know about you, but i'm very fond of the listview control - it's a very useful control to have around. microsoft's .net common language runtime (clr) provides much of the functionality you need to display data in a listview. however, you're still left will some work to do. loading the listview and getting the data can be an arduous task. in fact, your code can get pretty messy unless you think things through a bit. in order to display complex class data, you should consider a way to associate your class data with each listitem in the listview.

let's say you have a class as follows:

class person                                                                               
   {                                                                                       
   public string name;                                                                     
   public string rank;                                                                     
   public string serialnumber;                                                             
                                                                                           
   public person (string n, string r, string sn)                                           
      {                                                                                    
      name = n;                                                                            
      rank = r;                                                                            
      serialnumber = sn;  
      }                                                                            
   }

your form requires you to display the name, rank, and serialnumber members of each person object in a listview. we will now create a sub-class of the listitem base class: "personlistitem" class. this doesn't involve much code, and the pay-off will be cleaner code and greater flexibility if your display and/or design changes:

// provide easy access to the person class within a listview                              
class personlistitem : listitem                                                           
   {                                                                                      
   private person m_person;                                                               
                                                                                          
   public personlistitem (person person) : base()                                         
      {                                                                                   
      m_person = person;                                                                  
                                                                                          
      // assign name to the base listitem text property -                                 
      // this will cause name to display by default:                                      
      text = m_person.name;                                                               
                                                                                          
      // map the other class data to sub-items of the listitem                            
      // these aren't necessarily displayed...                                            
      setsubitem(0, m_person.rank);                                                        
      setsubitem(1, m_person.serialnumber);                                                
      }                                                                                   
                                                                                          
   // property for m_person access                                                        
   public person persondata                                                               
      {                                                                                   
      get { return m_person; }                                                            
      set { m_person = value; }                                                           
      }                                                                                   
   }                                                                                      


now all that remains is to add a listview control to your form called personlistview. we can optionally add three columns to the listview: name, rank and serial number. if you add the columns, be sure to set the view property of personlistview to "report." in the form code, we'll add the personlistitem objects via a simple helper function, called from the form's constructor, as follows:

private void initpersonlistview()                                                         
   {                                                                                      
                                                                                          
   // load our personlistview with our important people:                                  
                                                                                          
   personlistview.insertitem(personlistview.listitems.count,                              
  new personlistitem(new person("rob birdwell", "private", "123456")));                   
                                                                                          
   personlistview.insertitem(personlistview.listitems.count,                              
  new personlistitem(new person("bill gates", "chairman & csa", "987654")));              
                                                                                          
   personlistview.insertitem(personlistview.listitems.count,                              
   new personlistitem(new person("george bush", "commander-in chief", "9999")));          
   }                                                                                      



the benefits of having a personlistitem sub-class should be obvious - when a user clicks on an item, we have access to all the class data - not just the listitem.text property or sub-item data! here's a sample event handler for our personlistview's selectedindexchanged event:

protected void personlistview_selectedindexchanged (object sender, system.eventargs e)    
   {                                                                                      
   if (personlistview.listitems.count == 0)                                               
      return; // nothing to do!                                                           
                                                                                          
   // get the personlistitem object associated with the selection                         
   personlistitem li = (personlistitem)getcurrentselectedlistitem(personlistview);        
   person p = li.persondata;                                                         
   messagebox.show( " name: " + p.name +                                                  
                    " rank: " + p.rank +                                                  
                    " serial number: " p.serialnumber);                                   
   }                                                                                      
                                                                                          
// here's a generic helper function for getting a listview selection:                     
private listitem getcurrentselectedlistitem(listview lv)                                  
   {                                                                                      
                                                                                          
   if (lv.selecteditems == null || lv.selecteditems.count == 0)                           
      return null;                                                                        
                                                                                          
return lv.selecteditems[0];                                                              
   }                                                                                      


if the personlistview is defined with no columns, only the person.name member will be displayed since it was mapped to the personlistitem.text property in the constructor.

and that's what i call displaying listview items - with class!



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 佛坪县| 和龙市| 昔阳县| 榆社县| 长岛县| 旬阳县| 竹山县| 商水县| 山阳县| 平阳县| 永寿县| 晋江市| 乌拉特前旗| 屯留县| 罗田县| 金秀| 安达市| 曲周县| 庄河市| 都昌县| 彰化县| 望江县| 固安县| 洛浦县| 额济纳旗| 新化县| 修水县| 云安县| 获嘉县| 宁夏| 永顺县| 繁昌县| 阜阳市| 丹江口市| 安顺市| 鹤岗市| 五莲县| 盐津县| 兰州市| 同仁县| 陇西县|