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 CellViewerManager class / TrackBarViewer class

In This Topic
    TrackBarViewer class
    In This Topic

    The TrackBarViewer class demonstrates how to override the SetControlValueCore method to create a custom control CellViewerManager

    VB.NET Copy Code

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

    Class TrackBarViewer
       Inherits CellViewerManager 

       Public Sub New( ByVal minValue As Integer, ByVal maxValue As Integer )
        MyBase.New( New TrackBar(), "Value" ) 

        ' 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 SetControlAppearance method.
        CType( Me.Control, TrackBar ).Minimum = minValue
        CType( Me.Control, TrackBar ).Maximum = maxValue
        CType( Me.Control, TrackBar ).TickStyle = TickStyle.None
        CType( Me.Control, TrackBar ).AutoSize = False
       End Sub 

       Protected Overrides Sub SetControlValueCore( ByVal cell As Xceed.Grid.Cell )
        ' 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( Me.Control, TrackBar ).Value = CInt( Convert.ChangeType( cell.Value, _
                                                 GetType( Integer ) ) )
        Else
           CType( Me.Control, TrackBar ).Value = 0
        End If
       End Sub
    End Class

    C# Copy Code

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Xceed.Grid.Viewers;
    using System.Windows.Forms; 

    namespace Xceed.Grid.Samples
    {
       class TrackBarViewer : CellViewerManager
       {
        public TrackBarViewer( int minValue, int maxValue )
           : base( new TrackBar(), "Value" )
        {
           // 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 SetControlAppearance method. 

           ( ( TrackBar )this.Control ).Minimum = minValue;
           ( ( TrackBar )this.Control ).Maximum = maxValue;
           ( ( TrackBar )this.Control ).TickStyle = TickStyle.None;
           ( ( TrackBar )this.Control ).AutoSize = false;
        

         protected override void SetControlValueCore( Xceed.Grid.Cell cell )
        {
           // 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( ( cell.Value != null ) && ( cell.Value != DBNull.Value ) &&
                                         ( cell.Value != cell.NullValue ) )
           {
            ( ( TrackBar )this.Control ).Value = ( int )Convert.ChangeType( cell.Value, typeof( int ) );
           }
           else
           {
            ( ( TrackBar )this.Control ).Value = 0;
           }
           
       }
    }