CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2001
    Posts
    2,455

    Subclass ListViewItem

    I want to save a FileInfo object to my list view items.

    Code:
    class ListViewItemEx : ListViewItem
        {
            private FileInfo _info;
            public FileInfo Info
            {
                get { return _info; }
                set { _info = value; }
            }
    
            public ListViewItemEx(FileInfo fi)
            {
                _info = fi;
            }
         }
    The list view is in details view. The items are added but the item text is not displayed. What to I have to do to get the list view to show the FileInfo::Name property.

    Any suggestions?

    Mike B

  2. #2
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Subclass ListViewItem

    Try this:

    Code:
    public override string ToString()
    {
    
    return _info;
    
    }
    Touraj Ebrahimi [toraj_e] [at] [yahoo] [dot] [com]

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Subclass ListViewItem

    Why not just add an ordinary ListViewItem and set the text on it ?

    You can set the Tag property to your file info if you need to go from selected item to FileInfo e.g.

    Code:
    void AddFileInfo(FileInfo fileInfo)
    {
       ListViewItem newItem = _listView.Items.Add(fileInfo.Name);
       newItem.Tag = fileInfo;
    }
    
    FileInfo GetFileInfo(ListViewItem item)
    {
        return item.Tag as FileInfo;
    }
    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Subclass ListViewItem

    It's too bad the WinForms ListView doesn't have a DataSource member to support data binding. Here's an example of how slick this is for a listbox.

    Code:
    public partial class Form1 : Form
    {
    	public Form1( )
    	{
    		InitializeComponent( );
    
    		DirectoryInfo directoryInfo = new DirectoryInfo( "C:\\" );
    
    		foreach( FileInfo fileInfo in directoryInfo.GetFiles( ) )
    		{
    			_fileInfoList.Add( fileInfo );
    		}
    
    		this.listBox1.DataSource = _fileInfoList;
    		this.listBox1.DisplayMember = "Name";
    	}
    
    	private void listBox1_MouseDoubleClick( object sender, MouseEventArgs e )
    	{
    		FileInfo fileInfo = listBox1.SelectedItem as FileInfo;
    
    		if( null == fileInfo ) return;
    
    		MessageBox.Show( String.Format( "Name:\t\t'{0}'\nFullName:\t\t'{1}'\nFileSize:\t\t{2}"
    								, fileInfo.Name
    								, fileInfo.FullName
    								, fileInfo.Length ) );
    	}
    
    	private BindingList<FileInfo> _fileInfoList = new BindingList<FileInfo>( );
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured