Tải bản đầy đủ - 0 (trang)
7-9. Use an Autocomplete Combo Box or Text Box

7-9. Use an Autocomplete Combo Box or Text Box

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

CHAPTER 7 ■ WINDOWS FORMS



values), or the control might display a drop-down list of near matches (as Microsoft Internet Explorer

does when you are typing a URL). The AutoCompleteMode takes one of the following values, which define

how the control’s autocomplete behavior works:





None: Autocomplete is disabled. This is the default behavior for ComboBox and

TextBox.







Suggest: This displays suggestions as a drop-down list.







Append: This appends the remainder of the most likely suggestion to the end of the

text as the user enters it.







SuggestAppend: This combines the functionality of both Suggest and Append.



The AutoCompleteSource property defines where the ComboBox or TextBox control sources the

autocomplete suggestions it presents to the user. It is possible to make use of various system-level data

sources like the file system or URL histories. The most commonly used values for the

AutoCompleteSource property are ListItems, where the ComboBox uses its current content and

CustomSource. If you specify CustomSource, you must populate the AutoCompleteCustomSource property of

the ComboBox or TextBox with the set of strings you want to use as autocomplete suggestions.



The Code

The following example enables autocomplete on a ComboBox and populates it with a list of values using a

custom source. Figure 7-5 shows how the control offers suggestions to the user when AutoCompleteMode

is set to the value SuggestAppend.

using

using

using

using



System;

System.IO;

System.Drawing;

System.Windows.Forms;



namespace Apress.VisualCSharpRecipes.Chapter07

{

public partial class Recipe07_09 : Form

{

public Recipe07_09()

{

// Initialization code is designer generated and contained

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

InitializeComponent();

// Configure ComboBox1 to make its autocomplete

// suggestions from a custom source.

this.comboBox1.AutoCompleteCustomSource.AddRange(

new string[] { "Man", "Mark", "Money", "Motley",

"Mostly", "Mint", "Minion", "Milk", "Mist",

"Mush", "More", "Map", "Moon", "Monkey"});



330



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



this.comboBox1.AutoCompleteMode

= AutoCompleteMode.SuggestAppend;

this.comboBox1.AutoCompleteSource

= AutoCompleteSource.CustomSource;

// Configure ComboBox2 to make its autocomplete

// suggestions from its current contents.

this.comboBox2.Items.AddRange(

new string[] { "Man", "Mark", "Money", "Motley",

"Mostly", "Mint", "Minion", "Milk", "Mist",

"Mush", "More", "Map", "Moon", "Monkey"});

this.comboBox2.AutoCompleteMode

= AutoCompleteMode.SuggestAppend;

this.comboBox2.AutoCompleteSource

= AutoCompleteSource.ListItems;

// Configure ComboBox3 to make its autocomplete

// suggestions from the system's URL history.

this.comboBox3.AutoCompleteMode

= AutoCompleteMode.SuggestAppend;

this.comboBox3.AutoCompleteSource

= AutoCompleteSource.AllUrl;

}

[STAThread]

public static void Main(string[] args)

{

Application.Run(new Recipe07_09());

}

}

}



Figure 7-5. An autocomplete combo box



331



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



7-10. Sort a List View by Any Column

Problem

You need to sort a list view, but the built-in ListView.Sort method sorts based on only the first column.



Solution

Create a type that implements the System.Collections.IComparer interface and can sort ListViewItem

objects. The IComparer type can sort based on any ListViewItem criteria you specify. Set the

ListView.ListViewItemSorter property with an instance of the IComparer type before calling the

ListView.Sort method.



How It Works

The ListView control provides a Sort method that orders items alphabetically based on the text in the

first column. If you want to sort based on other column values or order items numerically, you need to

create a custom implementation of the IComparer interface that can perform the work. The IComparer

interface defines a single method named Compare, which takes two object arguments and determines

which one should be ordered first. Full details of how to implement the IComparer interface are available

in recipe 13-3.



The Code

The following example demonstrates the creation of an IComparer implementation named

ListViewItemComparer. The ListViewItemComparer class also implements two additional properties:

Column and Numeric. The Column property identifies the column that should be used for sorting. The

Numeric property is a Boolean flag that can be set to true if you want to perform number-based

comparisons instead of alphabetic comparisons.

When the user clicks a column heading, the example creates a ListViewItemComparer instance,

configures the column to use for sorting, and assigns the ListViewItemComparer instance to the

ListView.ListViewItemSorter property before calling the ListView.Sort method.

using System;

using System.Collections;

using System.Windows.Forms;

namespace Apress.VisualCSharpRecipes.Chapter07

{

public partial class Recipe07_10 : Form

{

public Recipe07_10()

{



332



www.it-ebooks.info



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



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

7-9. Use an Autocomplete Combo Box or Text Box

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

×