Click to See Complete Forum and Search --> : Subclass ListViewItem
MikeB
August 21st, 2008, 12:16 PM
I want to save a FileInfo object to my list view items.
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
toraj58
August 21st, 2008, 02:04 PM
Try this:
public override string ToString()
{
return _info;
}
Touraj Ebrahimi [toraj_e] [at] [yahoo] [dot] [com]
darwen
August 21st, 2008, 04:47 PM
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.
void AddFileInfo(FileInfo fileInfo)
{
ListViewItem newItem = _listView.Items.Add(fileInfo.Name);
newItem.Tag = fileInfo;
}
FileInfo GetFileInfo(ListViewItem item)
{
return item.Tag as FileInfo;
}
Darwen.
Arjay
August 21st, 2008, 05:56 PM
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.
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>( );
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.