Xceed Grid for WinForms v4.3 Documentation
BooleanImageTextViewer class

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 > BooleanImageTextViewer class

The BooleanImageTextViewer class demonstrates how to override the DefaultImageArea, DefaultImagePadding, and DefaultTextPadding properties, as well as the GetImageCore and GetTextCore methods, to create a custom painted CellViewerManager.

VB.NET Copy Code

Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.Text
Imports Xceed.Grid
Imports Xceed.Grid.Viewers
Imports System.Drawing 

Class BooleanImageTextViewer
   Inherits CellViewerManager 

   Shared Sub New()
     Dim streamFalseImage As Stream = GetType( BooleanImageTextViewer ).Assembly.GetManifestResourceStream( "Xceed.Grid.Samples.Delete.PNG" ) 

     ' We clone the bitmap so that exceptions do not occur when the source stream is
     ' disposed of.         
     BooleanImageTextViewer.FalseImage = New Bitmap( New Bitmap( streamFalseImage ) )
     streamFalseImage.Dispose() 

     Dim streamTrueImage As Stream = GetType( BooleanImageTextViewer ).Assembly.GetManifestResourceStream( "Xceed.Grid.Samples.GreenCheck.png" )

     BooleanImageTextViewer.TrueImage = New Bitmap( New Bitmap( streamTrueImage ) ) 

     streamTrueImage.Dispose()
  End Sub 

   Public Sub New()
     MyBase.New()
  End Sub 

   Protected Overrides ReadOnly Property DefaultImageArea() As ImageArea
     Get
       Return Xceed.Grid.ImageArea.Left
     End Get
  End Property 

   Protected Overrides ReadOnly Property DefaultImagePadding() As Xceed.UI.Margins
     Get
       Return New Xceed.UI.Margins( 2 )
     End Get
  End Property 

   Protected Overrides ReadOnly Property DefaultTextPadding() As Xceed.UI.Margins
     Get
       Return New Xceed.UI.Margins( 2 )
     End Get
  End Property 

   Protected Overrides Function GetImageCore( ByVal value As Object, _
                                              ByVal formatInfo As CellImageFormatInfo, _
                                            ByVal gridElement As GridElement ) As System.Drawing.Image

     Return IIf( CBool( Convert.ChangeType( value, GetType( Boolean ) ) ), _
                 BooleanImageTextViewer.TrueImage, BooleanImageTextViewer.FalseImage )
  End Function 

   Protected Overrides Function GetTextCore( ByVal value As Object, _
                                             ByVal formatInfo As CellTextFormatInfo, _ 
                                             ByVal gridElement As GridElement ) As String

     Return IIf( CBool( Convert.ChangeType( value, GetType( Boolean ) ) ), _
                            m_receivedText, m_delayedText )
  End Function 

   Private m_receivedText As String = "Order Received"
   Private m_delayedText As String = "Order Delayed"
   Private Shared TrueImage As Image
   Private Shared FalseImage As Image
End Class

C# Copy Code

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Xceed.Grid;
using Xceed.Grid.Viewers;
using System.Drawing; 

namespace Xceed.Grid.Samples
{
   class BooleanImageTextViewer : CellViewerManager
   {
    static BooleanImageTextViewer()        
    {
       // We clone the bitmap so that exceptions do not occur in the case where the source stream
       // is disposed of.          
       using( Stream stream = typeof( BooleanImageTextViewer ).Assembly.GetManifestResourceStream( "Xceed.Grid.Samples.Delete.PNG" ) )
       {
         BooleanImageTextViewer.FalseImage = new Bitmap( new Bitmap( stream ) );
      

       using( Stream stream = typeof( BooleanImageTextViewer ).Assembly.GetManifestResourceStream( "Xceed.Grid.Samples.GreenCheck.png" ) )
       {
         BooleanImageTextViewer.TrueImage = new Bitmap( new Bitmap( stream ) );
       }
    

    public BooleanImageTextViewer()
       : base()
    {
    }    

     protected override ImageArea DefaultImageArea
    {
       get{ return ImageArea.Left; }
    

     protected override Xceed.UI.Margins DefaultImagePadding
    {
       get{ return new Xceed.UI.Margins( 2 ); }
    

     protected override Xceed.UI.Margins DefaultTextPadding
    {
       get{ return new Xceed.UI.Margins( 2 ); }
    

     protected override System.Drawing.Image GetImageCore( object value,
                              CellImageFormatInfo formatInfo, GridElement gridElement )
    {
       return ( bool )Convert.ChangeType( value, typeof( bool ) ) ?
                  BooleanImageTextViewer.TrueImage : BooleanImageTextViewer.FalseImage;
    

     protected override string GetTextCore( object value, CellTextFormatInfo formatInfo,
                                           GridElement gridElement )
    {
       return ( bool )Convert.ChangeType( value, typeof( bool ) ) ?
                     m_receivedText : m_delayedText;        
    

    private string m_receivedText = "Order Received";
    private string m_delayedText = "Order Delayed";
    static private Image TrueImage;
    static private Image FalseImage;
   }
}