GcTimeSpanCellでは入力可能な範囲を指定したり、マイナス値の扱いを制御することができます。ここではGcTimeSpanCellが提供する入力機能について解説します。
GcTimeSpanCellでは書式設定により、入力および表示書式を自由に設定することができます。入力書式は、セルが入力フォーカスを受け取ったときの書式で、表示書式は入力フォーカスのないときの書式です。
入力書式の設定は、Fieldsプロパティによって行われ、フィールドとよばれる入力領域を構成する要素によって定義されます。また、表示書式は、DisplayFieldsプロパティによって行われ、Fieldsプロパティと同様フィールドによって定義します。FieldsプロパティおよびDisplayFieldsプロパティについては、「書式の設定」で詳しく解説します。
セルのMaxValueプロパティとMinValueプロパティを設定してセルに入力可能な範囲を指定することができます。
これらのプロパティを使用する場合、検証は入力中のリアルタイムに行われます。全てのフィールドの値が入力されると検証が行われ、範囲外の値の場合にはMaxMinBehaviorプロパティの設定によって制御されます。MaxMinBehaviorプロパティに設定できる値は以下のとおりで、既定値はMaxMinBehavior.AdjustToMaxMinです。
MaxMinBehaviorの値 |
説明 |
AdjustToMaxMin |
値を最小値か最大値の近い方に設定します。 |
Clear |
値を削除してnull にします。 |
Restore |
変更前の値に戻します。 |
CancelInput |
最後の入力をキャンセルしてフォーカスを保持します。 |
Keep |
エラーとなったText プロパティの値を保持します。 |
MaxMinBehaviorプロパティをMaxMinBehavior.CancelInputに設定した場合、範囲外の最後の入力を行った時点でGcTimeSpanEditingControl.InvalidInputイベントが発生します。 InvalidInputイベント側では入力された値が範囲外であることを取得できるため、メッセージを出力するなどのカスタマイズが可能です。
以下のサンプルコードは、範囲外の値が入力されたとき、メッセージボックスを表示する例です。
Imports GrapeCity.Win.MultiRow
Imports InputManCell = GrapeCity.Win.MultiRow.InputMan
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim GcTimeSpanCell1 As New InputManCell.GcTimeSpanCell()
' 最大値と最小値を設定します。
GcTimeSpanCell1.MaxValue = TimeSpan.Parse("1.00:00:00")
GcTimeSpanCell1.MinValue = TimeSpan.Parse("00:00:00")
' 範囲外の場合に値をどのように制御するかを設定します。
GcTimeSpanCell1.MaxMinBehavior = GrapeCity.Win.Editors.MaxMinBehavior.CancelInput
GcMultiRow1.Template = Template.CreateGridTemplate(New Cell() {GcTimeSpanCell1})
End Sub
Private Sub GcMultiRow1_EditingControlShowing(sender As Object, e As EditingControlShowingEventArgs) Handles GcMultiRow1.EditingControlShowing
If (TypeOf (e.Control) Is InputManCell.GcTimeSpanEditingControl) Then
Dim editor As InputManCell.GcTimeSpanEditingControl = DirectCast(e.Control, InputManCell.GcTimeSpanEditingControl)
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 GrapeCity.Win.Editors.InvalidInputEventArgs = TryCast(e, GrapeCity.Win.Editors.InvalidInputEventArgs)
If InvalidInputEventArgs Is Nothing Then
Exit Sub
End If
' 値が範囲外の場合には、メッセージを表示します。
If InvalidInputEventArgs.ValueOutOfRange Then
MessageBox.Show("範囲外の値です。")
End If
End Sub
using GrapeCity.Win.MultiRow;
using InputManCell = GrapeCity.Win.MultiRow.InputMan;
private void Form1_Load(object sender, EventArgs e)
{
InputManCell.GcTimeSpanCell gcTimeSpanCell1 = new InputManCell.GcTimeSpanCell();
// 最大値と最小値を設定します。
gcTimeSpanCell1.MaxValue = TimeSpan.Parse("1.00:00:00");
gcTimeSpanCell1.MinValue = TimeSpan.Parse("00:00:00");
// 範囲外の場合に値をどのように制御するかを設定します。
gcTimeSpanCell1.MaxMinBehavior = GrapeCity.Win.Editors.MaxMinBehavior.CancelInput;
gcMultiRow1.Template = Template.CreateGridTemplate(new Cell[] { gcTimeSpanCell1 });
gcMultiRow1.EditingControlShowing += new EventHandler<EditingControlShowingEventArgs>(gcMultiRow1_EditingControlShowing);
}
private void gcMultiRow1_EditingControlShowing(object sender, EditingControlShowingEventArgs e)
{
if (e.Control is InputManCell.GcTimeSpanEditingControl)
{
InputManCell.GcTimeSpanEditingControl editor = (InputManCell.GcTimeSpanEditingControl)e.Control;
editor.InvalidInput -= new EventHandler(editor_InvalidInput);
editor.InvalidInput += new EventHandler(editor_InvalidInput);
}
}
private void editor_InvalidInput(object sender, EventArgs e)
{
GrapeCity.Win.Editors.InvalidInputEventArgs invalidInputEventArgs = e as GrapeCity.Win.Editors.InvalidInputEventArgs;
if (invalidInputEventArgs == null)
{
return;
}
// 値が範囲外の場合には、メッセージを表示します。
if (invalidInputEventArgs.ValueOutOfRange)
{
MessageBox.Show("範囲外の値です。");
}
}
セルの入力をプラスの値かマイナスの値のいずれかのみを許可したい場合、ValueSignプロパティを設定します。
プロパティ値 |
説明 |
NoControl |
数値の符号が設定されていません。正数、負数両方の入力を許可します。 |
Positive |
正数の入力のみを許可します。 |
Negative |
負数の入力のみを許可します。 |
マイナス値が入力されたとき、書式の設定「書式の設定」で表示する文字列を指定することが可能ですが、NegativeColorプロパティを使用すれば、マイナス値が入力された場合の文字色を設定することができます。また、UseNegativeColorプロパティで、指定した色を適用するかどうかを設定します。

(図) マイナス値の書式と色を設定したGcTimeSpanCell
マイナスキーは値をプラスとマイナスを切り替えるスイッチのような動作を行います。(1回目のマイナスキー押下で値がマイナスに変化、2回目押下でプラスに変化)この動作は、GcTimeSpanCell に以下のショートカット機能が設定されていることで実現されています。
- キー:OemMinus に対して ショートカット動作の SwitchSign
- キー:Subtract に対して ショートカット動作の SwitchSign
この動作を、マイナスキーが押下された場合には値をマイナスに変更するだけの動作とさせることもできます。(1回目のマイナスキー押下で値がマイナスに変化、2回目押下でもマイナスのまま。プラスにしたい場合はプラスキーを押下する)
既定の動作から新しい動作に変更するためには、上記で挙げたふたつのショートカット機能を削除します。
Valueプロパティを使えば、リテラル文字列とプロンプト文字列を除いたセルの基となる値をTimeSpan型で取得または設定できます。
クリップボードにリテラル文字を含まない値を渡すには、ClipContentプロパティとGcTimeSpanEditingControl.SelectedTextプロパティを使用します。ClipContentプロパティで制御できるのは、SelectedTextプロパティの値のみなので、以下のプロパティの値をクリップボードに設定した場合は、ClipContentプロパティの設定は無効になります。なお、プロンプト文字列は、ClipContentプロパティの設定に関わらず常にクリップボードに渡されます。
次のサンプルコードは、ClipContentプロパティを使って、リテラル文字列を省いた文字列をクリップボードにコピーする方法を示します。
Imports GrapeCity.Win.MultiRow
Imports InputManCell = GrapeCity.Win.MultiRow.InputMan
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim GcTimeSpanCell1 = New InputManCell.GcTimeSpanCell()
' セルに値を設定します。
GcTimeSpanCell1.Fields.Clear()
GcTimeSpanCell1.Fields.AddRange("hh時間mm分ss秒,0,.,,,-,")
GcTimeSpanCell1.Value = System.TimeSpan.Parse("12:21:00")
GcTimeSpanCell1.ClipContent = GrapeCity.Win.Editors.ClipContent.ExcludeLiterals
Dim TextBoxCell1 As New TextBoxCell()
' MultiRowのテンプレートを設定します。
GcMultiRow1.Template = Template.CreateGridTemplate(New Cell() {GcTimeSpanCell1, TextBoxCell1})
GcMultiRow1.RowCount = 5
End Sub
Private Sub GcMultiRow1_EditingControlShowing(sender As Object, e As EditingControlShowingEventArgs) Handles GcMultiRow1.EditingControlShowing
If TypeOf e.Control Is InputManCell.GcTimeSpanEditingControl Then
' GcTimeSpanCellの編集用コントロールが表示された場合にKeyDownイベントを設定します。
RemoveHandler e.Control.KeyDown, AddressOf Editor_KeyDown
AddHandler e.Control.KeyDown, AddressOf Editor_KeyDown
End If
End Sub
Private Sub Editor_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
Dim cell As Cell = GcMultiRow1.CurrentCell
If e.KeyCode = Keys.F5 Then
Dim editor As InputManCell.GcTimeSpanEditingControl = DirectCast(sender, InputManCell.GcTimeSpanEditingControl)
' クリップボードにコピーします。
editor.SelectionStart = 0
editor.SelectionLength = editor.Text.Length
Clipboard.SetDataObject(editor.SelectedText)
' クリップボードのデータを取得して確認します。
Dim cbData As IDataObject = Clipboard.GetDataObject()
GcMultiRow1(cell.RowIndex, cell.CellIndex + 1).Value = cbData.GetData(DataFormats.Text).ToString()
End If
End Sub
using GrapeCity.Win.MultiRow;
using InputManCell = GrapeCity.Win.MultiRow.InputMan;
private void Form1_Load(object sender, EventArgs e)
{
InputManCell.GcTimeSpanCell gcTimeSpanCell1 = new InputManCell.GcTimeSpanCell();
// セルに値を設定します。
gcTimeSpanCell1.Fields.Clear();
gcTimeSpanCell1.Fields.AddRange("hh時間mm分ss秒,0,.,,,-,");
gcTimeSpanCell1.Value = System.TimeSpan.Parse("12:21:00");
gcTimeSpanCell1.ClipContent = GrapeCity.Win.Editors.ClipContent.ExcludeLiterals;
TextBoxCell textBoxCell1 = new TextBoxCell();
// MultiRowのテンプレートを設定します。
gcMultiRow1.Template = Template.CreateGridTemplate(new Cell[] { gcTimeSpanCell1, textBoxCell1 });
gcMultiRow1.RowCount = 5;
gcMultiRow1.EditingControlShowing += new EventHandler<EditingControlShowingEventArgs>(gcMultiRow1_EditingControlShowing);
}
private void gcMultiRow1_EditingControlShowing(object sender, EditingControlShowingEventArgs e)
{
if (e.Control is InputManCell.GcTimeSpanEditingControl)
{
// GcCharMaskCellの編集用コントロールが表示された場合にKeyDownイベントを設定します。
e.Control.KeyDown -= new KeyEventHandler(Editor_KeyDown);
e.Control.KeyDown += new KeyEventHandler(Editor_KeyDown);
}
}
private void Editor_KeyDown(object sender, KeyEventArgs e)
{
Cell cell = gcMultiRow1.CurrentCell;
if (e.KeyCode == Keys.F5)
{
InputManCell.GcTimeSpanEditingControl editor = (InputManCell.GcTimeSpanEditingControl)sender;
// クリップボードにコピーします。
editor.SelectionStart = 0;
editor.SelectionLength = editor.Text.Length;
Clipboard.SetDataObject(editor.SelectedText);
// クリップボードのデータを取得して確認します。
IDataObject cbData = Clipboard.GetDataObject();
gcMultiRow1[cell.RowIndex, cell.CellIndex + 1].Value = cbData.GetData(DataFormats.Text).ToString();
}
}
AcceptsCrLfプロパティを使用してクリップボードへ改行を含む文字列をコピー、または貼り付けた場合の改行コードの扱いを設定できます。AcceptsCrLf プロパティは、CrLfMode列挙体を使用して次の値を設定できます。
AcceptsCrLfの値 |
説明 |
NoControl |
改行コードはそのままでコピー、貼り付けを行います。従来のInputManのタイムスパンコントロールと同じ動作です。 |
Filter |
全ての改行コードを削除しコピー、貼り付けを行います。 |
Cut |
最初の改行コード以降の文字列を削除します。標準コントロールと同じ動作です。 |
GcTimeSpanEditingControl.TextChanging イベントを使用します。
DefaultActiveFieldプロパティを使用すると、セルがフォーカスを受け取ったときにカレットを配置するフィールドを指定できます。
現在カレットが置かれているフィールドは、GcTimeSpanEditingControl.ActiveFieldプロパティで取得できます。
セルに文字列を入力するとGcTimeSpanEditingControl.TextChangingイベントが、TextChangedイベントの前(入力された文字列がTextプロパティに渡される前)に発生します。そのため、このイベント内で入力文字列をチェックすれば、Textプロパティの値に影響を与えることなく、入力を制御できます。
ExitOnLastCharプロパティをTrueに設定すると、書式設定により定義されたフィールドすべてに満たされると、自動的に次のコントロールへフォーカスを移動できます。
GcTimeSpanCellのの表示領域は、通常、リテラル文字列と入力フィールドの2種類に分かれています。フィールドについては「書式の設定」を参照してください。

TabActionプロパティをTabAction.Controlに設定すると、[Tab]キーまたは[Shift]+[Tab]キーによるフォーカス移動は、セル間で行われます。TabAction.Fieldに設定すると、フォーカス移動は入力フィールド間で行われます。また、GcTimeSpanCellのナビゲーションや選択などの操作は、リテラル文字列と入力フィールドの位置関係による影響を受けます。