GrapeCity.Win.MultiRow.v80 アセンブリ > GrapeCity.Win.MultiRow 名前空間 : IEditingCell インターフェース |
このインタフェースは、CheckBoxCell、TrackBarCell、RadioGroupCellなどのCellから派生したクラスによって実装され、編集コントロールをホストせずに値を指定するためのユーザーインタフェース(UI)を提供します。この場合、セルのEditTypeプロパティはnull 参照 (Visual Basicでは Nothing)に設定されます。
ButtonCellなどの他のセル型では、UIは提供されますが、ユーザー指定の値は格納されません。これらのセル型ではIEditingCellは実装されず、編集コントロールもホストされません。
セルが編集モードのときに編集コントロールを表示するセル型(TextBoxCellなど)では、IEditingCellは実装されず、代わりにIEditingControlを実装するコンパニオンクラスが提供されます。たとえば、TextBoxCellは、System.Windows.Forms.TextBoxコントロールから派生した、IEditingControlを実装するTextBoxEditingControlを提供します。この場合、セルのEditTypeプロパティは、編集コントロールの型を表すTypeオブジェクトに設定されます。
using System; using System.Windows.Forms; using System.Collections.Generic; using System.Drawing; namespace GrapeCity.Win.MultiRow.SampleCode { public class EditedProgressBarCell : ProgressBarCell , IEditingCell { public EditedProgressBarCell() { // Initialize the cell, set read only property to false to make the cell can be edited. this.ReadOnly = false; } int _editingValue = 0; public override Type EditType { get { // editing cell's edit type should be null. return null; } } public object EditingCellFormattedValue { get { return _editingValue; } set { if ((int)value < this.Minimum) { value = Minimum; } else if ((int)value > this.Maximum) { value = Maximum; } if (_editingValue != (int)value) { _editingValue = (int)value; // When change the editing cell's value, the cell should fire EditingCellValueChanged event to notify MultiRow control. // So that, when leave editing status, MultiRow control can use this value as cell's new value. this.OnEditingCellValueChanged(EventArgs.Empty); // Notify MultiRow control redraw this cell. this.Invalidate(); } } } public event EventHandler EditingCellValueChanged; protected virtual void OnEditingCellValueChanged(EventArgs e) { if (this.EditingCellValueChanged != null) { this.EditingCellValueChanged(this, e); } } protected override void OnCellFormatting(CellFormattingEventArgs e) { // When multiRow control repaint cell, the cell will get the paint value. // The cell should use the EditingCellFormattedValue property value to paint cell, if cell in editing status. // You also change override OnPaint method to customize the cell painting logic for edit status, // if so, override OnCellFormatting is not necessary. if (this.GcMultiRow.IsCurrentCellInEditMode && this.GcMultiRow.CurrentCell.RowIndex == e.RowIndex && this.GcMultiRow.CurrentCell.CellIndex == e.CellIndex) { e.Value = this.EditingCellFormattedValue; } base.OnCellFormatting(e); } public bool WantsInputKey(Keys keyData) { // Tell MultiRow control that the cell will handle '+','-', and left, right key when edit status. if (keyData == Keys.Add || keyData == Keys.Subtract || keyData == Keys.Left || keyData == Keys.Right) { return true; } return false; } protected override void OnKeyDown(CellKeyEventArgs e) { // Handle key board message when editing state. if (this.GcMultiRow.CurrentCell.RowIndex == e.RowIndex && this.GcMultiRow.CurrentCell.CellIndex == e.CellIndex && this.GcMultiRow.IsCurrentCellInEditMode) { if (e.KeyData == Keys.Add || e.KeyData == Keys.Right) { this.EditingCellFormattedValue = _editingValue + 1; } else if (e.KeyData == Keys.Subtract || e.KeyData == Keys.Left) { this.EditingCellFormattedValue = _editingValue - 1; } base.OnKeyDown(e); } } bool _beginMouseDraging = false; protected override void OnMouseDown(CellMouseEventArgs e) { // Override OnMouseDown method to customize the action when mouse down the cell. if (e.Button == MouseButtons.Left) { // Assert the cell is current cell. if (this.GcMultiRow.CurrentCell.RowIndex == e.RowIndex && this.GcMultiRow.CurrentCell.CellIndex == e.CellIndex) { bool beginEditSuccess = this.GcMultiRow.BeginEdit(false); if (beginEditSuccess) { this.EditingCellFormattedValue = this.GetValueFromPoint(e.Location); this._beginMouseDraging = true; } } } base.OnMouseDown(e); } protected override void OnMouseMove(CellMouseEventArgs e) { if (this._beginMouseDraging) { // Mouse dragging to change the EditingCellFormattedValue. this.EditingCellFormattedValue = this.GetValueFromPoint(e.Location); } base.OnMouseMove(e); } protected override void OnMouseUp(CellMouseEventArgs e) { // When mouse up, end dragging. this._beginMouseDraging = false; base.OnMouseUp(e); } public override object Clone() { EditedProgressBarCell editedProgressBarCell = base.Clone() as EditedProgressBarCell; // If add new properties for new cell type. The properties should be assign to new cell instance here. return editedProgressBarCell; } protected override void OnEnter(CellEventArgs e) { // When MultiRow start up, the cells and rows are virtual because of performance reason. // When a cell which implement IEditingCell interface begin edit. the cell may change to a new instance. // So, I force create cell's instance when the cell enter. object o = this.GcMultiRow.CurrentCell; base.OnEnter(e); } void GcMultiRow_MouseCaptureChanged(object sender, EventArgs e) { // When MultiRow control mouse capture changed, end dragging. _beginMouseDraging = false; } protected override void InitializeEditingControl(int rowIndex, object formattedValue, CellStyle style) { base.InitializeEditingControl(rowIndex, formattedValue, style); this.EditingCellFormattedValue = formattedValue; this.GcMultiRow.MouseCaptureChanged += new EventHandler(GcMultiRow_MouseCaptureChanged); } protected override void TerminateEditingControl(int rowIndex) { base.TerminateEditingControl(rowIndex); this.GcMultiRow.MouseCaptureChanged -= new EventHandler(GcMultiRow_MouseCaptureChanged); } int GetValueFromPoint(Point point) { double value = (this.Maximum - this.Minimum) * (((double)point.X) / (this.Width)) + this.Minimum; return (int)value; } public void PrepareEditingCellForEdit(bool selectAll) { // Do some thing to prepare edit for editing cell. } } public class EditedProgressBarCellDemo : Form { private GcMultiRow gcMultiRow1 = new GcMultiRow(); public EditedProgressBarCellDemo() { this.Text = "Edit cell without embed 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 = 20; label.Dock = DockStyle.Bottom; label.BackColor = SystemColors.Info; label.Text = "Yow can try to click or drag a cell to edit cell value"; this.Controls.Add(label); this.Load += new EventHandler(Form1_Load); } private void Form1_Load(object sender, EventArgs e) { // Add ListBoxCell into template. EditedProgressBarCell editedProgressBarCell = new EditedProgressBarCell(); editedProgressBarCell.Size = new Size(200, 30); Cell[] cells = new Cell[] { editedProgressBarCell }; // Create a template by listBoxCell cell. Template template = Template.CreateGridTemplate(cells); this.gcMultiRow1.Template = template; this.gcMultiRow1.RowCount = 6; } [STAThreadAttribute()] public static void Main() { Application.EnableVisualStyles(); Application.Run(new EditedProgressBarCellDemo()); } } }
Imports System Imports System.Windows.Forms Imports System.Drawing Imports GrapeCity.Win.MultiRow Public Class EditedProgressBarCell Inherits ProgressBarCell Implements IEditingCell Public Sub New() ' Initialize the cell, set read only property to false to make the cell can be edited. Me.ReadOnly = False End Sub Private _editingValue As Integer = 0 Public Overloads Overrides ReadOnly Property EditType() As Type Get ' editing cell's edit type should be null. Return Nothing End Get End Property Public Property EditingCellFormattedValue() As Object Implements IEditingCell.EditingCellFormattedValue Get Return _editingValue End Get Set(ByVal value As Object) If DirectCast(value, Integer) < Me.Minimum Then value = Minimum ElseIf DirectCast(value, Integer) > Me.Maximum Then value = Maximum End If If _editingValue <> DirectCast(value, Integer) Then _editingValue = DirectCast(value, Integer) ' When change the editing cell's value, the cell should fire EditingCellValueChanged event to notify MultiRow control. ' So that, when leave editing status, MultiRow control can use this value as cell's new value. Me.OnEditingCellValueChanged(EventArgs.Empty) ' Notify MultiRow control redraw this cell. Me.Invalidate() End If End Set End Property Public Event EditingCellValueChanged As EventHandler Implements IEditingCell.EditingCellValueChanged Protected Overridable Sub OnEditingCellValueChanged(ByVal e As EventArgs) RaiseEvent EditingCellValueChanged(Me, e) End Sub Protected Overloads Overrides Sub OnCellFormatting(ByVal e As CellFormattingEventArgs) ' When multiRow control repaint cell, the cell will get the paint value. ' The cell should use the EditingCellFormattedValue property value to paint cell, if cell in editing status. ' You also change override OnPaint method to customize the cell painting logic for edit status, ' if so, override OnCellFormatting is not necessary. If Me.GcMultiRow.IsCurrentCellInEditMode AndAlso Me.GcMultiRow.CurrentCell.RowIndex = e.RowIndex AndAlso Me.GcMultiRow.CurrentCell.CellIndex = e.CellIndex Then e.Value = Me.EditingCellFormattedValue End If MyBase.OnCellFormatting(e) End Sub Public Function WantsInputKey(ByVal keyData As Keys) As Boolean Implements IEditingCell.WantsInputKey ' Tell MultiRow control that the cell will handle '+','-', and left, right key when edit status. If keyData = Keys.Add OrElse keyData = Keys.Subtract OrElse keyData = Keys.Left OrElse keyData = Keys.Right Then Return True End If Return False End Function Protected Overloads Overrides Sub OnKeyDown(ByVal e As CellKeyEventArgs) ' Handle key board message when editing state. If Me.GcMultiRow.CurrentCell.RowIndex = e.RowIndex AndAlso Me.GcMultiRow.CurrentCell.CellIndex = e.CellIndex AndAlso Me.GcMultiRow.IsCurrentCellInEditMode Then If e.KeyData = Keys.Add OrElse e.KeyData = Keys.Right Then Me.EditingCellFormattedValue = _editingValue + 1 ElseIf e.KeyData = Keys.Subtract OrElse e.KeyData = Keys.Left Then Me.EditingCellFormattedValue = _editingValue - 1 End If MyBase.OnKeyDown(e) End If End Sub Private _beginMouseDraging As Boolean = False Protected Overloads Overrides Sub OnMouseDown(ByVal e As CellMouseEventArgs) ' Override OnMouseDown method to customize the action when mouse down the cell. If e.Button = MouseButtons.Left Then ' Assert the cell is current cell. If Me.GcMultiRow.CurrentCell.RowIndex = e.RowIndex AndAlso Me.GcMultiRow.CurrentCell.CellIndex = e.CellIndex Then Dim beginEditSuccess As Boolean = Me.GcMultiRow.BeginEdit(False) If beginEditSuccess Then Me.EditingCellFormattedValue = Me.GetValueFromPoint(e.Location) Me._beginMouseDraging = True End If End If End If MyBase.OnMouseDown(e) End Sub Protected Overloads Overrides Sub OnMouseMove(ByVal e As CellMouseEventArgs) If Me._beginMouseDraging Then ' Mouse dragging to change the EditingCellFormattedValue. Me.EditingCellFormattedValue = Me.GetValueFromPoint(e.Location) End If MyBase.OnMouseMove(e) End Sub Protected Overloads Overrides Sub OnMouseUp(ByVal e As CellMouseEventArgs) ' When mouse up, end dragging. Me._beginMouseDraging = False MyBase.OnMouseUp(e) End Sub Public Overloads Overrides Function Clone() As Object Dim editedProgressBarCell As EditedProgressBarCell = TryCast(MyBase.Clone(), EditedProgressBarCell) ' If add new properties for new cell type. The properties should be assign to new cell instance here. Return editedProgressBarCell End Function Protected Overloads Overrides Sub OnEnter(ByVal e As CellEventArgs) ' When MultiRow start up, the cells and rows are virtual because of performance reason. ' When a cell which implement IEditingCell interface begin edit. the cell may change to a new instance. ' So, I force create cell's instance when the cell enter. Dim o As Object = Me.GcMultiRow.CurrentCell MyBase.OnEnter(e) End Sub Private Sub GcMultiRow_MouseCaptureChanged(ByVal sender As Object, ByVal e As EventArgs) ' When MultiRow control mouse capture changed, end dragging. _beginMouseDraging = False End Sub Protected Overloads Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal formattedValue As Object, ByVal style As CellStyle) MyBase.InitializeEditingControl(rowIndex, formattedValue, style) Me.EditingCellFormattedValue = formattedValue AddHandler Me.GcMultiRow.MouseCaptureChanged, AddressOf GcMultiRow_MouseCaptureChanged End Sub Protected Overloads Overrides Sub TerminateEditingControl(ByVal rowIndex As Integer) MyBase.TerminateEditingControl(rowIndex) RemoveHandler Me.GcMultiRow.MouseCaptureChanged, AddressOf GcMultiRow_MouseCaptureChanged End Sub Private Function GetValueFromPoint(ByVal point As Point) As Integer Dim value As Double = (Me.Maximum - Me.Minimum) * (CDbl(point.X) / (Me.Width)) + Me.Minimum Return CDbl(value) End Function Public Sub PrepareEditingCellForEdit(ByVal selectAll As Boolean) Implements IEditingCell.PrepareEditingCellForEdit ' Do some thing to prepare edit for editing cell. End Sub End Class Public Class EditedProgressBarCellDemo Inherits Form Private gcMultiRow1 As New GcMultiRow() Public Sub New() Me.Text = "Edit cell without embed 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 = 20 label.Dock = DockStyle.Bottom label.BackColor = SystemColors.Info label.Text = "Yow can try to click or drag a cell to edit cell value" 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 editedProgressBarCell As New EditedProgressBarCell() editedProgressBarCell.Size = New Size(200, 30) Dim cells As Cell() = New Cell() {editedProgressBarCell} ' Create a template by listBoxCell cell. Dim template1 As Template = Template.CreateGridTemplate(cells) Me.gcMultiRow1.Template = template1 Me.gcMultiRow1.RowCount = 6 End Sub <STAThreadAttribute()> _ Public Shared Sub Main() Application.EnableVisualStyles() Application.Run(New EditedProgressBarCellDemo()) End Sub End Class