GcDateTimeCellでは書式設定により、日付や時刻の入力および表示書式を自由に設定することができます。入力書式は、セルが入力フォーカスを受け取ったときの書式で、表示書式は入力フォーカスのないときの書式です。書式には和暦を使用することも可能です。
入力書式の設定は、
Fieldsプロパティによって行われ、フィールドとよばれる入力領域を構成する要素によって定義されます。また、表示書式は、
DisplayFieldsプロパティによって行われ、Fieldsプロパティと同様フィールドによって定義します。 FieldsプロパティおよびDisplayFieldsプロパティについては、「
書式の設定」で詳しく解説します。
セルのMaxDateプロパティとMinDateプロパティを設定してセルに入力可能な日付範囲を指定することができます。
すべてのフィールドの値が入力されると検証が行われ、範囲外の日付の場合にはMaxMinBehaviorプロパティの設定によって、値が制御されます。MaxMinBehaviorプロパティに設定できる値は以下のとおりで、既定値はMaxMinBehavior.AdjustToMaxMinです。
MaxMinBehaviorの値 |
説明 |
AdjustToMaxMin |
値を最小値か最大値の近い方に設定します。 |
Clear |
値を削除してnull にします。 |
Restore |
変更前の値に戻します。 |
CancelInput |
最後の入力をキャンセルしてフォーカスを保持します。 |
Keep |
エラーとなったText プロパティの値を保持します。 |
MaxMinBehaviorプロパティを
MaxMinBehavior.CancelInputに設定した場合、範囲外の日付の最後の入力を行った時点でGcDateTimeEditingControl.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 GcDateTimeCell1 As New InputManCell.GcDateTimeCell()
' 最大日付と最小日付を設定します。
GcDateTimeCell1.MaxDate = DateTime.Parse("2100/12/31 23:59:59")
GcDateTimeCell1.MinDate = DateTime.Parse("2000/1/1 0:00:00")
' 範囲外の場合に値をどのように制御するかを設定します。
GcDateTimeCell1.MaxMinBehavior = GrapeCity.Win.Editors.MaxMinBehavior.CancelInput
GcMultiRow1.Template = Template.CreateGridTemplate(New Cell() {GcDateTimeCell1})
End Sub
Private Sub GcMultiRow1_EditingControlShowing(sender As Object, e As EditingControlShowingEventArgs) Handles GcMultiRow1.EditingControlShowing
If (TypeOf (e.Control) Is InputManCell.GcDateTimeEditingControl) Then
Dim editor As InputManCell.GcDateTimeEditingControl = DirectCast(e.Control, InputManCell.GcDateTimeEditingControl)
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.GcDateTimeCell gcDateTimeCell1 = new InputManCell.GcDateTimeCell();
// 最大日付と最小日付を設定します。
gcDateTimeCell1.MaxDate = DateTime.Parse("2100/12/31 23:59:59");
gcDateTimeCell1.MinDate = DateTime.Parse("2000/1/1 0:00:00");
// 範囲外の場合に値をどのように制御するかを設定します。
gcDateTimeCell1.MaxMinBehavior = GrapeCity.Win.Editors.MaxMinBehavior.CancelInput;
gcMultiRow1.Template = Template.CreateGridTemplate(new Cell[] { gcDateTimeCell1 });
gcMultiRow1.EditingControlShowing += new EventHandler<EditingControlShowingEventArgs>(gcMultiRow1_EditingControlShowing);
}
private void gcMultiRow1_EditingControlShowing(object sender, EditingControlShowingEventArgs e)
{
if (e.Control is InputManCell.GcDateTimeEditingControl)
{
InputManCell.GcDateTimeEditingControl editor = (InputManCell.GcDateTimeEditingControl)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("範囲外の値です。");
}
}
Valueプロパティを使えば、リテラル文字列とプロンプト文字列を除いたセル内の基となる値をDateTime型で取得または設定できます。
また、GcDateTimeEditingContro.Numberプロパティを使えば、リテラル文字列とプロンプト文字列を除いたセル内の日付を長整数型で取得または設定できます。たとえば、Text プロパティに「平成 27年10月1日」が設定されているときには、Numberプロパティの値は「20151001000000」となります。Numberプロパティが変更されると、その変更はValue プロパティにも適用されます。
クリップボードにリテラル文字を含まない値を渡すには、ClipContentプロパティとGcDateTimeEditingContro.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 GcDateTimeCell1 = New InputManCell.GcDateTimeCell()
' セルに値を設定して、クリップボードにコピーします。
GcDateTimeCell1.Fields.Clear()
GcDateTimeCell1.Fields.AddRange("ggg ee年 MM月 dd日 hh時 mm分 ss秒")
GcDateTimeCell1.ClipContent = GrapeCity.Win.Editors.ClipContent.ExcludeLiterals
Dim TextBoxCell1 As New TextBoxCell()
' MultiRowのテンプレートを設定します。
GcMultiRow1.Template = Template.CreateGridTemplate(New Cell() {GcDateTimeCell1, 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.GcDateTimeEditingControl Then
' GcCharMaskCellの編集用コントロールが表示された場合に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.F8 Then
Dim editor As InputManCell.GcDateTimeEditingControl = DirectCast(sender, InputManCell.GcDateTimeEditingControl)
' Numberプロパティを使用して値を設定します。
editor.Number = 20091001000000
' クリップボードにコピーします。
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.GcDateTimeCell gcDateTimeCell1 = new InputManCell.GcDateTimeCell();
// セルに値を設定して、クリップボードにコピーします。
gcDateTimeCell1.Fields.Clear();
gcDateTimeCell1.Fields.AddRange("ggg ee年 MM月 dd日 hh時 mm分 ss秒");
gcDateTimeCell1.ClipContent = GrapeCity.Win.Editors.ClipContent.ExcludeLiterals;
TextBoxCell textBoxCell1 = new TextBoxCell();
// MultiRowのテンプレートを設定します。
gcMultiRow1.Template = Template.CreateGridTemplate(new Cell[] { gcDateTimeCell1, textBoxCell1 });
gcMultiRow1.RowCount = 5;
gcMultiRow1.EditingControlShowing += new EventHandler<EditingControlShowingEventArgs>(gcMultiRow1_EditingControlShowing);
}
private void gcMultiRow1_EditingControlShowing(object sender, EditingControlShowingEventArgs e)
{
if (e.Control is InputManCell.GcDateTimeEditingControl)
{
// 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.F8)
{
InputManCell.GcDateTimeEditingControl editor = (InputManCell.GcDateTimeEditingControl)sender;
// Numberプロパティを使用して値を設定します。
editor.Number = 20091001000000;
// クリップボードにコピーします。
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();
}
}
年月だけ、あるいば時刻部分のみ入力を許可しているなど、非表示に設定されたフィールドが存在する場合、そのデフォルト値には、RecommendedValueプロパティの値が設定されます。非表示フィールドの値は通常、Valueプロパティから取得しますが、Value プロパティがnull参照 (Visual BasicではNothing)の場合は、RecommendedValueプロパティに設定された値を非表示フィールドに設定します。
RecommendedValueプロパティの既定値はnull参照 (Visual BasicではNothing)です。この場合、デフォルト値にはアプリケーション実行後に値がクリアされた日付、時刻が設定されます。
以下のサンプルコードでは、年月だけの入力を許可してRecommendedValueプロパティにデフォルト値を設定しています。この場合、年月の入力が行われると、非表示になっている日と時分秒の部分には、RecommendedValueプロパティに設定された日と時分秒の値が設定されます。
Imports GrapeCity.Win.MultiRow
Imports InputManCell = GrapeCity.Win.MultiRow.InputMan
Dim GcDateTimeCell1 = New InputManCell.GcDateTimeCell()
' フィールドをクリアします。
GcDateTimeCell1.Fields.Clear()
' キーワードから書式を設定します。
GcDateTimeCell1.Fields.AddRange("yyyy/MM")
' RecommendedValueプロパティに非表示フィールドのデフォルト値を設定します。
GcDateTimeCell1.RecommendedValue = New DateTime(2000, 1, 1, 12, 0, 0)
' ValueプロパティにNothingを設定します。
GcDateTimeCell1.Value = Nothing
GcMultiRow1.Template = Template.CreateGridTemplate(New Cell() {GcDateTimeCell1})
GcMultiRow1.RowCount = 5
using GrapeCity.Win.MultiRow;
using GrapeCity.Win.MultiRow.InputMan;
InputManCell.GcDateTimeCell gcDateTimeCell1 = new InputManCell.GcDateTimeCell();
// フィールドをクリアします。
gcDateTimeCell1.Fields.Clear();
// キーワードから書式を設定します。
gcDateTimeCell1.Fields.AddRange("yyyy/MM");
// RecommendedValueプロパティに非表示フィールドのデフォルト値を設定します。
gcDateTimeCell1.RecommendedValue = new DateTime(2000, 1, 1, 12, 0, 0);
// Valueプロパティにnullを設定します。
gcDateTimeCell1.Value = null;
gcMultiRow1.Template = Template.CreateGridTemplate(new Cell[] { gcDateTimeCell1 });
AcceptsCrLfプロパティを使用してクリップボードへ改行を含む文字列をコピー、または貼り付けた場合の改行コードの扱いを設定できます。AcceptsCrLfプロパティは、CrLfMode 列挙体を使用して次の値を設定できます。
AcceptsCrLfの値 |
説明 |
NoControl |
改行コードはそのままでコピー、貼り付けを行います。 |
Filter |
全ての改行コードを削除しコピー、貼り付けを行います。 |
Cut |
最初の改行コード以降の文字列を削除します。標準コントロールと同じ動作です。 |
DefaultActiveField プロパティを使用すると、セルがフォーカスを受け取ったときにカレットを配置するフィールドを指定できます。
現在カレットが置かれているフィールドは、GcDateTimeEditingControl.ActiveFieldプロパティで取得できます。
セルに文字列を入力するとGcDateTimeEditingControl.TextChangingイベントが、GcDateTimeEditingControl.TextChangedイベントの前(入力された文字列がText プロパティに渡される前)に発生します。そのため、このイベント内で入力文字列をチェックすれば、Textプロパティの値に影響を与えることなく、入力を制御できます。
ExitOnLastCharプロパティをTrueに設定すると、入力された文字列が、 書式設定により定義されたフィールドすべてに満たされると、自動的に次のコントロールへフォーカスを移動できます。
GcDateTimeCellの表示領域は、通常、リテラル文字列と入力フィールドの2種類に分かれています。フィールドについては「書式の設定」を参照してください。

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