Tải bản đầy đủ - 0 (trang)
7-8. Restrict a Text Box to Accept Only Specific Input

7-8. Restrict a Text Box to Accept Only Specific Input

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

CHAPTER 7 ■ WINDOWS FORMS



each point in the control’s text area. If the user enters an incorrect character, the control will beep if the

BeepOnError property is true, and the MaskInputRejected event will be raised so that you can customize

the handling of incorrect input.



■ Note The MaskedTextBox control will not solve all your user input validation problems. While it does make some

types of validation easy to implement, without customization it will not ensure some common validation

requirements are met. For example, you can specify that only numeric digits can be input, but you cannot specify

that they must be less than a specific value, nor can you control the overall characteristics of the input value.

Recipe 2-5 discusses regular expressions which provide a great deal of flexibility when testing whether text meets

complex formatting requirements.



The Code

The following example demonstrates the use of the MaskedTextBox control. A series of buttons allows you

to change the active mask on the MaskedTextBox control and experiment with the various masks. Notice

that the control tries to accommodate existing content with the new mask when the mask is changed. If

the content is not allowed with the new mask, the control is cleared.

using System;

using System.Threading;

using System.Windows.Forms;

namespace Apress.VisualCSharpRecipes.Chapter07

{

public partial class Recipe07_08 : Form

{

public Recipe07_08()

{

// Initialization code is designer generated and contained

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

InitializeComponent();

}

private void btnTime_Click(object sender, EventArgs e)

{

// Set the input mask to that of a short time.

this.mskTextBox.UseSystemPasswordChar = false;

this.mskTextBox.Mask = "00:00";

this.lblActiveMask.Text = this.mskTextBox.Mask;

this.mskTextBox.Focus();

}



327



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



private void btnUSZip_Click(object sender, EventArgs e)

{

// Set the input mask to that of a US ZIP code.

this.mskTextBox.UseSystemPasswordChar = false;

this.mskTextBox.Mask = "00000-9999";

this.lblActiveMask.Text = this.mskTextBox.Mask;

this.mskTextBox.Focus();

}

private void btnUKPost_Click(object sender, EventArgs e)

{

// Set the input mask to that of a UK postcode.

this.mskTextBox.UseSystemPasswordChar = false;

this.mskTextBox.Mask = ">LCCC 9LL";

this.lblActiveMask.Text = this.mskTextBox.Mask;

this.mskTextBox.Focus();

}

private void btnCurrency_Click(object sender, EventArgs e)

{

// Set the input mask to that of a currency.

this.mskTextBox.UseSystemPasswordChar = false;

this.mskTextBox.Mask = "$999,999.00";

this.lblActiveMask.Text = this.mskTextBox.Mask;

this.mskTextBox.Focus();

}

private void btnDate_Click(object sender, EventArgs e)

{

// Set the input mask to that of a short date.

this.mskTextBox.UseSystemPasswordChar = false;

this.mskTextBox.Mask = "00/00/0000";

this.lblActiveMask.Text = this.mskTextBox.Mask;

this.mskTextBox.Focus();

}

private void btnSecret_Click(object sender, EventArgs e)

{

// Set the input mask to that of a secret PIN.

this.mskTextBox.UseSystemPasswordChar = true;

this.mskTextBox.Mask = "0000";

this.lblActiveMask.Text = this.mskTextBox.Mask;

this.mskTextBox.Focus();

}



328



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



[STAThread]

public static void Main(string[] args)

{

Application.Run(new Recipe07_08());

}

}

}



Notes

The MaskedTextBox used in this recipe was introduced in the .NET Framework 2.0. In previous versions of

the .NET Framework, one approach was to use a standard TextBox control and handle the KeyPress

events it raises. The KeyPress event is raised after each keystroke has been received but before it is

displayed. You can use the KeyPressEventArgs event parameter to effectively cancel an invalid keystroke

by setting its Handled property to true.

For example, to allow only numeric input, you must allow a keystroke only if it corresponds to a

number (0 through 9) or a special control key (such as Delete or the arrow keys). The keystroke character

is provided to the KeyPress event through the KeyPressEventArgs.KeyChar property. You can use two

static methods of the System.Char class—IsDigit and IsControl—to quickly test the character.



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

Problem

You want to display a combo box or text box that automatically completes what the user is typing based

on a list of predefined items.



Solution

Configure the autocomplete features of the standard .NET ComboBox or TextBox control. The

AutoCompleteMode property controls the autocompletion behavior, and the AutoCompleteSource property

allows you to specify the source of the autocomplete data.



■ Note Prior to the addition of the autocomplete functionality to the ComboBox and TextBox controls in the .NET

Framework 2.0, to implement autocomplete functionality it was necessary to create a custom control that inherited

from ComboBox or TextBox and overrode the inherited OnKeyPress and OnTextChanged methods.



How It Works

Autocomplete functionality is common and comes in many different variations. For example, a control

may fill in values based on a list of recent selections (as Microsoft Excel does when you are entering cell



329



www.it-ebooks.info



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



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

7-8. Restrict a Text Box to Accept Only Specific Input

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

×