CalendarGcDateTimeCellTypeの基本的な使い方について説明します。
書式の設定
CalendarGcDateTimeCellType.Fieldsプロパティを使用します。
フィールド
CalendarGcDateTimeCellTypeでは書式設定により、日付や時刻の入力および表示書式を自由に設定することができます。入力書式は、セルが入力フォーカスを受け取ったときの書式で、表示書式は入力フォーカスのないときの書式です。書式には和暦を使用することも可能です。
入力書式の設定は、Fieldsプロパティによって行われ、フィールドとよばれる入力領域を構成する要素によって定義されます。また、表示書式は、DisplayFieldsプロパティによって行われ、Fieldsプロパティと同様フィールドによって定義します。FieldsプロパティおよびDisplayFieldsプロパティについては、「書式を設定する」で詳しく解説します。
CalendarGridのデザイナ上で書式を設定するには、入力書式・表示書式共に、次の2通りのデザイン機能が用意されています。
- フィールドコレクションエディタを使用する
- キーワードから書式を生成する
フィールドコレクションエディタを使用する
CalendarGcDateTimeCellTypeで設定可能なフィールドを組み合わせて書式を設定する方法です。次のいずれかの方法で設定することができます。
入力書式
- プロパティウィンドウのFieldsプロパティから開かれる[フィールドコレクションエディタ]
表示書式
- プロパティウィンドウのDisplayFieldsプロパティから開かれる[フィールドコレクションエディタ]
これらのデザイン機能では、コレクションにフィールドを追加すると同時に各フィールドのテキストのフォントや色といったスタイルを設定することができます。

(図)フィールド毎に異なったスタイルを設定
キーワードから書式を生成する
キーワードを設定することで書式を設定します。個々のフィールドのプロパティは設定できませんが、一般的な日付のフォーマットが予めビルトイン書式として用意されているため、これらのリストから書式を選択するだけで簡単に書式を定義することが可能です。
キーワードから書式を設定するには、CalendarGridデザイナでCalendarGcDateTimeCellTypeが設定されているセルを選択してプロパティウィンドウのCellType.Fieldsプロパティから「入力フィールドの編集」ダイアログを開き、ツールボタンの右端の「書式を指定してフィールドを設定」をクリックして、ドロップダウンリストからキーワードを選択します。表示書式の場合はCellType.DisplayFieldsプロパティを使用します。
キーワードを使って設定した書式は、フィールドオブジェクトのコレクションに変換されます。フィールドオブジェクトに変換された後はフィールドエディタで編集できるようになります。
非表示に設定したフィールドのデフォルト値
CalendarGcDateTimeCellTypeの非表示に設定されたフィールドのデフォルト値には、RecommendedValueプロパティの値が設定されます。非表示フィールドの値は通常、Valueプロパティから取得しますが、Valueプロパティがnull参照 (Visual BasicではNothing)の場合は、RecommendedValueプロパティに設定された値を非表示フィールドに設定します。
RecommendedValueプロパティの既定値はnull参照(Visual BasicではNothing)です。この場合、デフォルト値にはアプリケーション実行後に値がクリアされた日付、時刻が設定されます。
リテラル文字とプロンプト文
CalendarGcDateTimeCellType.ShowLiteralsプロパティを使用します。
ShowLiteralsプロパティを使えば、入力中にリテラル文字列を表示するかどうかを指定できます。
リテラル文字とは、DateLiteralField(リテラルフィールド)で定義された文字列をそのまま表示する文字列をいいます。
また、入力パターンフィールドには、プロンプト文字を設定することができます。プロンプト文字はPromptCharプロパティを使用します。プロンプト文字を使用することで、入力フィールドを明示的に表示したり入力文字数を視覚的に表すことができます。
Valueプロパティを使えば、リテラル文字列とプロンプト文字列を除いたセルの基となる値をDateTime型で取得または設定できます。
また、GcDateTime.Numberプロパティを使えば、リテラル文字列とプロンプト文字列を除いたセル内の日付を長整数型で取得または設定できます。たとえば、Textプロパティに「平成 21年10月1日」が設定されているときには、Numberプロパティの値は「20091001000000」となります。Number プロパティが変更されると、その変更は Value プロパティにも適用されます。
クリップボードにリテラル文字を含まない値を渡すには、ClipContentプロパティとGcDateTime.SelectedTextプロパティを使用します。ClipContentプロパティで制御できるのは、SelectedTextプロパティの値のみなので、以下のプロパティの値をクリップボードに設定した場合は、ClipContentプロパティの設定は無効になります。なお、プロンプト文字列は、ClipContentプロパティの設定に関わらず常にクリップボードに渡されます。
次のサンプルコードは、ClipContentプロパティを使って、リテラル文字列を省いた文字列をクリップボードにコピーする方法を示します。
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 GcDateTimeCellType As New InputManCell.CalendarGcDateTimeCellType()
' セルに値を設定して、クリップボードにコピーします。
GcDateTimeCellType.Fields.Clear()
GcDateTimeCellType.Fields.AddRange("ggg ee年 MM月 dd日 hh時 mm分 ss秒")
GcDateTimeCellType.ClipContent = InputMan.ClipContent.ExcludeLiterals
Dim TextBoxCellType = New CalendarTextBoxCellType()
Dim Template As New CalendarTemplate()
Template.RowCount = 3
Template.ColumnHeader.Rows(0).Cells(0).DateFormat = "{DayOfWeek}"
Template.ColumnHeader.Columns(0).Width = 200
Template.Content.Rows(0).Cells(0).DateFormat = "{MonthDay}"
Template.Content.Rows(0).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(1).Cells(0).Name = "myCell1"
Template.Content.Rows(1).Cells(0).CellType = GcDateTimeCellType.Clone()
Template.Content.Rows(1).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(2).Cells(0).CellType = TextBoxCellType.Clone()
Template.Content.Rows(2).Cells(0).CellStyleName = "defaultStyle"
GcCalendarGrid1.Template = Template
AddHandler GcCalendarGrid1.EditingControlShowing, AddressOf GcCalendarGrid1_EditingControlShowing
End Sub
Private Sub GcCalendarGrid1_EditingControlShowing(sender As Object, e As CalendarEditingControlShowingEventArgs) Handles GcCalendarGrid1.EditingControlShowing
If TypeOf e.Control Is CalendarGridInputMan.GcDateTime Then
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 KeyEventArgs)
Dim cell As CalendarCell = GcCalendarGrid1(GcCalendarGrid1.CurrentCellPosition.Date) _
(GcCalendarGrid1.CurrentCellPosition.RowIndex, GcCalendarGrid1.CurrentCellPosition.ColumnIndex)
If e.KeyCode = Keys.F8 Then
Dim editor As CalendarGridInputMan.GcDateTime = DirectCast(sender, CalendarGridInputMan.GcDateTime)
' Numberプロパティを使用して値を設定します。
editor.Number = 20140601000000
' クリップボードにコピーします。
editor.SelectionStart = 0
editor.SelectionLength = editor.Text.Length
Clipboard.SetDataObject(editor.SelectedText)
' クリップボードのデータを取得して確認します。
Dim cbData As IDataObject = Clipboard.GetDataObject()
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 gcDateTimeCellType = new InputManCell.CalendarGcDateTimeCellType();
// セルに値を設定して、クリップボードにコピーします。
gcDateTimeCellType.Fields.Clear();
gcDateTimeCellType.Fields.AddRange("ggg ee年 MM月 dd日 hh時 mm分 ss秒");
gcDateTimeCellType.ClipContent = InputManCell.ClipContent.ExcludeLiterals;
var textBoxCellType = new CalendarTextBoxCellType();
var template = new CalendarTemplate();
template.RowCount = 3;
template.ColumnHeader.Rows[0].Cells[0].DateFormat = "{DayOfWeek}";
template.ColumnHeader.Columns[0].Width = 200;
template.Content.Rows[0].Cells[0].DateFormat = "{MonthDay}";
template.Content.Rows[0].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[1].Cells[0].Name = "myCell1";
template.Content.Rows[1].Cells[0].CellType = gcDateTimeCellType.Clone();
template.Content.Rows[1].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[2].Cells[0].CellType = textBoxCellType.Clone();
template.Content.Rows[2].Cells[0].CellStyleName = "defaultStyle";
gcCalendarGrid1.Template = template;
gcCalendarGrid1.EditingControlShowing += gcCalendarGrid1_EditingControlShowing;
}
private void gcCalendarGrid1_EditingControlShowing(object sender, CalendarEditingControlShowingEventArgs e)
{
if (e.Control is CalendarGridInputMan.GcDateTime)
{
e.Control.KeyDown -= new KeyEventHandler(Editor_KeyDown);
e.Control.KeyDown += new KeyEventHandler(Editor_KeyDown);
}
}
private void Editor_KeyDown(object sender, KeyEventArgs e)
{
CalendarCell cell = gcCalendarGrid1[gcCalendarGrid1.CurrentCellPosition.Date]
[gcCalendarGrid1.CurrentCellPosition.RowIndex, gcCalendarGrid1.CurrentCellPosition.ColumnIndex];
if (e.KeyCode == Keys.F8)
{
var editor = sender as CalendarGridInputMan.GcDateTime;
// Numberプロパティを使用して値を設定します。
editor.Number = 20140601000000;
// クリップボードにコピーします。
editor.SelectionStart = 0;
editor.SelectionLength = editor.Text.Length;
Clipboard.SetDataObject(editor.SelectedText);
// クリップボードのデータを取得して確認します。
IDataObject cbData = Clipboard.GetDataObject();
}
}
編集モードの切り替え
CalendarGcDateTimeCellType.EditModeプロパティを使用します。
EditModeプロパティを使って、セルがフォーカスを受け取ったときのデフォルトの編集モードを定義できます。EditModeプロパティをEditMode.Insertにすると挿入モード、EditMode.Overwriteにすると上書きモードになります。また、EditMode.FixedInsertとEditMode.FixedOverwriteでは、編集モードが固定されるので、実行中に[Ins]キーが押されても編集モードは切り替わりません。
セルの編集時では、EditModeプロパティがEditMode.InsertまたはEditMode.Overwriteに設定されている場合、編集モードが[Ins]キーまたはEditModeプロパティによって変更されたときには、GcDateTime.EditStatusChangedイベントが発生します。また、フォーカス取得時の編集モードは、Overwriteプロパティを使って調べることができます。
次のサンプルコードは、GcDateTime.KeyDownイベントで[Alt]キーおよび[C]キーが押下されたとき、GcDateTime.Overwriteプロパティを使用して編集モードを取得します。
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 GcDateTimeCellType As New InputManCell.CalendarGcDateTimeCellType()
GcDateTimeCellType.EditMode = InputMan.EditMode.Insert
Dim Template As New CalendarTemplate()
Template.RowCount = 3
Template.ColumnHeader.Rows(0).Cells(0).DateFormat = "{DayOfWeek}"
Template.ColumnHeader.Columns(0).Width = 200
Template.Content.Rows(0).Cells(0).DateFormat = "{MonthDay}"
Template.Content.Rows(0).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(1).Cells(0).Name = "myCell1"
Template.Content.Rows(1).Cells(0).CellType = GcDateTimeCellType.Clone()
Template.Content.Rows(1).Cells(0).CellStyleName = "defaultStyle"
GcCalendarGrid1.Template = Template
AddHandler GcCalendarGrid1.EditingControlShowing, AddressOf GcCalendarGrid1_EditingControlShowing
End Sub
Private Sub GcCalendarGrid1_EditingControlShowing(sender As Object, e As CalendarEditingControlShowingEventArgs) Handles GcCalendarGrid1.EditingControlShowing
If TypeOf e.Control Is CalendarGridInputMan.GcDateTime Then
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 KeyEventArgs)
Dim editor As CalendarGridInputMan.GcDateTime = DirectCast(sender, CalendarGridInputMan.GcDateTime)
If e.Alt AndAlso e.KeyCode = Keys.C Then
If editor.OverWrite = True Then
Console.WriteLine("上書き")
Else
Console.WriteLine("挿入")
End If
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 gcDateTimeCellType = new InputManCell.CalendarGcDateTimeCellType();
gcDateTimeCellType.EditMode = InputManCell.EditMode.Insert;
var template = new CalendarTemplate();
template.RowCount = 3;
template.ColumnHeader.Rows[0].Cells[0].DateFormat = "{DayOfWeek}";
template.ColumnHeader.Columns[0].Width = 200;
template.Content.Rows[0].Cells[0].DateFormat = "{MonthDay}";
template.Content.Rows[0].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[1].Cells[0].Name = "myCell1";
gcCalendarGrid1.Template = template;
gcCalendarGrid1.EditingControlShowing += gcCalendarGrid1_EditingControlShowing;
}
private void gcCalendarGrid1_EditingControlShowing(object sender, CalendarEditingControlShowingEventArgs e)
{
if (e.Control is CalendarGridInputMan.GcTextBox)
{
e.Control.KeyDown -= new KeyEventHandler(Editor_KeyDown);
e.Control.KeyDown += new KeyEventHandler(Editor_KeyDown);
}
}
private void Editor_KeyDown(object sender, KeyEventArgs e)
{
var editor = sender as CalendarGridInputMan.GcTextBox;
if (e.Alt && e.KeyCode == Keys.C)
{
if (editor.OverWrite == true)
{
Console.WriteLine("上書き");
}
else
{
Console.WriteLine("挿入");
}
}
}
フィールドの入力モード
CalendarGcDateTimeCellType.FieldsEditModeプロパティを使用します。
FieldsEditModeプロパティを使用すると、CalendarGcDateTimeCellTypeの各フィールドの入力モードを変更することができます。FieldsEditModeには次の3つの値を設定することができます。
FieldsEditModeの値 |
説明 |
LeftSide |
フィールドで値は左から右に入力されます。
 |
RightSide |
フィールドで値は右から左に入力されます。
 |
Unfixed |
フィールドで値は左から右に入力されます。 日付や時刻が1桁の値の場合は先頭に0を付加する必要はありません。
 (図) 月のフィールドに1を入力した場合、値は1月と認識されます。 |
イベントを使った文字列操作
GcDateTime.TextChangingイベントを使用します。
DefaultActiveFieldプロパティを使用すると、セルがフォーカスを受け取ったときにカレットを配置するフィールドを指定できます。
現在カレットが置かれているフィールドは、GcDateTime.
ActiveFieldプロパティで取得できます。
セルに文字列を入力するとGcDateTime.TextChangingイベントが、GcDateTime.TextChangedイベントの前(入力された文字列がTextプロパティに渡される前)に発生します。そのため、このイベント内で入力文字列をチェックすれば、Textプロパティの値に影響を与えることなく、入力を制御できます。
コンテキストメニューの表示
CalendarGridの他のセル型の場合と同様です。CalendarGcDateTimeCellType.ContextMenuStripプロパティを使用します。
関連トピック