欢迎光临
我们一直在努力

Displaying ListView items – with class!-.NET教程,Windows开发

建站超值云服务器,限时71元/月

i dont know about you, but im very fond of the listview control – its a very useful control to have around. microsofts .net framework provides much of the functionality you need to display data in a listview. however, youre 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.
for example, consider the following class, which represents data you might want to relate to a particular listview control:

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 doesnt 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 arent 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, well add the personlistitem objects via a simple helper function, called from the forms 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! heres a sample event handler for our personlistviews 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);                                   
   }                                                                                      
                                                                                          
// heres 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 thats what i call displaying listview items – with class!
 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Displaying ListView items – with class!-.NET教程,Windows开发
分享到: 更多 (0)

相关推荐

  • 暂无文章