GrapeCity InputMan for Windows Forms 10.0J
汎用検証コンポーネント

汎用検証コンポーネントでは、GcTextBox等のテキスト入力用のコントロールやVisual Studio標準およびサードパーティ製のコントロールの検証に適しており、入力画面で用いられるさまざまな検証方法(検証アイテム)が用意されています。

本項では、汎用検証コンポーネントに用意された検証アイテムの種類とその設定方法について解説します。なお、検証アイテムはデザイン画面からの設定も可能ですが、ここではコードによる設定方法を紹介しています。 デザイン画面での設定方法については「検証機能と設定方法」を参照してください。また、エラーの通知方法や不正値の処理の設定方法については「検証アクション」を参照してください。

必須入力(EmptyText)

EmptyText オブジェクトは、対象のコントロールのText プロパティが空かどうかを検証します。コントロールのText プロパティが空もしくはNull の場合は不正とみなされます。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim EmptyText1 As New EmptyText()
EmptyText1.InvalidMessage = "入力必須項目です"

' TextBox1に作成した検証アイテムを設定します。 
GcCommonValidator1.GetValidateItems(TextBox1).AddRange(New Object() {EmptyText1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。 
EmptyText emptyText1 = new EmptyText();
emptyText1.InvalidMessage = "入力必須項目です";

// textBox1に作成した検証アイテムを設定します。 
gcCommonValidator1.GetValidateItems(textBox1).AddRange(new object[] { emptyText1 });
データタイプ(InvalidType)

InvalidType オブジェクトは、コントロールに入力された値が指定したデータ型に一致するかどうか検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim InvalidType1 As New InvalidType()
' Integer型の入力に制限します。
InvalidType1.ExpectedType = ExpectedType.Integer
' エラーメッセージを設定します。
InvalidType1.InvalidMessage = "整数を入力してください。"

' TextBox1に作成した検証アイテムを設定します。 
GcCommonValidator1.GetValidateItems(TextBox1).AddRange(New Object() {EmptyText1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。 
EmptyText invalidType1 = new InvalidType();
// Integer型の入力に制限します。
InvalidType1.ExpectedType = ExpectedType.Integer;
// エラーメッセージを設定します。
invalidType1.InvalidMessage = "整数を入力してください。";

// textBox1に作成した検証アイテムを設定します。 
gcCommonValidator1.GetValidateItems(textBox1).AddRange(new object[] { emptyText1 });
文字列リスト(IncludeList)

IncludeList オブジェクトは、文字列リストに設定した文字列のいずれかが入力されているかどうか検証します。入力された文字列がリスト内にない場合、不正とみなされます。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim IncludeList1 As New IncludeList()
' 文字列リストを設定
IncludeList1.Candidates = New String() {"abc", "def", "ghi"}
' エラーメッセージを設定
IncludeList1.InvalidMessage = "不正な文字列が入力されました。"

' TextBox1に作成した検証アイテムを設定します。 
GcCommonValidator1.GetValidateItems(TextBox1).AddRange(New Object() {IncludeList1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。
IncludeList includeList1 = new IncludeList();
// 文字列リストを設定
includeList1.Candidates = new String[] {"abc", "def", "ghi"};
// エラーメッセージを設定
includeList1.InvalidMessage = "不正な文字列が入力されました。";

// textBox1に作成した検証アイテムを設定します。 
gcCommonValidator1.GetValidateItems(textBox1).AddRange(new object[] { includeList1 });
禁止文字(ExcludeList)

ExcludeList オブジェクトは、文字列リストに設定した禁止文字列が入力されていないか検証します。リスト内にある文字列が入力された場合、不正とみなされます。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim ExcludeList1 As New ExcludeList()
' 文字列リストを設定します。
ExcludeList1.Candidates = New String() {"abc", "def", "ghi"}
' エラーメッセージを設定します。
ExcludeList1.InvalidMessage = "入力が禁止された文字列です。"

' TextBox1に作成した検証アイテムを設定します。 
GcCommonValidator1.GetValidateItems(TextBox1).AddRange(New Object() {ExcludeList1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。
ExcludeList excludeList1 = new ExcludeList();
// 文字列リストを設定します。
excludeList1.Candidates = new String[] {"abc", "def", "ghi"};
// エラーメッセージを設定します。
excludeList1.InvalidMessage = "入力が禁止された文字列です。";

// textBox1に作成した検証アイテムを設定します。 
gcCommonValidator1.GetValidateItems(textBox1).AddRange(new object[] { excludeList1 });
サロゲートペア文字(SurrogateChar)

SurrogateChar オブジェクトは、JIS2004で追加されたサロゲートペア文字が入力されていないかどうかを検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim SurrogateChar1 As New SurrogateChar()
' エラーメッセージを設定します。
SurrogateChar1.InvalidMessage = "サロゲートペア文字(環境依存文字)は入力できません。"

' TextBox1に作成した検証アイテムを設定します。 
GcCommonValidator1.GetValidateItems(TextBox1).AddRange(New Object() {SurrogateChar1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。
SurrogateChar surrogateChar1 = new SurrogateChar();
// エラーメッセージを設定します。
surrogateChar1.InvalidMessage = "サロゲートペア文字(環境依存文字)は入力できません。";

// textBox1に作成した検証アイテムを設定します。 
gcCommonValidator1.GetValidateItems(textBox1).AddRange(new object[] { surrogateChar1 });
正規表現(RegularExpression)

RegularExpression オブジェクトは、入力された文字列を正規表現を使って検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim RegularExpression1 As New RegularExpression()
' 正規表現により英字のみ入力を許可します。
RegularExpression1.Expression = "[a-z]+"
' エラーメッセージを設定します。
RegularExpression1.InvalidMessage = "1文字以上の英字を入力してください。"

' TextBox1に作成した検証アイテムを設定します。 
GcCommonValidator1.GetValidateItems(TextBox1).AddRange(New Object() {RegularExpression1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。
RegularExpression regularExpression1 = new RegularExpression();
// 正規表現により英字のみ入力を許可します。
regularExpression1.Expression = "[a-z]+";
// エラーメッセージを設定します。
regularExpression1.InvalidMessage = "1文字以上の英字を入力してください。";

// textBox1に作成した検証アイテムを設定します。 
gcCommonValidator1.GetValidateItems(textBox1).AddRange(new object[] { regularExpression1 });
ペア文字(InvalidPairChar)

InvalidPairChar オブジェクトは、カッコなどのペアとなる文字が両方存在するかどうかを検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。 
Dim InvalidPairChar1 As New InvalidPairChar()
' 丸カッコ()をペアの対象として設定 
Dim PairChar1 As New PairChar()
PairChar1.Left = ChrW(40)
PairChar1.Right = ChrW(41)
InvalidPairChar1.PairChars.Add(PairChar1)
' エラーメッセージを設定 
InvalidPairChar1.InvalidMessage = "ペアになる文字が両方存在しません。"

' TextBox1に作成した検証アイテムを設定します。  
GcCommonValidator1.GetValidateItems(TextBox1).AddRange(New Object() {InvalidPairChar1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。 
InvalidPairChar invalidPairChar1 = new InvalidPairChar();
// 丸カッコ()をペアの対象として設定 
PairChar pairChar1 =new PairChar();
pairChar1.Left = '(';
pairChar1.Right = ')';
invalidPairChar1.PairChars.Add(pairChar1);
// エラーメッセージを設定 
invalidPairChar1.InvalidMessage = "ペアになる文字が両方存在しません。";

// textBox1に作成した検証アイテムを設定します。  
gcCommonValidator1.GetValidateItems(textBox1).AddRange(new Object[] { invalidPairChar1 });
数値範囲(InvalidNumberRange)

InvalidNumberRange オブジェクトは、入力された値が指定した数値の範囲内の値かどうかを検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim InvalidNumberRange1 As New InvalidNumberRange()
' 0 〜 1000 の範囲の数値を入力できます。
InvalidNumberRange1.MaxValue = 1000
InvalidNumberRange1.MinValue = 0
' エラーメッセージを設定します。
InvalidNumberRange1.InvalidMessage = "0 〜 1000 の範囲の数値を入力できます。"

' NumericUpDown1に作成した検証アイテムを設定します。 
GcCommonValidator1.GetValidateItems(NumericUpDown1).AddRange(New Object() {InvalidNumberRange1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。
InvalidNumberRange invalidNumberRange1 = new InvalidNumberRange();
// 0 〜 1000 の範囲の数値を入力できます。
invalidNumberRange1.MaxValue = 1000;
invalidNumberRange1.MinValue = 0;
// エラーメッセージを設定します。
invalidNumberRange1.InvalidMessage = "0 〜 1000 の範囲の数値を入力できます。";

// numericUpDown1に作成した検証アイテムを設定します。 
gcCommonValidator1.GetValidateItems(numericUpDown1).AddRange(new object[] { invalidNumberRange1 });
日付範囲(InvalidDateTimeRange)

InvalidDateTimeRange オブジェクトは、入力された値が、指定した日付の範囲内の値かどうかを検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim InvalidDateTimeRange1 As New InvalidDateTimeRange()
' 2000/1/1 0:00:00 〜 2100/12/31 0:00:00 の範囲の日付を入力できます。
InvalidDateTimeRange1.MaxValue = DateTime.Parse("2100/12/31 0:00:00")
InvalidDateTimeRange1.MinValue = DateTime.Parse("2000/1/1 0:00:00")
' エラーメッセージを設定します。
InvalidDateTimeRange1.InvalidMessage = "2000/1/1 〜 2100/12/31 の範囲の日付を入力できます。"

' DateTimePicker1に作成した検証アイテムを設定します。 
GcCommonValidator1.GetValidateItems(DateTimePicker1).AddRange(New Object() {InvalidDateTimeRange1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。
InvalidDateTimeRange invalidDateTimeRange1 = new InvalidDateTimeRange();
// 2000/1/1 0:00:00 〜 2100/12/31 0:00:00 の範囲の日付を入力できます。
invalidDateTimeRange1.MaxValue = DateTime.Parse("2100/12/31 0:00:00");
invalidDateTimeRange1.MinValue = DateTime.Parse("2000/1/1 0:00:00");
// エラーメッセージを設定します。
invalidDateTimeRange1.InvalidMessage = "2000/1/1 〜 2100/12/31 の範囲の日付を入力できます。";

// dateTimePicker1に作成した検証アイテムを設定します。 
gcCommonValidator1.GetValidateItems(dateTimePicker1).AddRange(new object[] { invalidDateTimeRange1 });
他のコントロールと数値比較(NumberCompareControl)

NumberCompareControl オブジェクトは、対象となるコントロールの値と数値を比較検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim NumberCompareControl1 As New NumberCompareControl()
' 比較対象のコントロールを指定します。
NumberCompareControl1.ComparedControl = NumericUpDown2
' 比較方法を設定します。
NumberCompareControl1.ComparedOperator = ComparedOperator.GreaterThan
NumberCompareControl1.DifferenceValue = 0
' エラーメッセージを設定します。
NumberCompareControl1.InvalidMessage = "最小値で設定した値より大きい値を入力してください。"

' NumericUpDown1に作成した検証アイテムを設定します。
GcCommonValidator1.GetValidateItems(NumericUpDown1).AddRange(New Object() {NumberCompareControl1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。
NumberCompareControl numberCompareControl1 = new NumberCompareControl();
// 比較対象のコントロールを指定します。
numberCompareControl1.ComparedControl = NumericUpDown2;
// 比較方法を設定します。
numberCompareControl1.ComparedOperator = ComparedOperator.GreaterThan;
numberCompareControl1.DifferenceValue = 0;
// エラーメッセージを設定します。
numberCompareControl1.InvalidMessage = "最小値で設定した値より大きい値を入力してください。";

// numericUpDown1に作成した検証アイテムを設定します。 
gcCommonValidator1.GetValidateItems(numericUpDown1).AddRange(new object[] { numberCompareControl1 });
数値比較(NumberCompareValue)

NumberCompareValue オブジェクトは、指定する数値と値を比較検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim NumberCompareValue1 As New NumberCompareValue()

' 比較する値を設定します。
NumberCompareValue1.ComparedValue = 0

' 比較方法を設定します。
NumberCompareValue1.ComparedOperator = ComparedOperator.GreaterThan
NumberCompareValue1.DifferenceValue = 0
' エラーメッセージを設定します。
NumberCompareValue1.InvalidMessage = "0より大きい値を入力してください。"

' NumericUpDown1に作成した検証アイテムを設定します。
GcCommonValidator1.GetValidateItems(NumericUpDown1).AddRange(New Object() {NumberCompareValue1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。
NumberCompareValue numberCompareValue1 = new NumberCompareValue();

// 比較する値を設定します。
numberCompareValue1.ComparedValue = 0;

// 比較方法を設定します。
numberCompareValue1.ComparedOperator = ComparedOperator.GreaterThan;
numberCompareValue1.DifferenceValue = 0;
// エラーメッセージを設定します。
numberCompareValue1.InvalidMessage = "0 より大きい値を入力してください。";

// numericUpDown1に作成した検証アイテムを設定します。 
gcCommonValidator1.GetValidateItems(numericUpDown1).AddRange(new object[] { numberCompareValue1 });
他のコントロールと日付比較(DateTimeCompareControl)

DateTimeCompareControl オブジェクトは、対象となるコントロールの値と日付を比較検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim DateTimeCompareControl1 As New DateTimeCompareControl()
' 比較対象のコントロールを指定します。
DateTimeCompareControl1.ComparedControl = DateTimePicker2
' 比較方法を設定します。
DateTimeCompareControl1.ComparedOperator = ComparedOperator.SmallerThanOrEquals
DateTimeCompareControl1.DifferenceValue = New TimeSpan(2, 0, 0, 0)
' エラーメッセージを設定します。
DateTimeCompareControl1.InvalidMessage = "2日後より未来の日付は入力できません。"

' DateTimePicker1に作成した検証アイテムを設定します。
GcCommonValidator1.GetValidateItems(DateTimePicker1).AddRange(New Object() {DateTimeCompareControl1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。
DateTimeCompareControl dateTimeCompareControl1 =new DateTimeCompareControl();
// 比較対象のコントロールを指定します。
dateTimeCompareControl1.ComparedControl = dateTimePicker2;
// 比較方法を設定します。
dateTimeCompareControl1.ComparedOperator = ComparedOperator.SmallerThanOrEquals;
dateTimeCompareControl1.DifferenceValue = new TimeSpan(2, 0, 0, 0);
// エラーメッセージを設定します。
dateTimeCompareControl1.InvalidMessage = "2日後より未来の日付は入力できません。";

// DateTimePicker1に作成した検証アイテムを設定します。
gcCommonValidator1.GetValidateItems(dateTimePicker1).AddRange(new Object[] { dateTimeCompareControl1 });
日付値比較(DateTimeCompareValue)

DateTimeCompareValue オブジェクトは、指定する日付値と値を比較検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim DateTimeCompareValue1 As New DateTimeCompareValue()
' 比較する値を設定します。
DateTimeCompareValue1.ComparedValue = New Date(2100, 12, 31, 23, 59, 59)
' 比較方法を設定します。
DateTimeCompareValue1.ComparedOperator = ComparedOperator.SmallerThan
DateTimeCompareValue1.DifferenceValue = New TimeSpan(0)
' エラーメッセージを設定します。
DateTimeCompareValue1.InvalidMessage = "2100/12/31より小さい値を入力してください。"

' NumericUpDown1に作成した検証アイテムを設定します。
GcCommonValidator1.GetValidateItems(DateTimePicker1).AddRange(New Object() {DateTimeCompareValue1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。
DateTimeCompareValue dateTimeCompareValue1 = new DateTimeCompareValue();
// 比較する値を設定します。
dateTimeCompareValue1.ComparedValue = new DateTime(2100, 12, 31, 23, 59, 59);
// 比較方法を設定します。
dateTimeCompareValue1.ComparedOperator = ComparedOperator.SmallerThan;
dateTimeCompareValue1.DifferenceValue = new TimeSpan(0);
// エラーメッセージを設定します。
dateTimeCompareValue1.InvalidMessage = "2100/12/31より小さい値を入力してください。";

// NumericUpDown1に作成した検証アイテムを設定します。
gcCommonValidator1.GetValidateItems(dateTimePicker1).AddRange(new Object[] { dateTimeCompareValue1 });
文字種(CharFormat)

CharFormat オブジェクトは、入力された文字が文字種キーワードで指定した文字種かどうか検証します。 使用する文字種キーワードはテキストコントロールの「書式の設定」と同じものです。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim CharFormat1 As New CharFormat()
' Shift JIS文字で構成された文字の入力のみを許可します。 
CharFormat1.Format = "M"
' エラーメッセージを設定します。
CharFormat1.InvalidMessage = "Shift JIS文字で構成された文字以外の入力はできません。"

' TextBox1に作成した検証アイテムを設定します。  
GcCommonValidator1.GetValidateItems(TextBox1).AddRange(New Object() {CharFormat1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。 
CharFormat charFormat1 = new CharFormat();
// Shift JIS文字で構成された文字の入力のみを許可します。 
charFormat1.Format = "M";
// エラーメッセージを設定します。
charFormat1.InvalidMessage = "Shift JIS文字で構成された文字以外の入力はできません。";

// textBox1に作成した検証アイテムを設定します。  
gcCommonValidator1.GetValidateItems(textBox1).AddRange(new object[] { charFormat1 });
文字列比較(CompareString)

CompareString オブジェクトは、指定する文字列と入力された文字列を比較検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim CompareString1 As New CompareString()
' 比較する文字と比較方法を設定します。 
CompareString1.ComparedString = "AAA"
CompareString1.ComparedOperator = CompareStringOperator.Equals
                                                        
' エラーメッセージを設定します。
CompareString1.InvalidMessage = "「AAA」以外の入力はできません。"

' TextBox1に作成した検証アイテムを設定します。  
GcCommonValidator1.GetValidateItems(TextBox1).AddRange(New Object() {CompareString1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。 
CompareString compareString1 = new CompareString();
// Shift JIS文字で構成された文字の入力のみを許可します。 
compareString1.ComparedString = "AAA";
compareString1.ComparedOperator = CompareStringOperator.Equals;
// エラーメッセージを設定します。
compareString1.InvalidMessage = "「AAA」以外の入力はできません。";

// textBox1に作成した検証アイテムを設定します。  
gcCommonValidator1.GetValidateItems(textBox1).AddRange(new object[] { compareString1 });
文字列の長さ(TextLength)

TextLength オブジェクトは、コントロール文字列の長さが指定した範囲内かどうかを検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim TextLength1 As New TextLength()
' 入力可能な文字数とカウント方法を設定します。
TextLength1.MaximumLength = 10
TextLength1.MinimumLength = 5
TextLength1.LengthUnit = LengthUnit.Byte
' エラーメッセージを設定します。
TextLength1.InvalidMessage = "5〜10文字で入力してください。"

' TextBox1に作成した検証アイテムを設定します。  
GcCommonValidator1.GetValidateItems(TextBox1).AddRange(New Object() {TextLength1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。 
TextLength textLength1 = new TextLength();
// 入力可能な文字数とカウント方法を設定します。
textLength1.MaximumLength = 10;
textLength1.MinimumLength = 5;
textLength1.LengthUnit = LengthUnit.Byte;

// エラーメッセージを設定します。
textLength1.InvalidMessage = "5〜10文字で入力してください。";

// textBox1に作成した検証アイテムを設定します。  
gcCommonValidator1.GetValidateItems(textBox1).AddRange(new object[] { textLength1 });
エンコード形式(EncodingCheck)

EncodingCheck オブジェクトは、コントロールの値が指定したエンコードの種類に一致するかどうかを検証します。

プロパティ

サンプルコード

Imports GrapeCity.Win.Editors

' 検証アイテムを作成します。
Dim EncodingCheck1 As New EncodingCheck()
' コードページにShift−JISを指定します。
EncodingCheck1.EncodingCodePage = 932
' エラーメッセージを設定します。
EncodingCheck1.InvalidMessage = "Shift−JIS以外の入力はできません。"

' TextBox1に作成した検証アイテムを設定します。  
GcCommonValidator1.GetValidateItems(TextBox1).AddRange(New Object() {EncodingCheck1})
using GrapeCity.Win.Editors;

// 検証アイテムを作成します。 
EncodingCheck encodingCheck1 = new EncodingCheck();
// コードページにShift−JISを指定します。
encodingCheck1.EncodingCodePage = 932;
// エラーメッセージを設定します。
encodingCheck1.InvalidMessage = "Shift-JIS以外の入力はできません。";

// textBox1に作成した検証アイテムを設定します。  
gcCommonValidator1.GetValidateItems(textBox1).AddRange(new object[] { encodingCheck1 });
参照

 

 


© 2004 GrapeCity inc. All rights reserved.