The ListView Control
The list from which you select files to open in the standard dialog boxes in Windows is a ListView control Everything you can do to the view in the standard list view dialog (large icons, details view, and so on).
The list view is usually used to present data where the user is allowed some control over the detail and style of the presentation. It is possible to display the data contained in the control as columns and rows much like in a grid, as a single column or in with varying icon representations. The most commonly used list view is like the one seen above which is used to navigate the folders on a computer.
ListView Methods
ListView Events
ListViewItem
Write code on Form Loading for holding folders like below-
Code for Creating Header and ListView in ListView Control.
Code for ListView
Code for Listview Control for activating items
Code for making small icons view. Write below code on radio button named small icon
Code for making large icons view. Write below code on radio button named large icon
The list from which you select files to open in the standard dialog boxes in Windows is a ListView control Everything you can do to the view in the standard list view dialog (large icons, details view, and so on).
The list view is usually used to present data where the user is allowed some control over the detail and style of the presentation. It is possible to display the data contained in the control as columns and rows much like in a grid, as a single column or in with varying icon representations. The most commonly used list view is like the one seen above which is used to navigate the folders on a computer.
ListView Control Property
Name | Availability | Description |
Activation | Read/Write | By using this property, you can control how a user activates an item in the list view. You should not change the default setting unless you have a good reason for doing so, because you will be altering a setting that the user have set for his or her entire system. The possible values are: Standard: This setting is that which the user has chosen for his or her machine. OneClick: Clicking an item activates it. TwoClick: Double-clicking an item activates it. |
Alignment | Read/Write | his property allows you to control how the items in the list view are aligned. The four possible values are: Default: If the user drags and drops an item it remains where he or she dropped it. Left: Items are aligned to the left edge of the ListView control. Top: Items are aligned to the top edge of the ListView control. SnapToGrid: The ListView control contains an invisible grid to which the items will snap. |
AllowColumn Reorder | Read/Write | If you set this property to true, you allow the user to change the order of the columns in a list view. If you do so, you should be sure that the routines that fill the list view are able to insert the items properly, even after the order of the columns is changed. |
AutoArrange | Read/Write | If you set this property to true, items will automatically arrange themselves according to the Alignment property. If the user drags an item to the center of the list view, and Alignment is Left, then the item will automatically jump to the left of the list view. This property is only meaningful if the View property is LargeIcon or SmallIcon. |
CheckBoxes | Read/Write | If you set this property to true, every item in the list view will have a CheckBox displayed to the left of it. This property is only meaningful if the View property is Details or List. |
CheckedIndices CheckedItems | Read-only | These two properties gives you access to a collection of indices and items, respectively, containing the checked items in the list. |
Columns | Read-only | A list view can contain columns. This property gives you access to the collection of columns through which you can add or remove columns. |
FocusedItem | Read-only | This property holds the item that has focus in the list view. If nothing is selected, it is null. |
FullRowSelect | Read/Write | When this property is true, and an item is clicked, the entire row in which the item resides will be highlighted. If it is false, only the item itself will be highlighted. |
GridLines | Read/Write | Setting this property to true will cause the list view to draw grid lines between rows and columns. This property is only meaningful when the View property is Details. |
HeaderStyle | Read/Write | You can control how the column headers are displayed. There are three styles: Clickable: The column header works like a button. NonClickable: The column headers do not respond to mouse clicks. None: The column headers are not displayed. |
HoverSelection | Read/Write | When this property is true, the user can select an item in the list view by hovering the mouse pointer over it. |
Items | Read-only | The collection of items in the list view. |
LabelEdit | Read/Write | When this property is true, the user can edit the content of the first column in a Details view. |
LabelWrap | Read/Write | If this property is true, labels will wrap over as many lines is needed to display all of the text. |
LargeImageList | Read/Write | This property holds the ImageList, which holds large images. These images can be used when the View property is LargeIcon. |
MultiSelect | Read/Write | Set this property to true to allow the user to select multiple items. |
Scrollable | Read/Write | Set this property to true to display scrollbars. |
SelectedIndices SelectedItems | Read-only | These two properties contain the collections that hold the indices and items that are selected, respectively. |
SmallImageList | Read/Write | When the View property is SmallIcon this property holds the ImageList that contain the images used. |
Sorting | Read/Write | You can allow the list view to sort the items it contains. There are three possible modes: Ascending Descending None |
StateImageList | Read/Write | The ImageList contains masks for images that are used as overlays on the LargeImageList and SmallImageList images to represent custom states. |
TopItem | Read-only | Returns the item at the top of the list view. |
View | Read/Write | A list view can display its items in four different modes: LargeIcon: All items are displayed with a large icon (32x32) and a label. SmallIcon: All items are displayed with a small icon (16x16) and a label. List: Only one column is displayed. That column can contain an icon and a label Details: Any number of columns can be displayed. Only the first column can contain an icon. |
Name | Description |
BeginUpdate | By calling this method, you tell the list view to stop drawing updates until EndUpdate is called. This is useful when you are inserting many items at once, because it stops the view from flickering and dramatically increases speed. |
Clear | Clears the list view completely. All items and columns are removed. |
EndUpdate | Call this method after calling BeginUpdate. When you call this method, the list view will draw all of its items. |
EnsureVisible | When you call this method, the list view will scroll itself to make the item with the index you specified visible. |
GetItemAt | Returns the item at position x, y in the list view. |
Name | Description |
AfterLabelEdit | This event occurs after a label have been edited |
BeforeLabelEdit | This event occurs before a user begins editing a label |
ColumnClick | This event occurs when a column is clicked |
ItemActivate | Occurs when an item is activated |
ListViewItem
The ListViewItem holds information such as text and the index of the icon to display. ListViewItems have a collection called SubItems that holds instances of another class, ListViewSubItem. These sub items are displayed if the ListView control is in Details mode.
ColumnHeader
To make a list view display column headers, you add instances of a class called ColumnHeader to the Columns collection of the ListView. ColumnHeaders provide a caption for the columns that can be displayed when the ListView is in Details mode.
The ImageList Control
The ImageList control provides a collection that can be used to store images that is used in other controls on your form. You can store images of any size in an image list, but within each control every image must be of the same size. In the case of the ListView, which means that you need two ImageList controls to be able to display both large and small images.
Example of Listview Control
For using Listview Control make your window form design view like following image. And change controls property according to your choice. Take 2 listimage Control name as Large_Image and small_image and add images in that collection property and set large_image size 32X32.
private System.Collections.Specialized.StringCollection folderCol;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
folderCol = new System.Collections.Specialized.StringCollection();
CreateHeadersAndFillListView();
PaintListView(@"D:\");
folderCol.Add(@"D:\");
this.lwFilesAndFolders.ItemActivate += new
System.EventHandler(this.lwFilesAndFolders_ItemActivate);
} |
private void CreateHeadersAndFillListView()
{
ColumnHeader colHead;
// First header
colHead = new ColumnHeader();
colHead.Text = "Filename";
this.lwFilesAndFolders.Columns.Add(colHead); // Insert the header
// Second header
colHead = new ColumnHeader();
colHead.Text = "Size";
this.lwFilesAndFolders.Columns.Add(colHead); // Insert the header
// Third header
colHead = new ColumnHeader();
colHead.Text = "Last accessed";
this.lwFilesAndFolders.Columns.Add(colHead); // Insert the header
} |
Code for ListView
private void PaintListView(string root)
{
try
{
// Two local variables that is used to create the items to insert
ListViewItem lvi;
ListViewItem.ListViewSubItem lvsi;
// If there's no root folder, we can't insert anything
if (root.CompareTo("") == 0)
return;
// Get information about the root folder.
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(root);
// Retrieve the files and folders from the root folder.
DirectoryInfo[] dirs = dir.GetDirectories(); // Folders
FileInfo[] files = dir.GetFiles(); // Files
// Clear the ListView. Note that we call the Clear method on the
// Items collection rather than on the ListView itself.
// The Clear method of the ListView remove everything, including column
// headers, and we only want to remove the items from the view.
this.lwFilesAndFolders.Items.Clear();
// Set the label with the current path
this.lblCurrentPath.Text = root;
// Lock the ListView for updates
this.lwFilesAndFolders.BeginUpdate();
// Loop through all folders in the root folder and insert them
foreach (System.IO.DirectoryInfo di in dirs)
{
// Create the main ListViewItem
lvi = new ListViewItem();
lvi.Text = di.Name; // Folder name
lvi.ImageIndex = 0; // The folder icon has index 0
lvi.Tag = di.FullName; // Set the tag to the qualified path of the
// folder
// Create the two ListViewSubItems.
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = ""; // Size - a folder has no size and so this column
// is empty
lvi.SubItems.Add(lvsi); // Add the sub item to the ListViewItem
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = di.LastAccessTime.ToString(); // Last accessed column
lvi.SubItems.Add(lvsi); // Add the sub item to the ListViewItem
// Add the ListViewItem to the Items collection of the ListView
this.lwFilesAndFolders.Items.Add(lvi);
}
// Loop through all the files in the root folder
foreach (System.IO.FileInfo fi in files)
{
// Create the main ListViewItem
lvi = new ListViewItem();
lvi.Text = fi.Name; // Filename
lvi.ImageIndex = 1; // The icon we use to represent a folder has
// index 1
lvi.Tag = fi.FullName; // Set the tag to the qualified path of the
// file
// Create the two sub items
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = fi.Length.ToString(); // Length of the file
lvi.SubItems.Add(lvsi); // Add to the SubItems collection
lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = fi.LastAccessTime.ToString(); // Last Accessed Column
lvi.SubItems.Add(lvsi); // Add to the SubItems collection
// Add the item to the Items collection of the ListView
this.lwFilesAndFolders.Items.Add(lvi);
}
// Unlock the ListView. The items that have been inserted will now
// be displayed
this.lwFilesAndFolders.EndUpdate();
}
catch (System.Exception err)
{
MessageBox.Show("Error: " + err.Message);
}
} |
private void lwFilesAndFolders_ItemActivate(object sender, EventArgs e)
{
// Cast the sender to a ListView and get the tag of the first selected
// item.
System.Windows.Forms.ListView lw = (System.Windows.Forms.ListView)sender;
string filename = lw.SelectedItems[0].Tag.ToString();
if (lw.SelectedItems[0].ImageIndex != 0)
{
try
{
// Attempt to run the file
System.Diagnostics.Process.Start(filename);
}
catch
{
// If the attempt fails we simply exit the method
return;
}
}
else
{
// Insert the items
PaintListView(filename);
folderCol.Add(filename);
}
} |
Code for making small icons view. Write below code on radio button named small icon
private void rdoSmallIcon_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.lwFilesAndFolders.View = View.List;
} |
private void rdoLargeIcon_CheckedChanged(object sender, EventArgs e)
{
RadioButton rdb = (RadioButton)sender;
if (rdb.Checked)
this.lwFilesAndFolders.View = View.LargeIcon;
} |
i follow all the steps but when i run the program, everything is fine except, there's no icon for the files. only titles. Help pls.
ReplyDeletePS, im running Visual Studio 2013 in win 8.1 64bit.
ReplyDelete