PowerTools CalendarGrid for Windows Forms 1.0J
基本的な使い方(CalendarGcTimeSpanCellType)

CalendarGcTimeSpanCellTypeの基本的な使い方について説明します。


書式の設定

CalendarGcTimeSpanCellType.Fieldsプロパティおよび CalendarGcTimeSpanCellType.DisplayFieldsプロパティを使用します。

フィールド

CalendarGcTimeSpanCellTypeでは書式設定により、入力および表示書式を自由に設定することができます。入力書式は、セルが入力フォーカスを受け取ったときの書式で、表示書式は入力フォーカスのないときの書式です。
入力書式の設定は、Fieldsプロパティによって行われ、フィールドとよばれる入力領域を構成する要素によって定義されます。また、表示書式は、DisplayFieldsプロパティによって行われ、Fieldsプロパティと同様フィールドによって定義します。FieldsプロパティおよびDisplayFieldsプロパティについては、「書式を設定する」で詳しく解説します。

CalendarGridのデザイナ上で書式を設定するには、入力書式・表示書式共に、次の2通りのデザイン機能が用意されています。        

  1. フィールドコレクションエディタを使用する
  2. キーワードから書式を生成する

フィールドコレクションエディタを使用する

CalendarGcTimeSpanCellTypeで設定可能なフィールドを組み合わせて書式を設定する方法です。次のいずれかの方法で設定することができます。        

入力書式

表示書式


これらのデザイン機能では、コレクションにフィールドを追加すると同時に各フィールドのテキストのフォントや色といったスタイルを設定することができます。


(図)フィールド毎に異なったスタイルを設定

キーワードから書式を生成する

キーワードを設定することで書式を設定します。個々のフィールドのプロパティは設定できませんが、一般的な時間間隔のフォーマットが予めビルトイン書式として用意されているため、これらのリストから書式を選択するだけで簡単に書式を定義することが可能です。
キーワードから書式を設定するには、CalendarGridデザイナでCalendarGcTimeSpanCellTypeが設定されているセルを選択してプロパティウィンドウのCellType.Fieldsプロパティから「入力フィールドの編集」ダイアログを開き、ツールボタンの右端の「書式を指定してフィールドを設定」をクリックして、ドロップダウンリストからキーワードを選択します。表示書式の場合はCellType.DisplayFieldsプロパティを使用します。
キーワードを使って設定した書式は、フィールドオブジェクトのコレクションに変換されます。フィールドオブジェクトに変換された後はフィールドエディタで編集できるようになります。


マイナス値の設定と色

CalendarGcTimeSpanCellType.ValueSignプロパティおよび CalendarGcTimeSpanCellType.UseNegativeColorプロパティを使用します。

セルの入力をプラスの値かマイナスの値のいずれかのみを許可したい場合、ValueSignプロパティを設定します。

プロパティ値 説明

NoControl

数値の符号が設定されていません。正数、負数両方の入力を許可します。

Positive

正数の入力のみを許可します。

Negative

負数の入力のみを許可します。


マイナス値が入力されたとき、書式の設定「書式を設定する」で表示する文字列を指定することが可能ですが、NegativeColorプロパティを使用すれば、マイナス値が入力された場合の文字色を設定することができます。また、UseNegativeColorプロパティで、指定した色を適用するかどうかを設定します。


(図) マイナス値の書式と色を設定したCalendarGcTimeSpanCellType


マイナスキーの動作

CalendarGcTimeSpanCellType.ShortcutKeys プロパティを使用します。

マイナスキーは値をプラスとマイナスを切り替えるスイッチ機能となっています。(1回目のマイナスキー押下で値がマイナスに変化、2回目押下でプラスに変化) デフォルトではスイッチ機能のまま動作します。
このデフォルトの動作は、CalendarGcTimeSpanCellType に以下のショートカット機能が設定されていることで実現されています。

この動作を、マイナスキーが押下された場合には値をマイナスに変更するだけの動作とさせることもできます。(1回目のマイナスキー押下で値がマイナスに変化、2回目押下でもマイナスのまま。プラスにしたい場合はプラスキーを押下する)
デフォルトの動作から新しい動作に変更するためには、上記で挙げたふたつのショートカット機能を削除します。    


リテラル文字とプロンプト文字

CalendarGcTimeSpanCellType.ShowLiterals プロパティを使用します。

ShowLiteralsプロパティを使えば、入力中にリテラル文字列を表示するかどうかを指定できます。
リテラル文字とは、TimeSpanLiteralField(リテラルフィールド)で定義された文字列をそのまま表示する文字列をいいます。

また、入力パターンフィールドには、プロンプト文字を設定することができます。プロンプト文字はPromptCharプロパティを使用します。プロンプト文字を使用することで、入力フィールドを明示的に表示したり入力文字数を視覚的に表すことができます。

Valueプロパティを使えば、リテラル文字列とプロンプト文字列を除いたセルの基となる値をTimeSpan型で取得または設定できます。

クリップボードにリテラル文字を含まない値を渡すには、ClipContentプロパティとGcTimeSpan.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 GcTimeSpanCellType As New InputManCell.CalendarGcTimeSpanCellType()
    ' セルに値を設定します。
    GcTimeSpanCellType.Fields.Clear()
    GcTimeSpanCellType.Fields.AddRange("hh時間mm分ss秒,0,.,,,-,")
    GcTimeSpanCellType.ClipContent = InputMan.ClipContent.ExcludeLiterals

    Dim Template As New CalendarTemplate()
    Template.RowCount = 3
    Template.ColumnHeader.Rows(0).Cells(0).DateFormat = "{DayOfWeek}"
    Template.ColumnHeader.Columns(0).Width = 120
    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 = GcTimeSpanCellType.Clone()
    Template.Content.Rows(1).Cells(0).CellStyleName = "defaultStyle"
    Template.Content.Rows(1).Cells(0).Value = System.TimeSpan.Parse("12:21:00")

    GcCalendarGrid1.Template = Template

    AddHandler GcCalendarGrid1.EditingControlShowing, AddressOf GcCalendarGrid1_EditingControlShowing
End Sub

Private Sub GcCalendarGrid1_EditingControlShowing(sender As Object, e As CalendarEditingControlShowingEventArgs)
    If TypeOf e.Control Is CalendarGridInputMan.GcTimeSpan 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)
    Dim editor As CalendarGridInputMan.GcTimeSpan = DirectCast(sender, CalendarGridInputMan.GcTimeSpan)

    If e.KeyCode = Keys.F5 Then
        ' クリップボードにコピーします。
        editor.SelectionStart = 0
        editor.SelectionLength = editor.Text.Length
        Clipboard.SetDataObject(editor.SelectedText)

        ' クリップボードのデータを取得して確認します。
        Dim cbData As IDataObject = Clipboard.GetDataObject()
        GcCalendarGrid1(GcCalendarGrid1.CurrentCellPosition.Date)(cell.RowIndex + 1, cell.ColumnIndex).Value =
            cbData.GetData(DataFormats.Text).ToString()
    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)
{
    InputManCell.CalendarGcTimeSpanCellType gcTimeSpanCellType = new InputManCell.CalendarGcTimeSpanCellType();
    // セルに値を設定します。
    gcTimeSpanCellType.Fields.Clear();
    gcTimeSpanCellType.Fields.AddRange("hh時間mm分ss秒,0,.,,,-,");
    gcTimeSpanCellType.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 = 100;
    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 = gcTimeSpanCellType.Clone();
    template.Content.Rows[1].Cells[0].CellStyleName = "defaultStyle";
    template.Content.Rows[1].Cells[0].Value = System.TimeSpan.Parse("12:21:00");

    gcCalendarGrid1.Template = template;

    gcCalendarGrid1.EditingControlShowing += gcCalendarGrid1_EditingControlShowing;
}

private void gcCalendarGrid1_EditingControlShowing(object sender, CalendarEditingControlShowingEventArgs e)
{
    if (e.Control is CalendarGridInputMan.GcTimeSpan)
    {
        // GcTimeSpanの編集用コントロールが表示された場合にKeyDownイベントを設定します。
        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];
    var editor = sender as CalendarGridInputMan.GcTimeSpan;

    if (e.KeyCode == Keys.F5)
    {
        // クリップボードにコピーします。
        editor.SelectionStart = 0;
        editor.SelectionLength = editor.Text.Length;
        Clipboard.SetDataObject(editor.SelectedText);

        // クリップボードのデータを取得して確認します。
        IDataObject cbData = Clipboard.GetDataObject();
        gcCalendarGrid1[gcCalendarGrid1.CurrentCellPosition.Date][cell.RowIndex + 1, cell.ColumnIndex].Value =
            cbData.GetData(DataFormats.Text).ToString();
    }
}


編集モードの切り替え

CalendarGcTimeSpanCellType.EditModeプロパティを使用します。

EditModeプロパティを使って、セルがフォーカスを受け取ったときのデフォルトの編集モードを定義できます。EditModeプロパティをEditMode.Insertにすると挿入モード、EditMode.Overwriteにすると上書きモードになります。また、EditMode.FixedInsertとEditMode.FixedOverwriteでは、編集モードが固定されるので、実行中に[Ins]キーが押されても編集モードは切り替わりません。

セルの編集時では、EditMode プロパティがEditMode.InsertまたはEditMode.Overwriteに設定されている場合、編集モードが[Ins]キーまたはEditModeプロパティによって変更されたときには、GcTimeSpan.EditStatusChanged イベントが発生します。また、フォーカス取得時の編集モードは、Overwriteプロパティを使って調べることができます。

次のサンプルコードは、GcTimeSpan.KeyDownイベントで[Alt]キーおよび[C]キーが押下されたとき、GcTimeSpan.EditModeプロパティを使用して編集モードを取得します。

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 GcTimeSpanCellType As New InputManCell.CalendarGcTimeSpanCellType()
    GcTimeSpanCellType.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 = 120
    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 = GcTimeSpanCellType.Clone()
    Template.Content.Rows(1).Cells(0).CellStyleName = "defaultStyle"
    Template.Content.Rows(1).Cells(0).Value = System.TimeSpan.Parse("12:21:00")

    GcCalendarGrid1.Template = Template

    AddHandler GcCalendarGrid1.EditingControlShowing, AddressOf GcCalendarGrid1_EditingControlShowing
End Sub

Private Sub GcCalendarGrid1_EditingControlShowing(sender As Object, e As CalendarEditingControlShowingEventArgs)
    If TypeOf e.Control Is CalendarGridInputMan.GcTimeSpan 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.GcTimeSpan = DirectCast(sender, CalendarGridInputMan.GcTimeSpan)

    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)
{

    InputManCell.CalendarGcTimeSpanCellType gcTimeSpanCellType = new InputManCell.CalendarGcTimeSpanCellType();
    gcTimeSpanCellType.EditMode = InputManCell.EditMode.Insert;

    var textBoxCellType = new CalendarTextBoxCellType();

    var template = new CalendarTemplate();
    template.RowCount = 3;
    template.ColumnHeader.Rows[0].Cells[0].DateFormat = "{DayOfWeek}";
    template.ColumnHeader.Columns[0].Width = 100;
    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 = gcTimeSpanCellType.Clone();
    template.Content.Rows[1].Cells[0].CellStyleName = "defaultStyle";

    gcCalendarGrid1.Template = template;

    gcCalendarGrid1.EditingControlShowing += gcCalendarGrid1_EditingControlShowing;
}

private void gcCalendarGrid1_EditingControlShowing(object sender, CalendarEditingControlShowingEventArgs e)
{
    if (e.Control is CalendarGridInputMan.GcTimeSpan)
    {
        // CalendarGcTimeSpanCellTypeの編集用コントロールが表示された場合にKeyDownイベントを設定します。
        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.GcTimeSpan;

    if (e.Alt && e.KeyCode == Keys.C)
    {
        if (editor.OverWrite == true)
        {
            Console.WriteLine("上書き");
        }
        else
        {
            Console.WriteLine("挿入");
        }
    }
}


イベントを使った文字列操作

GcTimeSpan.TextChangingイベントを使用します。

DefaultActiveFieldプロパティを使用すると、セルがフォーカスを受け取ったときにカレットを配置するフィールドを指定できます。
現在カレットが置かれているフィールドは、GcTimeSpan.ActiveFieldプロパティで取得できます。

セルに文字列を入力するとGcTimeSpan.TextChangingイベントが、TextChangedイベントの前(入力された文字列がTextプロパティに渡される前)に発生します。そのため、このイベント内で入力文字列をチェックすれば、Textプロパティの値に影響を与えることなく、入力を制御できます。


コンテキストメニューの表示

CalendarGridの他のセル型の場合と同様です。CalendarGcTimeSpanCellType.ContextMenuStripを使用します。


参照

 

 


© 2014 GrapeCity inc. All rights reserved.