As I have shifted toward the desktop application using C# 2.0 now I have to learn new things which I have not leaned before. So here is my first post related to the desktop applications and in this post I will discuss the list view control which is mostly used to display records like the micro soft excel sheet display records.. And it is very interested control as well. So let us start with our example. In this example I have two list view control in the form, The list view control which is on the left side is named as lvwFirstListView and the list view control on the right side is named lvwSecondListView. And also two button one which contain two greater then sign (>>) is used to assign value from the first list view control to the second list view control and the second button which contain two less then sign (<< ) is used to assign values from second list view control to the first list view control if there is any check list view item.
When you drag the list view control the it contain the default properties and one of the important property regarding the display of the record is the view property of the list view control. Here are the values of the enum constant of the view property of the list view control
- LargeIcon:Each item appears as a full-sized icon with a label below it.
- DetailsEach item appears on a separate line with further information about each item arranged in columns. The left-most column contains a small icon and label, and subsequent columns contain sub items as specified by the application. A column displays a header which can display a caption for the column. The user can resize each column at run time.
- SmallIcon:Each item appears as a small icon with a label to its right.
- List:Each item appears as a small icon with a label to its right. Items are arranged in columns with no column headers.
- Tile: Each item appears as a full-sized icon with the item label and subitem information to the right of it. The subitem information that appears is specified by the application. This view is available only on Windows XP and the Windows Server 2003 family. On earlier operating systems, this value is ignored and the ListView control displays in the LargeIcon view.
For my example I have choose detail view of both the list view control. You can change it to what ever your requirement is. Next property which I have set is the CheckBox property.The CheckBoxes property allows you to display a check box next to each item in the list. This enables your application to display a list of items (and subitems if the View property is set to View.Details) that the user can select by clicking the check box. The CheckBoxes property offers a way to select multiple items in the ListView control without using the CTRL key. Depending on your application, using check boxes to select items rather than the standard multiple selection method may be easier for the user. Even if the MultiSelect property of the ListView control is set to false, you can still display checkboxes and provide multiple selection capabilities to the user. This feature can be useful if you do not want multiple items to be selected yet still want to allow the user to choose multiple items from the list to perform an operation within your application.
Next property which I have set is the sorting property of both the list view control.The Sorting property allows you to specify whether or not items are sorted in the ListView control. By default, no sorting is performed. When the Sorting property is set to Ascending or Descending, the items in the ListView are sorted automatically in ascending alphabetical order (when the property is set to Ascending) or descending alphabetical order (when the property is set to Descending). You can use this property to automatically sort items that are displayed in your ListView control to make it easier for users to find items when a large number of items are available. I have set the sorting property of the first list view control to Ascending and for second list view control Descending. Here are the possible value for the sorting property with their description.
|The items are not sorted.|
|The items are sorted in descending order.|
Here is the code which is used to populate the first list view control. Here I have declare the ListViewItem object to add the items in the list view control.The ListView control is similar to a ListBox in that it displays a list of items. The main difference is that the ListView control provides a number of different ways items can be viewed by the user. The ListViewItem class defines the appearance, behavior, and data associated with an item that is displayed in the ListView control. ListViewItem objects can be displayed in the ListView control in one of four different views. Items can be displayed as large or small icons or as small icons in a vertical list. Items can also have subitems that contain information that is related to the parent item. The fourth view style, details view, allows you to display the item and its subitems in a grid with column headers that can be used to identify the information being displayed in a subitem. In the code below I have used the used the listViewItem object and passed the value of the item to the constrctor, and then added the next sub item in the item list by using the SubItems property of th listViewItem object. In this case the value passed to the constructor is placed at column one and the value added using the SubItem.Add will be place at column two and so on if you have more item to display then you can add using the subItem.Add. This piece of code is used in the for loop to add the 18 item in the first list.
No the code which is used to add remove item from one list to another list. Here is the image of the code which you can find in the source code of this example. Here you can see the first for loop is used to assign the item from the first list view control to the second list view control. Here I have used the CheckedIndices property of the list view control, This property is only useful when the CheckBoxes property of the ListView control is set to true. The CheckedIndices property returns a collection containing the index positions in the ListView.ListViewItemCollection of all items that are checked in the control.
The Second for loop which is used to remove the item from the source list view control as in the above for loop the checked items are moved to the destination list view control. So here I have loop from the maximum checked index to the lower, as if I go start the loop from the lower to maximum index then by removing the first index item, the index of the remaining check item are changed so I go from the maximum checked index to the minimum. And I have used the RemoveAt function of the items collection which take the index of the item which is used to be removed.You can download the source code from here.
All and any comments / bugs / suggestions are welcomed!
All and any comments / bugs / suggestions are welcomed!