独自の検証アイテム/検証アクションを作成し、検証コンポーネントに設定するには?
対象製品
InputMan for Windows Forms 8.0J
詳細
コントロールの値に対して、InputManの各検証コンポーネントで提供されている内容以外の検証を行ったり、無効時の動作をカスタマイズしたい場合には、独自の検証アイテム/検証アクションを作成し、それを各検証コンポーネントに設定することができます。
以下に汎用、数値および日付検証コンポーネントをカスタマイズする方法を紹介します。
汎用検証コンポーネントのカスタマイズ
汎用検証コンポーネントに、以下の設定を行う例です。
検証アイテム : 入力された文字数が10文字以下の場合は無効
検証アクション :ColorNotify
[Visual Basic]
[C#]
数値検証コンポーネントのカスタマイズ
数値検証コンポーネントに、以下の設定を行う例です。
検証アイテム : InvalidRange
検証アクション :無効の場合には値を[0]に設定
[Visual Basic]
[C#]
日付検証コンポーネントのカスタマイズ
日付検証コンポーネントに、以下の設定を行う例です。
検証アイテム : 入力された日付が月末の場合は無効
検証アクション :無効の場合には値を翌日に設定
[Visual Basic]
[C#]
以下に汎用、数値および日付検証コンポーネントをカスタマイズする方法を紹介します。
汎用検証コンポーネントのカスタマイズ
汎用検証コンポーネントに、以下の設定を行う例です。
検証アイテム : 入力された文字数が10文字以下の場合は無効
検証アクション :ColorNotify
[Visual Basic]
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' 独自の検証アイテムを設定します。 Dim ValidateItem1 As New CustomValidateItem() GcCommonValidator1.GetValidateItems(GcTextBox1).Add(ValidateItem1) ' 検証アクション(ColorNotify)を設定します。 Dim ColorNotify1 As New GrapeCity.Win.Editors.ColorNotify() ColorNotify1.InvalidBackColor = Color.Red ColorNotify1.InvalidForeColor = Color.White ColorNotify1.ValidBackColor = GcTextBox1.BackColor ColorNotify1.ValidForeColor = GcTextBox1.ForeColor GcCommonValidator1.GetValidateActions(GcTextBox1).Add(ColorNotify1) End Sub End Class '' '' 独自の検証アイテムを作成します。 '' Public Class CustomValidateItem Inherits GrapeCity.Win.Editors.Validator(Of Control).ValidateItem Protected Overrides Function Validate() As Boolean Dim targetControl As GrapeCity.Win.Editors.GcTextBox = DirectCast(Me.Target, GrapeCity.Win.Editors.GcTextBox) ' 入力された文字数が10文字以下の場合には無効とします。 If targetControl.TextLength <= 10 Then Return False End If Return True End Function End Class
[C#]
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // 独自の検証アイテムを設定します。 CustomValidateItem validateItem1 = new CustomValidateItem(); gcCommonValidator1.GetValidateItems(gcTextBox1).Add(validateItem1); // 検証アクション(ColorNotify)を設定します。 GrapeCity.Win.Editors.ColorNotify colotNotify1 = new GrapeCity.Win.Editors.ColorNotify(); colotNotify1.InvalidBackColor = Color.Red; colotNotify1.InvalidForeColor = Color.White; colotNotify1.ValidBackColor = gcTextBox1.BackColor; colotNotify1.ValidForeColor = gcTextBox1.ForeColor; gcCommonValidator1.GetValidateActions(gcTextBox1).Add(colotNotify1); } } // // 独自の検証アイテムを作成します。 // public class CustomValidateItem : GrapeCity.Win.Editors.Validator<Control>.ValidateItem { protected override bool Validate() { GrapeCity.Win.Editors.GcTextBox targetControl = (GrapeCity.Win.Editors.GcTextBox)this.Target; // 入力された文字数が10文字以下の場合には無効とします。 if (targetControl.TextLength <= 10) { return false; } return true; } } }
数値検証コンポーネントのカスタマイズ
数値検証コンポーネントに、以下の設定を行う例です。
検証アイテム : InvalidRange
検証アクション :無効の場合には値を[0]に設定
[Visual Basic]
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' 検証アイテム(InvalidRange)を設定します。 Dim InvalidRange1 As New GrapeCity.Win.Editors.GcNumberValidator.InvalidRange() InvalidRange1.MaxValue = 100 InvalidRange1.MinValue = 0 GcNumberValidator1.GetValidateItems(GcNumber1).Add(InvalidRange1) ' 独自の検証アクションを設定します。 Dim ValidateAction1 As New CustomValidateAction() GcNumberValidator1.GetValidateActions(GcNumber1).Add(ValidateAction1) End Sub End Class '' '' 独自の検証アクションを作成します。 '' Public Class CustomValidateAction Inherits GrapeCity.Win.Editors.ValidateAction Protected Overrides Sub DoAction(ByVal context As GrapeCity.Win.Editors.ValidateContext) '' 値が無効な場合には値を[0]にします。 If Not context.IsValid Then Dim targetcontext As GrapeCity.Win.Editors.GcNumber = DirectCast(context.Target, GrapeCity.Win.Editors.GcNumber) targetcontext.Value = 0 End If End Sub End Class
[C#]
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // 検証アイテム(InvalidRange)を設定します。 GrapeCity.Win.Editors.GcNumberValidator.InvalidRange invalidRange1 = new GrapeCity.Win.Editors.GcNumberValidator.InvalidRange(); invalidRange1.MaxValue = 100; invalidRange1.MinValue = 0; gcNumberValidator1.GetValidateItems(gcNumber1).Add(invalidRange1); // 独自の検証アクションを設定します。 CustomValidateAction validateAction1 = new CustomValidateAction(); gcNumberValidator1.GetValidateActions(gcNumber1).Add(validateAction1); } } // // 独自の検証アクションを作成します。 // public class CustomValidateAction : GrapeCity.Win.Editors.ValidateAction { protected override void DoAction(GrapeCity.Win.Editors.ValidateContext context) { // 値が無効な場合には値を[0]にします。 if (!context.IsValid) { GrapeCity.Win.Editors.GcNumber targetcontext = (GrapeCity.Win.Editors.GcNumber)context.Target; targetcontext.Value = 0; } } } }
日付検証コンポーネントのカスタマイズ
日付検証コンポーネントに、以下の設定を行う例です。
検証アイテム : 入力された日付が月末の場合は無効
検証アクション :無効の場合には値を翌日に設定
[Visual Basic]
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' 独自の検証アイテムを設定します。 Dim ValidateItem1 As New CustomValidateItem() GcDateValidator1.GetValidateItems(GcDate1).Add(ValidateItem1) ' 独自の検証アクションを設定します。 Dim ValidateAction1 As New CustomValidateAction() GcDateValidator1.GetValidateActions(GcDate1).Add(ValidateAction1) End Sub End Class '' '' 独自の検証アイテムを作成します。 '' Public Class CustomValidateItem Inherits GrapeCity.Win.Editors.GcDateValidator.ValidateItem Protected Overrides Function Validate() As Boolean Dim targetControl As GrapeCity.Win.Editors.GcDate = DirectCast(Me.Target, GrapeCity.Win.Editors.GcDate) ' 入力された日付が月末の場合には無効とします。 If targetControl.Value.Value.Day = DateTime.DaysInMonth(targetControl.Value.Value.Year, targetControl.Value.Value.Month) Then Return False End If Return True End Function End Class '' '' 独自の検証アクションを作成します。 '' Public Class CustomValidateAction Inherits GrapeCity.Win.Editors.ValidateAction Protected Overrides Sub DoAction(ByVal context As GrapeCity.Win.Editors.ValidateContext) '' 値が無効な場合には翌日を設定します。 If Not context.IsValid Then Dim targetcontext As GrapeCity.Win.Editors.GcDate = DirectCast(context.Target, GrapeCity.Win.Editors.GcDate) targetcontext.Value = targetcontext.Value.Value.AddDays(1) End If End Sub End Class
[C#]
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // 独自の検証アイテムを設定します。 CustomValidateItem validateItem1 = new CustomValidateItem(); gcDateValidator1.GetValidateItems(gcDate1).Add(validateItem1); // 独自の検証アクションを設定します。 CustomValidateAction validateAction1 = new CustomValidateAction(); gcDateValidator1.GetValidateActions(gcDate1).Add(validateAction1); } } // // 独自の検証アイテムを作成します。 // public class CustomValidateItem : GrapeCity.Win.Editors.GcDateValidator.ValidateItem { protected override bool Validate() { GrapeCity.Win.Editors.GcDate targetControl = (GrapeCity.Win.Editors.GcDate)this.Target; // 入力された日付が月末の場合には無効とします。 if (targetControl.Value.Value.Day == DateTime.DaysInMonth(targetControl.Value.Value.Year,targetControl.Value.Value.Month)) { return false; } return true; } } // // 独自の検証アクションを作成します。 // public class CustomValidateAction : GrapeCity.Win.Editors.ValidateAction { protected override void DoAction(GrapeCity.Win.Editors.ValidateContext context) { // 値が無効な場合には翌日を設定します。 if (!context.IsValid) { GrapeCity.Win.Editors.GcDate targetcontext = (GrapeCity.Win.Editors.GcDate)context.Target; targetcontext.Value = targetcontext.Value.Value.AddDays(1); } } } }