Tải bản đầy đủ - 0 (trang)
7-10. Sort a List View by Any Column

7-10. Sort a List View by Any Column

Tải bản đầy đủ - 0trang

CHAPTER 7 ■ WINDOWS FORMS



// Initialization code is designer generated and contained

// in a separate file named Recipe07-10.Designer.cs.

InitializeComponent();

}

// Event handler to handle user clicks on column headings.

private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)

{

// Create and/or configure the ListViewItemComparer to sort based on

// the column that was clicked.

ListViewItemComparer sorter =

listView1.ListViewItemSorter as ListViewItemComparer;

if (sorter == null)

{

// Create a new ListViewItemComparer.

sorter = new ListViewItemComparer(e.Column);

listView1.ListViewItemSorter = sorter;

}

else

{

// Configure the existing ListViewItemComparer.

sorter.Column = e.Column;

}

// Sort the ListView.

listView1.Sort();

}

[STAThread]

public static void Main(string[] args)

{

Application.Run(new Recipe07_10());

}

}

public class ListViewItemComparer : IComparer

{

// Property to get/set the column to use for comparison.

public int Column { get; set; }

// Property to get/set whether numeric comparison is required

// as opposed to the standard alphabetic comparison.

public bool Numeric { get; set; }

public ListViewItemComparer(int columnIndex)

{

Column = columnIndex;

}



333



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



public int Compare(object x, object y)

{

// Convert the arguments to ListViewItem objects.

ListViewItem itemX = x as ListViewItem;

ListViewItem itemY = y as ListViewItem;

// Handle logic for null reference as dictated by the

// IComparer interface; null is considered less than

// any other value.

if (itemX == null && itemY == null) return 0;

else if (itemX == null) return -1;

else if (itemY == null) return 1;

// Short-circuit condition where the items are references

// to the same object.

if (itemX == itemY) return 0;

// Determine if numeric comparison is required.

if (Numeric)

{

// Convert column text to numbers before comparing.

// If the conversion fails, just use the value 0.

decimal itemXVal, itemYVal;

if (!Decimal.TryParse(itemX.SubItems[Column].Text, out itemXVal))

{

itemXVal = 0;

}

if (!Decimal.TryParse(itemY.SubItems[Column].Text, out itemYVal))

{

itemYVal = 0;

}

return Decimal.Compare(itemXVal, itemYVal);

}

else

{

// Keep the column text in its native string format

// and perform an alphabetic comparison.

string itemXText = itemX.SubItems[Column].Text;

string itemYText = itemY.SubItems[Column].Text;

return String.Compare(itemXText, itemYText);

}

}

}

}



334



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



7-11. Lay Out Controls Automatically

Problem

You have a large set of controls on a form and you want them arranged automatically.



Solution

Use the FlowLayoutPanel container to dynamically arrange the controls using a horizontal or vertical

flow, or use the TableLayoutPanel container to dynamically arrange the controls in a grid.



How It Works

The FlowLayoutPanel and TableLayoutPanel containers simplify the design-time and runtime layout of

the controls they contain. At both design time and runtime, as you add controls to one of these panels,

the panel’s logic determines where the control should be positioned, so you do not need to determine

the exact location.

With the FlowLayoutPanel container, the FlowDirection and WrapContents properties determine

where controls are positioned. FlowDirection controls the order and location of controls, and it can be

set to LeftToRight, TopDown, RightToLeft, or BottomUp. The WrapContents property controls whether

controls run off the edge of the panel or wrap around to form a new line of controls.

With the TableLayoutPanel container, the RowCount and ColumnCount properties control how many

rows and columns are currently in the panel’s grid. The GrowStyle property determines how the grid

grows to accommodate more controls once it is full, and it can be set to AddRows, AddColumns, or

FixedSize (which means the grid cannot grow).

Figure 7-6 shows the design-time appearance of both a TableLayoutPanel container and a

FlowLayoutPanel container. The TableLayoutPanel panel is configured with three rows and three

columns. The FlowLayoutPanel panel is configured to wrap contents and use left-to-right flow direction.



Figure 7-6. Using a FlowLayoutPanel panel and a TableLayoutPanel panel



335



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



7-12. Use Part of a Main Menu for a Context Menu

Problem

You need to create a context menu that shows the same menu items as those displayed as part of an

application’s main menu.



Solution

Use the CloneMenu method of the MenuItem class to duplicate the required portion of the main menu.



How It Works

In many applications, a control’s context-sensitive menu duplicates a portion of the main menu.

However, .NET does not allow you to create a MenuItem instance that is contained in more than one

menu at a time.

The solution is to make a duplicate copy of a portion of the menu using the CloneMenu method. The

CloneMenu method not only copies the appropriate MenuItem items (and any contained submenus), but

also registers each MenuItem object with the same event handlers. Thus, when a user clicks a cloned

menu item in a context menu, the event handler will be triggered as if the user had clicked the duplicate

menu item in the main menu.



The Code

The following example uses the CloneMenu method to configure the context menu for a TextBox to be a

duplicate of the File menu. Figure 7-7 shows how the example will look when run.

using System;

using System.Drawing;

using System.Windows.Forms;

namespace Apress.VisualCSharpRecipes.Chapter07

{

public partial class Recipe07_12 : Form

{

public Recipe07_12()

{

// Initialization code is designer generated and contained

// in a separate file named Recipe07-12.Designer.cs.

InitializeComponent();

}

// As the main form loads, clone the required section of the main

// menu and assign it to the ContextMenu property of the TextBox.

protected override void OnLoad(EventArgs e)

{



336



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

7-10. Sort a List View by Any Column

Tải bản đầy đủ ngay(0 tr)

×