Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports FarPoint.Win.Spread
Imports FarPoint.Win
Imports Spread.Common.Features.Spread.Common.Features.Sample

Partial Public Class Introduction
    Inherits BaseSampleControl
    Private _bclrHeader As Color = Color.DarkGreen
    Private _fclrHeader As Color = Color.White
    Private _fntHeader As New Font("Segoi UI", 10, FontStyle.Bold)
    Private _bdrLine As New ComplexBorder(New ComplexBorderSide(ComplexBorderSideStyle.Dashed), New ComplexBorderSide(ComplexBorderSideStyle.Dashed), New ComplexBorderSide(ComplexBorderSideStyle.Dashed), New ComplexBorderSide(ComplexBorderSideStyle.Dashed))

    Private _unary As UnaryComparisonConditionalFormattingRule
    Private _betweenFileringl As BetweenValuesConditionalFormattingRule
    Private _twoColorScale As TwoColorScaleConditionalFormattingRule
    Private _threeColorScale As ThreeColorScaleConditionalFormattingRule
    Private _timePeriiodRule As TimePeriodConditionalFormattingRule
    Private _textFormattingRule As TextConditionalFormattingRule
    Private _dataBarRule As DatabarConditionalFormattingRule
    Private _iconSetRule As IconSetConditionalFormattingRule
    Public Sub New()
        InitializeComponent()

        _sheetView.SetRowHeight(-1, 25)
        'Header
        SetTitle(1, 1, 2, 13, "Conditional Format Samples")
        _sheetView.Cells(1, 1).HorizontalAlignment = CellHorizontalAlignment.Center
        _sheetView.Cells(1, 1).VerticalAlignment = CellVerticalAlignment.Center
        _sheetView.Cells(1, 1).Font = New Font("Segoi UI", 14, FontStyle.Bold)

        'Conditional Formats
        'Table 1
        _unary = New UnaryComparisonConditionalFormattingRule(UnaryComparisonOperator.GreaterThanOrEqualTo, 100, False)
        _unary.BackColor = Color.FromArgb(204, 255, 204)
        SetTableAndFormats(4, 1, 4, 6, "Displays green background if cell value > 100:", _unary)

        'Table 2
        _betweenFileringl = New BetweenValuesConditionalFormattingRule(False, 60, False, 120, False)
        _betweenFileringl.BackColor = Color.FromArgb(255, 204, 204)
        SetTableAndFormats(4, 8, 4, 6, "Displays red background if value >= 60 and value <= 120:", _betweenFileringl)

        'Table 3
        _twoColorScale = New TwoColorScaleConditionalFormattingRule(Color.FromArgb(255, 153, 153), Color.FromArgb(153, 153, 255), True)
        SetTableAndFormats(10, 1, 4, 6, "Displays two color gradient representing cell value:", _twoColorScale)

        'Table 4
        _threeColorScale = New ThreeColorScaleConditionalFormattingRule(Color.FromArgb(255, 153, 153), Color.FromArgb(153, 255, 153), Color.FromArgb(153, 153, 255))
        SetTableAndFormats(10, 8, 4, 6, "Displays three color gradient representing cell value:", _threeColorScale)

        'Table 5
        _timePeriiodRule = New TimePeriodConditionalFormattingRule(TimePeriod.NextWeek)
        _timePeriiodRule.BackColor = Color.FromArgb(204, 204, 255)
        SetTableAndFormats(16, 1, 4, 6, "Display blue background if cell value is in next week:", _timePeriiodRule,
            True)

        'Table 6
        _textFormattingRule = New TextConditionalFormattingRule(TextConditionOperator.Contains, "O")
        _textFormattingRule.ForeColor = Color.Red
        _textFormattingRule.FontStyle = New SpreadFontStyle(RegularBoldItalicFontStyle.Bold)
        SetTableAndFormats(16, 8, 4, 6, "Display red foreground if cell value contains ""o"":", _textFormattingRule,
            False, True)

        'Table 7
        _dataBarRule = New DatabarConditionalFormattingRule()
        _dataBarRule.FillColor = Color.Green
        _dataBarRule.Gradient = True
        SetTableAndFormats(22, 1, 4, 6, "Display a colored data bar representing cell value:", _dataBarRule)

        'Table 8
        _iconSetRule = New IconSetConditionalFormattingRule(ConditionalFormattingIconSetStyle.FiveColoredArrows)
        SetTableAndFormats(22, 8, 4, 6, "Displays an icon represents cell value:", _iconSetRule)
    End Sub
    Private Sub SetTableAndFormats(row As Integer, col As Integer, rowCount As Integer, colCount As Integer, title As String, formatRule As IConditionalFormattingRule,
        Optional IsDateTime As Boolean = False, Optional IsString As Boolean = False)
        SetTitle(row, col, 1, colCount, title)
        _sheetView.Cells(row, col, row + rowCount - 1, col + colCount - 1).Border = _bdrLine

        If IsDateTime Then
            Dim value = DateTime.Now
            Dim day As Integer = 0
            For r As Integer = row + 1 To row + (rowCount - 1)
                For c As Integer = col To col + (colCount - 1)
                    _sheetView.Cells(r, c).Value = value.AddDays(System.Math.Max(System.Threading.Interlocked.Increment(day), day - 1)).ToShortDateString()
                Next
            Next
        ElseIf IsString Then
            Dim value = "The quick brown fox jumps over the lazy dog".Split(" "c)
            Dim valueAt As Integer = 0
            For r As Integer = row + 1 To row + (rowCount - 1)
                For c As Integer = col To col + (colCount - 1)
                    _sheetView.Cells(r, c).Value = value(valueAt)
                    valueAt += 1
                    valueAt = valueAt Mod value.Length
                Next
            Next
        Else
            Dim value = 0
            For r As Integer = row + 1 To row + (rowCount - 1)
                For c As Integer = col To col + (colCount - 1)
                    _sheetView.Cells(r, c).Value = value
                    value += 10
                Next
            Next
        End If
        _sheetView.SetConditionalFormatting(row + 1, col, rowCount - 1, colCount, formatRule)
    End Sub
    Private Sub SetTitle(row As Integer, col As Integer, rowCount As Integer, colCount As Integer, title As String)
        _sheetView.Cells(row, col).RowSpan = rowCount
        _sheetView.Cells(row, col).ColumnSpan = colCount
        _sheetView.Cells(row, col).BackColor = _bclrHeader
        _sheetView.Cells(row, col).ForeColor = _fclrHeader
        _sheetView.Cells(row, col).Font = _fntHeader
        _sheetView.Cells(row, col).Value = title
    End Sub
End Class

'
'
'

Partial Class Introduction
	''' <summary> 
	''' Required designer variable.
	''' </summary>
	Private components As System.ComponentModel.IContainer = Nothing

    ''' <summary> 
    ''' Clean up any resources being used.
    ''' </summary>
    ''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    Protected Shadows Sub Dispose(disposing As Boolean)
        If disposing AndAlso (components IsNot Nothing) Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

#Region "Component Designer generated code"

    ''' <summary> 
    ''' Required method for Designer support - do not modify 
    ''' the contents of this method with the code editor.
    ''' </summary>
    Private Sub InitializeComponent()
		Me.fpSpread1 = New FarPoint.Win.Spread.FpSpread()
		Me._sheetView = New FarPoint.Win.Spread.SheetView()
		Me.pnlMain.SuspendLayout()
		DirectCast(Me.fpSpread1, System.ComponentModel.ISupportInitialize).BeginInit()
		DirectCast(Me._sheetView, System.ComponentModel.ISupportInitialize).BeginInit()
		Me.SuspendLayout()
		' 
		' pnlMain
		' 
		Me.pnlMain.Controls.Add(Me.fpSpread1)
		' 
		' fpSpread1
		' 
		Me.fpSpread1.AccessibleDescription = ""
		Me.fpSpread1.Dock = System.Windows.Forms.DockStyle.Fill
		Me.fpSpread1.Location = New System.Drawing.Point(0, 0)
		Me.fpSpread1.Name = "fpSpread1"
		Me.fpSpread1.Sheets.AddRange(New FarPoint.Win.Spread.SheetView() {Me._sheetView})
		Me.fpSpread1.Size = New System.Drawing.Size(477, 487)
		Me.fpSpread1.TabIndex = 0
		' 
		' _sheetView
		' 
		Me._sheetView.Reset()
		Me._sheetView.SheetName = "Sheet1"
		' Formulas and custom names must be loaded with R1C1 reference style
		Me._sheetView.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1
		Me._sheetView.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1
		' 
		' Introduction
		' 
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.Name = "Introduction"
		Me.pnlMain.ResumeLayout(False)
		DirectCast(Me.fpSpread1, System.ComponentModel.ISupportInitialize).EndInit()
		DirectCast(Me._sheetView, System.ComponentModel.ISupportInitialize).EndInit()
		Me.ResumeLayout(False)

	End Sub

	#End Region

	Private fpSpread1 As FarPoint.Win.Spread.FpSpread
	Private _sheetView As FarPoint.Win.Spread.SheetView
End Class
