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

首頁 > 開發 > 綜合 > 正文

Displaying ListView items - with class! by Rob Bir

2024-07-21 02:28:05
字體:
來源:轉載
供稿:網友
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!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴隆县| 闵行区| 泌阳县| 木兰县| 雷波县| 神农架林区| 宽城| 边坝县| 七台河市| 玉田县| 香港| 行唐县| 阿鲁科尔沁旗| 威信县| 阿拉善左旗| 盘锦市| 永济市| 晋宁县| 修水县| 涟水县| 富裕县| 苍梧县| 承德县| 泰和县| 米脂县| 分宜县| 新密市| 潼关县| 乌拉特后旗| 合川市| 盐边县| 庄浪县| 柘城县| 叶城县| 勐海县| 白山市| 梓潼县| 大邑县| 白水县| 大足县| 益阳市|