GrapeCity.Win.MultiRow.v80 アセンブリ > GrapeCity.Win.MultiRow 名前空間 : IEditingControl インターフェース |
セルが編集モードのときに編集コントロールを表示するセル型(TextBoxCellなど)では、IEditingCellは実装されず、代わりにIEditingControlを実装するコンパニオンクラスが提供されます。たとえば、TextBoxCellは、System.Windows.Forms.TextBoxコントロールから派生した、IEditingControlを実装するTextBoxEditingControlを提供します。この場合、セルのCell.EditTypeプロパティは、編集コントロールの型を表すTypeオブジェクトに設定されます。GcMultiRowは、編集コントロールをホストするCellが初めて作成されるときに、セルのCell.EditTypeプロパティに基づいてインスタンスを作成します。同じCell.EditTypeプロパティを持つセルは、すでに作成されている編集コントロールを再利用します。
セルが編集モードのときにセルを表示するセル型(CheckBoxCellなど)では、IEditingControlは実装されず、代わりにCellから派生したIEditingCellを実装するコンパニオンクラスを提供することで、編集コントロールをホストせずに値を指定するためのユーザーインタフェース(UI)が提供されます。この場合、セルのEditTypeプロパティはnull 参照 (Visual Basicでは Nothing)に設定されます。
ButtonCellなどの他のセル型では、UIは提供されますが、ユーザー指定の値は格納されません。これらのセル型ではIEditingCellは実装されず、編集コントロールもホストされません。
using System; using System.Windows.Forms; using System.Collections.Generic; using System.Drawing; namespace GrapeCity.Win.MultiRow.SampleCode { public class ListBoxCell : Cell { public override Type EditType { get { return typeof(ListBoxEditingControl); } } List<string> _items = new List<string>(); public List<string> Items { get { return _items; } } protected override void InitializeEditingControl(int rowIndex, object formattedValue, CellStyle style) { // Apply the settings of cell to editing control. base.InitializeEditingControl(rowIndex, formattedValue, style); ListBoxEditingControl listBoxEditingControl = this.GcMultiRow.EditingControl as ListBoxEditingControl; for (int i = 0; i < this._items.Count; i++) { listBoxEditingControl.Items.Add(this._items[i]); } listBoxEditingControl.FormattedValue = formattedValue; } protected override void TerminateEditingControl(int rowIndex) { ListBoxEditingControl listBoxEditingControl = this.GcMultiRow.EditingControl as ListBoxEditingControl; // For performance reason, MultiRow use same instance for all cells. // So, when leave editing status, you should clear items, to prepare editing control be used next time. listBoxEditingControl.Items.Clear(); base.TerminateEditingControl(rowIndex); } public override object Clone() { // When you derive from Cell and add new properties to the derived class, be sure to // override the Clone method to copy the new properties during cloning operations. // You should also call the base class's Clone method so that the properties of the // base class are copied to the new cell. ListBoxCell listBoxCell = base.Clone() as ListBoxCell; listBoxCell.Items.AddRange(this.Items); return listBoxCell; } } public class ListBoxEditingControl : ListBox, IEditingControl { public ListBoxEditingControl() { this.BorderStyle = BorderStyle.None; } private GcMultiRow _gcMultiRow; // MultiRow control will set this property when begin edit. Editing control can use this property. public GcMultiRow GcMultiRow { get { return _gcMultiRow; } set { _gcMultiRow = value; } } private int _rowIndex; // MultiRow control will set this property when begin edit. Editing control can use this property. public int RowIndex { get { return _rowIndex; } set { _rowIndex = value; } } // 1. When begin edit, MultiRow should initial this property. // 2. When end edit, MultiRow will use this property's value as cell value. public object FormattedValue { get { return this.Text; } set { if (value == null) { this.Text = string.Empty; } else { this.Text = value.ToString(); } } } // When user press Ctrl and wheel mouse wheel button. MultiRow will be zoomed. // MultiRow will change this property's value when zoom. // EditingControl should do some thing to fit the new zoom factor, for example, change Font. private float _zoomFactor; private Font _initializeFont = null; public float ZoomFactor { get { return _zoomFactor; } set { this._zoomFactor = value; if (_zoomFactor != 1 && _initializeFont != null) { this.Font = new Font(_initializeFont.FontFamily, _initializeFont.Size * _zoomFactor); } } } public Cursor EditingPanelCursor { get { return Cursors.Default; } } // The keys which will be handle by editing control. public bool WantsInputKey(Keys keyData) { // The editing control will handle up and down keys. if (keyData == Keys.Up || keyData == Keys.Down) { return true; } return false; } public void PrepareEditingControlForEdit(bool selectAll) { // Do some thing to prepare edit for editing control. } // Apply style. public void ApplyCellStyleToEditingControl(CellStyle cellStyle) { this.BackColor = Color.FromArgb(255, cellStyle.BackColor); this.ForeColor = Color.FromArgb(255, cellStyle.ForeColor); _initializeFont = cellStyle.Font; if (this._zoomFactor != 1) { this.Font = new Font(_initializeFont.FontFamily, _initializeFont.Size * _zoomFactor); } } // When the value has been edited, editing control should fire DirtyStateChanged event to notify MultiRow control. protected override void OnSelectedIndexChanged(EventArgs e) { base.OnSelectedIndexChanged(e); this.OnDirtyStateChanged(EventArgs.Empty); } protected virtual void OnDirtyStateChanged(EventArgs e) { if (this.DirtyStateChanged != null) { this.DirtyStateChanged(this, e); } } public event EventHandler DirtyStateChanged; } public class EmbedThridPartControlDemo : Form { private GcMultiRow gcMultiRow1 = new GcMultiRow(); public EmbedThridPartControlDemo() { this.Text = "Embed Third Part Control Demo"; this.Height = 400; // Add MultiRow to form this.gcMultiRow1.Dock = DockStyle.Fill; this.Controls.Add(this.gcMultiRow1); // Tip label Label label = new Label(); label.Height = 35; label.Dock = DockStyle.Bottom; label.BackColor = SystemColors.Info; label.Text = "Yow can try to double click a cell to enter editing status, and select an item in list box control to edit cell"; this.Controls.Add(label); this.Load += new EventHandler(Form1_Load); } private void Form1_Load(object sender, EventArgs e) { // Add ListBoxCell into template. ListBoxCell listBoxCell = new ListBoxCell(); listBoxCell.Items.Add("Item1"); listBoxCell.Items.Add("Item2"); listBoxCell.Items.Add("Item3"); listBoxCell.Size = new Size(150, 42); Cell[] cells = new Cell[] { listBoxCell }; // Create a template by listBoxCell cell. Template template = Template.CreateGridTemplate(cells); this.gcMultiRow1.Template = template; this.gcMultiRow1.RowCount = 6; // Initialize some cell's back color, you can try how the style affect the editing control's back color. this.gcMultiRow1.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow; } [STAThreadAttribute()] public static void Main() { Application.EnableVisualStyles(); Application.Run(new EmbedThridPartControlDemo()); } } }
Imports System Imports System.Windows.Forms Imports System.Drawing Imports GrapeCity.Win.MultiRow Public Class ListBoxCell Inherits Cell Public Overloads Overrides ReadOnly Property EditType() As Type Get Return GetType(ListBoxEditingControl) End Get End Property Private _items As New List(Of String)() Public ReadOnly Property Items() As List(Of String) Get Return _items End Get End Property Protected Overloads Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal formattedValue As Object, ByVal style As CellStyle) ' Apply the settings of cell to editing control. MyBase.InitializeEditingControl(rowIndex, formattedValue, style) Dim listBoxEditingControl As ListBoxEditingControl = TryCast(Me.GcMultiRow.EditingControl, ListBoxEditingControl) For i As Integer = 0 To Me._items.Count - 1 listBoxEditingControl.Items.Add(Me._items(i)) Next listBoxEditingControl.FormattedValue = formattedValue End Sub Protected Overloads Overrides Sub TerminateEditingControl(ByVal rowIndex As Integer) Dim listBoxEditingControl As ListBoxEditingControl = TryCast(Me.GcMultiRow.EditingControl, ListBoxEditingControl) ' For performance reason, MultiRow use same instance for all cells. ' So, when leave editing status, you should clear items, to prepare editing control be used next time. listBoxEditingControl.Items.Clear() MyBase.TerminateEditingControl(rowIndex) End Sub Public Overloads Overrides Function Clone() As Object ' When you derive from Cell and add new properties to the derived class, be sure to ' override the Clone method to copy the new properties during cloning operations. ' You should also call the base class's Clone method so that the properties of the ' base class are copied to the new cell. Dim listBoxCell As ListBoxCell = TryCast(MyBase.Clone(), ListBoxCell) listBoxCell.Items.AddRange(Me.Items) Return listBoxCell End Function End Class Public Class ListBoxEditingControl Inherits ListBox Implements IEditingControl Public Sub New() Me.BorderStyle = BorderStyle.None End Sub Private _gcMultiRow As GcMultiRow ' MultiRow control will set this property when begin edit. Editing control can use this property. Public Property GcMultiRow() As GcMultiRow Implements IEditingControl.GcMultiRow Get Return _gcMultiRow End Get Set(ByVal value As GcMultiRow) _gcMultiRow = value End Set End Property Private _rowIndex As Integer ' MultiRow control will set this property when begin edit. Editing control can use this property. Public Property RowIndex() As Integer Implements IEditingControl.RowIndex Get Return _rowIndex End Get Set(ByVal value As Integer) _rowIndex = value End Set End Property ' 1. When begin edit, MultiRow should initial this property. ' 2. When end edit, MultiRow will use this property's value as cell value. Public Property FormattedValue() As Object Implements IEditingControl.FormattedValue Get Return Me.Text End Get Set(ByVal value As Object) If value = Nothing Then Me.Text = String.Empty Else Me.Text = value.ToString() End If End Set End Property ' When user press Ctrl and wheel mouse wheel button. MultiRow will be zoomed. ' MultiRow will change this property's value when zoom. ' EditingControl should do some thing to fit the new zoom factor, for example, change Font. Private _zoomFactor As Single Private _initializeFont As Font = Nothing Public Property ZoomFactor() As Single Implements IEditingControl.ZoomFactor Get Return _zoomFactor End Get Set(ByVal value As Single) Me._zoomFactor = value If _zoomFactor <> 1 AndAlso Not _initializeFont Is Nothing Then Me.Font = New Font(_initializeFont.FontFamily, _initializeFont.Size * _zoomFactor) End If End Set End Property Public ReadOnly Property EditingPanelCursor() As Cursor Implements IEditingControl.EditingPanelCursor Get Return Cursors.Default End Get End Property ' The keys which will be handle by editing control. Public Function WantsInputKey(ByVal keyData As Keys) As Boolean Implements IEditingControl.WantsInputKey ' The editing control will handle up and down keys. If keyData = Keys.Up OrElse keyData = Keys.Down Then Return True End If Return False End Function Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IEditingControl.PrepareEditingControlForEdit ' Do some thing to prepare edit for editing control. End Sub ' Apply style. Public Sub ApplyCellStyleToEditingControl(ByVal cellStyle As CellStyle) Implements IEditingControl.ApplyCellStyleToEditingControl Me.BackColor = Color.FromArgb(255, cellStyle.BackColor) Me.ForeColor = Color.FromArgb(255, cellStyle.ForeColor) _initializeFont = cellStyle.Font If Me._zoomFactor <> 1 Then Me.Font = New Font(_initializeFont.FontFamily, _initializeFont.Size * _zoomFactor) End If End Sub ' When the value has been edited, editing control should fire DirtyStateChanged event to notify MultiRow control. Protected Overloads Overrides Sub OnSelectedIndexChanged(ByVal e As EventArgs) MyBase.OnSelectedIndexChanged(e) Me.OnDirtyStateChanged(EventArgs.Empty) End Sub Protected Overridable Sub OnDirtyStateChanged(ByVal e As EventArgs) RaiseEvent DirtyStateChanged(Me, e) End Sub Public Event DirtyStateChanged As EventHandler Implements IEditingControl.DirtyStateChanged End Class Public Class EmbedThridPartControlDemo Inherits Form Private gcMultiRow1 As New GcMultiRow() Public Sub New() Me.Text = "Embed Third Part Control Demo" Me.Height = 400 ' Add MultiRow to form Me.gcMultiRow1.Dock = DockStyle.Fill Me.Controls.Add(Me.gcMultiRow1) ' Tip label Dim label As New Label() label.Height = 35 label.Dock = DockStyle.Bottom label.BackColor = SystemColors.Info label.Text = "Yow can try to double click a cell to enter editing status, and select an item in list box control to edit cell" Me.Controls.Add(label) End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load ' Add ListBoxCell into template. Dim listBoxCell As New ListBoxCell() listBoxCell.Items.Add("Item1") listBoxCell.Items.Add("Item2") listBoxCell.Items.Add("Item3") listBoxCell.Size = New Size(150, 42) Dim cells As New List(Of Cell)() cells.Add(listBoxCell) ' Create a template by listBoxCell cell. Dim template1 As Template = Template.CreateGridTemplate(cells) Me.gcMultiRow1.Template = template1 Me.gcMultiRow1.RowCount = 6 ' Initialize some cell's back color, you can try how the style affect the editing control's back color. Me.gcMultiRow1.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow End Sub <STAThreadAttribute()> _ Public Shared Sub Main() Application.EnableVisualStyles() Application.Run(New EmbedThridPartControlDemo()) End Sub End Class