NET User Interfaces in Csharp Windows Forms and Custom Controls [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

NET User Interfaces in Csharp Windows Forms and Custom Controls [Electronic resources] - نسخه متنی

Matthew MacDonald

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید




































Advanced ListView Tricks



To unlock all the functionality of the ListView control, you need to delve deeper into the .NET classes that support it. Some of the highlights are described in Table 6-3.











Table 6-3: Advanced ListView Members






MemberDescription















Activation and HoverSelectionActivation determines how items in the ListView are highlighted. If you select OneClick, the mouse cursor becomes a hand icon when it hovers over an item. The HoverSelection property, when set to true, automatically selects an item when the user hovers over it. This formerly cutting-edge feature is now discouraged as being unintuitive (and somewhat "touchy").










AlignmentSets the side of the ListView that items are aligned against.










AllowColumnReorderWhen set to true, the user can drag column headers around to rearrange column order in Details view, without requiring any code.










AutoArrange and ArrangeIcons()In SmallIcon and LargeIcon view, this property determines whether icons automatically snap to a grid, or can be positioned anywhere by the user.










CheckBoxes, CheckedIndices, and CheckedItemsWhen CheckBoxes is true, every item will have a check box next to it. The state of the check box is reflected in the ListViewItem.Checked property of each item. You can also retrieve checked items directly (using CheckedItems) or their index values (CheckedIndices).










FullRowSelectWhen set to true, the entire row will be highlighted when you select an item in Details view, not just the first column. It's a useful setting for database applications that are using the ListView as a grid control.










GridLinesDisplays attractive column and row gridlines in Details view. Useful if you are displaying many rows of complex or hard to read information.










HeaderStyleAllows you to configure whether column headers respond to clicks (Clickable) or ignore them (Nonclickable).










LabelEditWhen set to true, ListViewItem text can be modified by the user or in code using the BeginEdit() method.










LabelWrapAllows the text label to wrap in one of the icon views.










SortingAllows you to specify an ascending or descending sort order, which considers the main text of the ListViewItem only (not any subitems).










BeginUpdate() and EndUpdate()Allows you to temporarily suspend the ListView drawing, so that you can add or modify several items at once.










EnsureVisible()Scrolls to make sure a specified ListViewItem is visible. You indicate the item by its zero-based row index.










GetItemAt()Retrieves the ListViewItem at the given X and Y coordinate. Useful for hit testing and drag-and-drop operations.










AfterLabelEdit, and BeforeLabelEdit eventsEvents that fire before and after a label is modified. Both events provide the index to the appropriate ListViewItem, and a property that allows you to cancel the edit.










ColumnClick eventOccurs when a user clicks a column. Could be used for programmatic sorting by column, but the current ListView class does not support it.










ItemCheck eventOccurs when the state of a checkbox next to an item is changed.
















If you decide to use the ListView as a grid control, you can use a few useful properties to fine-tune the display by adding gridlines and row selection (rather than single-column value selection):



listAuthors.GridLines = true;
listAuthors.FullRowSelect = true;


You probably also want to add the ability to sort and rearrange columns. If you only need to sort using the ListItem.Text property, you can make use of the Sorting property.



listAuthors.Sorting = SortOrder.Ascending;


If you want to provide column sorting in details view, life is a little more difficult. The ListView control has no intrinsic support for sorting by column. However, you can easily develop a custom IComparer sorting class to handle the task. This class has a simple responsibility: examine two ListView objects, and return a 1, 0, or −1 depending on how they compare.


The best option is to create an IComparer class that stores a column index as a public member variable, allowing it to provide sorting for any column. In addition, the example includes a Boolean member variable called Alphabetic that allows two types of sorting: numeric or letter-by-letter alphabetic. For simplicity's sake, this class doesn't use any type of error checking.



public class CompareListViewItems : IComparer
{
// This index identifies the column that is used for sorting
public readonly int Column;
// Is the sort alphabetic or number?
public readonly bool Alphabetic;
public CompareListViewItems(int columnIndex, bool alphabetic)
{
this.Column = columnIndex;
this.Alphabetic = alphabetic;
}
public int Compare(object x, object y)
{
// Convert the items that must be compared into ListViewItem objects.
string listX = ((ListViewItem)x).SubItems[Column].Text;
string listY = ((ListViewItem)y).SubItems[Column].Text;
// Sort using the specified column and specified sorting type.
if (Alphabetic)
{
return listX.CompareTo(listY);
}
else
{
if (int.Parse(listX) > int.Parse(listY))
{
return 1;
}
else if (int.Parse(listX) == int.Parse(listY))
{
return 0;
}
else
{
return -1;
}
}
}


Now, you can easily create a ListView that re-sorts itself as a column header when it is clicked by handling the ColumnClicked event, generating a new CompareListViewItems object, and calling the ListView.Sort() method:



private void listAuthors_ColumnClick(object sender, ColumnClickEventArgs e)
{
// Specify an alphabetic sort based on the column that was clicked.
listAuthors.ListViewItemSorter = new CompareListViewItems(e.Column, true);
// Perform the sort.
listAuthors.Sort();
}


Another interesting trick is column reordering. This allows the user to rearrange columns by dragging the column header. This technique takes place automatically, if you set the AllowColumnReorder property to true. Unfortunately, there is no easy way to save these view settings and apply them later. To manage this type of advanced data display, you may want to consider the DataGrid control described in Chapter 9.



Label Editing



The ListView includes an automatic label-editing feature that you have probably already witnessed in Windows Explorer. You trigger the label editing by clicking a selected item once. This automatic editing is confusing to many new users. If you use it, you should also provide another way for the user to edit the corresponding information.


To enable label editing, set the LabelEdit property to true. You can programmatically start label editing for a node using the node's BeginEdit() method.



private void cmdStartEdit_Click(object sender, System.EventArgs e)
{
// The user clicked a dedicated "Edit" button.
// Put the label of the first selected item into edit mode.
if (list.SelectedItems.Count > 0)
{
list.SelectedItems[0].BeginEdit();
}
// (You might also want to disable other controls until the user completes
// the edit and the AfterLabelEdit event fires.)
}


In addition, you can prevent certain nodes from being edited by handling the BeforeLabelEdit event and setting the Cancel flag to true. You can also fix up any invalid changes by reacting to the AfterLabelEdit event.



Tip


If you want to use the BeginEdit() method but prevent users from modifying the label by clicking it, you must set the LabelEdit property to true. To prevent users from editing labels directly set a special form-level property (like AllowEdit) before you use the BeginEdit() method, and check for this property in the Before-LabelEdit event. If it has not been set and the user has started the edit, then you should cancel it.






Adding Information to a ListView



A typical application often needs to store information about display items that isn't rendered in the user interface. For example, you might want to keep track of unique identifier numbers that will allow you to look up a given item in a database, but you won't show this information to the end user, because it's of no use. Sometimes, programmers handle this in a control-specific way using hidden columns or other work arounds. However, a more generic and elegant approach is to find some way to bind the extra information to the control.


There are three ways that you can add additional information to a ListView control to represent custom data.





Assign a DataRow object to the Tag property of a ListViewItem. The advantage of this technique easily accommodates any type of data, and doesn't require modifications if the data fields change (information is held in a weakly typed name/value collection of fields).





Assign a custom data object to the Tag. This allows you to wrap all the datarelated functionality you need into a neat object. The disadvantage is that it requires more steps. For example, if you are retrieving your data from a database, you need to create the corresponding object, initialize its data, and then assign it to the Tag property.





Derive a custom ListViewItem, and add the properties you need for your particular type of data. Though this is the only approach directly explained in the MSDN reference, it is probably the least convenient because it tightly integrates details about the structure of your data into the user interface code. That means that you need to modify these classes if the data changes or if you move to a different type of control (like the TreeView).





Chapter 9 introduces a reusable pattern that allows a control to interact with data objects without needing to know their database-specific internals. It also provides the flexibility to change data access strategies or the display control.






/ 142