Xceed Grid for WinForms v4.3 Documentation
Welcome to Xceed Grid for WinForms v4.3 / Advanced Concepts / Extending the grid's classes / CellEditorManager and CellViewerManager extensions / How to derive from the CellEditorManager class / TrackBarEditor class
In This Topic
    TrackBarEditor class
    In This Topic

    The TrackBarEditor class demonstrates how to override the SetControlValueCore method to create a custom control CellEditorManager

    VB.NET
    Copy Code

    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports Xceed.Grid
    Imports Xceed.Grid.Editors
    Imports System.Windows.Forms

    Class TrackBarEditor
       Inherits CellEditorManager

       Public Sub New()
        Me.New( 0, 100 )
       End Sub

       Public Sub New( ByVal minValue As Integer, ByVal maxValue As Integer )
        MyBase.new( New TrackBar(), True, True )

        ' Because these variables affect all instances, there is no need for this to be done
        ' in the SetControlAppearanceCore method. If these values were to be applied differently
        ' depending on a cell value, then they should be done in the SetControlAppearanceMethod.

        CType( Me.TemplateControl, TrackBar ).Minimum = minValue
        CType( Me.TemplateControl, TrackBar ).Maximum = maxValue
        CType( Me.TemplateControl, TrackBar ).TickStyle = TickStyle.None
        CType( Me.TemplateControl, TrackBar ).AutoSize = False
       End Sub

       Protected Overrides Sub SetControlValueCore( ByVal control As Control, ByVal cell As Cell )
        ' No need to call base since everything is handled here.
        ' Set the value of the cell to the control currently editing the cell, NOT to the TemplateControl.
        '
        ' Verify that the cell's value is not null as null is not supported by the underlying
        ' TrackBar control. If it is null, set 0 as the control's value instead.

        If( ( Not cell.Value Is Nothing ) And ( Not cell.Value Is DBNull.Value ) And _
                                              ( Not cell.Value Is cell.NullValue ) ) Then
           CType( control, TrackBar ).Value = CInt( Convert.ChangeType( cell.Value, GetType( Integer ) ) )
        Else
           CType( control, TrackBar ).Value = 0
        End If
       End Sub

       Protected Overrides ReadOnly Property CreateControlMode() As CreateControlMode
        Get
           ' We override CreateControlMode to return ClonedInstance so that we can
           ' support all the CellEditorDisplayConditions.
           '
           ' Using a ClonedInstace is only possible when deriving. It is not possible when creating
           ' a custom CellEditorManager using events.
           Return CreateControlMode.ClonedInstance
        End Get
       End Property

       ' Because we have overridden CreateControlMode, we must return a cloned instance
       ' of our TemplateControl in the CreateControl method.
       Protected Overrides Function CreateControl() As Control

        ' If you do not want to do the clone manually, you can use the static CloneControl method.
        Return Xceed.UI.ThemedControl.CloneControl( Me.TemplateControl )
       End Function

       Protected Overrides Function GetControlValueCore( ByVal control As Control, _
                                                         ByVal cell As Cell, _
                                                         ByVal returnDataType As Type ) As Object
         ' Return the value of the control that is currently editing the cell and not the TemplateControl.
         Return Convert.ChangeType( CType( control, TrackBar ).Value, returnDataType )
       End Function

       Protected Overrides Function IsInputKeyCore( ByVal control As Control, ByVal cell As Cell, _
                                                    ByVal keyData As Keys ) As Boolean
        ' There is no need to override IsInputKeyCore as base will call the underlying control's
        ' IsInputKey method automatically.
        Return MyBase.IsInputKeyCore( control, cell, keyData )
       End Function

       Protected Overrides Function IsActivationKeyCore( ByVal cell As Cell, _
                                                         ByVal keyData As Keys ) As Boolean
        ' There is no need to call base as nothing happens there.
        Return( ( keyData = Keys.Left) Or ( keyData = Keys.Right) )
       End Function
    End Class