PowerTools CalendarGrid for Windows Forms 1.0J
入力可能な範囲を設定する(CalendarGcNumberCellType)

CalendarGcNumberCellTypeに入力範囲を設定する方法について説明します。


MaxValue、MinValueプロパティをを使用

CalendarGcNumberCellType.MaxValue、CalendarGcNumberCellType.MinValue、CalendarGcNumberCellType.MaxMinBehaviorプロパティを使用します。

セルのMaxValueプロパティとMinValueプロパティを設定してセルに入力可能な範囲を指定します。値が入力されると検証が行われ、範囲外の場合にはMaxMinBehaviorプロパティの設定によって、値が制御されます。

【設定手順】

  1. CalendarGcNumberCellTypeのMaxValue、MinValueプロパティに最大値、最小値を設定します。
  2. CalendarGcNumberCellTypeのMaxMinBehaviorプロパティを設定します。既定値はAdjustToMaxMinです。

    MaxMinBehaviorの値 説明
    AdjustToMaxMin 値を最小値か最大値の近い方に設定します。
    Clear 値を削除してnull にします。
    Restore 変更前の値に戻します。
    CancelInput 最後の入力をキャンセルしてフォーカスを保持します。
    Keep エラーとなったText プロパティの値を保持します。

MaxMinBehaviorプロパティをCancelInputに設定した場合、範囲外の最後の入力を行った時点でGcNumberEditingControl.InvalidInputイベントが発生します。
InvalidInputイベント側では入力された値が範囲外であることを取得できるため、メッセージを出力するなどのカスタマイズが可能です。

以下のサンプルコードは、範囲外の値が入力されたとき、メッセージボックスを表示する例です。

Imports GrapeCity.Win.CalendarGrid
Imports InputManCell = GrapeCity.Win.CalendarGrid.InputMan
Imports CalendarGridInputMan = GrapeCity.Win.CalendarGrid.Editors

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim today As DateTime = DateTime.Today

    Dim GcNumberCellType As New InputManCell.CalendarGcNumberCellType()
    ' 最大値と最小値を設定します。
    GcNumberCellType.MaxValue = 100
    GcNumberCellType.MinValue = 0

    ' 範囲外の場合に値をどのように制御するかを設定します。
    GcNumberCellType.MaxMinBehavior = InputManCell.MaxMinBehavior.CancelInput

    GcCalendarGrid1.Content(today).Rows(1).Cells(0).CellType = GcNumberCellType
    GcCalendarGrid1.ScrollIntoView(today)

    AddHandler GcCalendarGrid1.EditingControlShowing, AddressOf GcCalendarGrid1_EditingControlShowing
End Sub

Private Sub GcCalendarGrid1_EditingControlShowing(sender As Object, e As CalendarEditingControlShowingEventArgs)
    If TypeOf e.Control Is CalendarGridInputMan.GcNumber Then
        Dim editor As CalendarGridInputMan.GcNumber = TryCast(e.Control, CalendarGridInputMan.GcNumber)

        RemoveHandler editor.InvalidInput, AddressOf editor_InvalidInput
        AddHandler editor.InvalidInput, AddressOf editor_InvalidInput
    End If
End Sub

Private Sub editor_InvalidInput(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim InvalidInputEventArgs As CalendarGridInputMan.InvalidInputEventArgs = DirectCast(e, CalendarGridInputMan.InvalidInputEventArgs)
    If InvalidInputEventArgs Is Nothing Then
        Return
    End If

    ' 値が範囲外の場合には、メッセージを表示します。     
    If InvalidInputEventArgs.ValueOutOfRange Then
        MessageBox.Show("範囲外の値です。")
    End If
End Sub
using GrapeCity.Win.CalendarGrid;
using InputManCell = GrapeCity.Win.CalendarGrid.InputMan;
using CalendarGridInputMan = GrapeCity.Win.CalendarGrid.Editors;

private void Form1_Load(object sender, EventArgs e)
{
    var today = DateTime.Today;
    var gcNumberCellType = new InputManCell.CalendarGcNumberCellType();

    // 最大値と最小値を設定します。
    gcNumberCellType.MaxValue = 100;
    gcNumberCellType.MinValue = 0;

    // 範囲外の場合に値をどのように制御するかを設定します。
    gcNumberCellType.MaxMinBehavior = InputManCell.MaxMinBehavior.CancelInput;

    gcCalendarGrid1.Content[today].Rows[1].Cells[0].CellType = gcNumberCellType;
    gcCalendarGrid1.ScrollIntoView(today);

    gcCalendarGrid1.EditingControlShowing += gcCalendarGrid1_EditingControlShowing;
}

private void gcCalendarGrid1_EditingControlShowing(object sender, CalendarEditingControlShowingEventArgs e)
{
    if (e.Control is CalendarGridInputMan.GcNumber)
    {
        CalendarGridInputMan.GcNumber editor = (CalendarGridInputMan.GcNumber)e.Control;

        editor.InvalidInput -= editor_InvalidInput;
        editor.InvalidInput += editor_InvalidInput;
    }
}

void editor_InvalidInput(object sender, CalendarGridInputMan.InvalidInputEventArgs e)
{
    CalendarGridInputMan.InvalidInputEventArgs invalidInputEventArgs = e as CalendarGridInputMan.InvalidInputEventArgs;

    if (invalidInputEventArgs == null)
    {
        return;
    }

    // 値が範囲外の場合には、メッセージを表示します。
    if (invalidInputEventArgs.ValueOutOfRange)
    {
        MessageBox.Show("範囲外の値です。");
    }
}
参照

 

 


© 2014 GrapeCity inc. All rights reserved.