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 Spread.Common.DataStore
Imports FarPoint.Win.Spread.CellType
Imports FarPoint.Win.Spread
Imports Spread.Common.Features.Spread.Common.Features.Sample

Namespace Features
    Partial Public Class Introduction
        Inherits BaseSampleControl
        Public Sub New()
            InitializeComponent()
            Name = "Introduction to Cell Types"
            SetupSpreadSheet()
            InitComboBox()
            InitCurrency()
            InitNumber()
            InitCheckBox()
            InitButton()

            spread.TextTipPolicy = TextTipPolicy.Floating
            AddHandler spread.ButtonClicked, AddressOf Spread_ButtonClicked
            AddHandler spread.TextTipFetch, AddressOf Spread_TextTipFetch
        End Sub

        Private Sub SetupSpreadSheet()
            introductionSheet.DataAutoSizeColumns = False
            introductionSheet.DataAutoCellTypes = False
            introductionSheet.Columns.[Default].Width = 90
            introductionSheet.Rows.[Default].Height = 25
            introductionSheet.Columns(2).Width = 200
            introductionSheet.Columns(3).Width = 80
            introductionSheet.DataSource = New DataService().AccessDataSource.GetNWINDData("Products")

            introductionSheet.Columns.Add(introductionSheet.ColumnCount, 1)
            introductionSheet.Columns(introductionSheet.ColumnCount - 1).Label = "Place Order"
        End Sub
        Private Sub InitComboBox()
            Dim supplierComboBox As New ComboBoxCellType()

            supplierComboBox.Items = GetComboBoxData("CompanyName", "SupplierID", "Suppliers")
            supplierComboBox.ItemData = GetComboBoxData("SupplierID", "SupplierID", "Products")

            introductionSheet.Columns(2).CellType = supplierComboBox
            supplierComboBox.EditorValue = EditorValue.ItemData

            introductionSheet.ColumnHeader.Cells(0, 2).Note = "ComboBox CellType"
            introductionSheet.ColumnHeader.Cells(0, 2).NoteStyle = NoteStyle.PopupNote
        End Sub
        Private Sub InitCurrency()
            Dim cellType As New CurrencyCellType()
            cellType.SpinButton = True
            cellType.SpinIntegerIncrement = 1
            cellType.SpinWrap = True
            introductionSheet.Columns(5).CellType = cellType
            introductionSheet.ColumnHeader.Cells(0, 5).Note = "Currency CellType"
            introductionSheet.ColumnHeader.Cells(0, 5).NoteStyle = NoteStyle.PopupNote
        End Sub
        Private Sub InitNumber()
            Dim cellType As New NumberCellType()
            cellType.DecimalPlaces = 2
            cellType.FractionMode = True
            cellType.OverflowCharacter = "#"c
            cellType.DropDownButton = True
            cellType.SpinButton = True

            introductionSheet.Columns(6).CellType = cellType
            introductionSheet.ColumnHeader.Cells(0, 6).Note = "Number CellType"
            introductionSheet.ColumnHeader.Cells(0, 6).NoteStyle = NoteStyle.PopupNote
        End Sub
        Private Sub InitCheckBox()
            Dim checkBox As New CheckBoxCellType()
            checkBox.TextFalse = "Unchecked"
            checkBox.TextTrue = "Checked"

            introductionSheet.Columns(9).CellType = checkBox
            introductionSheet.ColumnHeader.Cells(0, 9).Note = "CheckBox CellType"
            introductionSheet.ColumnHeader.Cells(0, 9).NoteStyle = NoteStyle.PopupNote
        End Sub
        Private Sub InitButton()
            Dim buttonCell As New ButtonCellType()
            buttonCell.Text = "Place Order"
            introductionSheet.Columns(introductionSheet.ColumnCount - 1).CellType = buttonCell
        End Sub
        Private Function GetComboBoxData(selectColumn As String, orderColumn As String, tableName As String) As String()
            Dim data = New DataService().AccessDataSource.GetNWINDDataComplete(String.Format("SELECT DISTINCT {0},{2} FROM `{1}` ORDER BY {2} ASC", selectColumn, tableName, orderColumn))
            Dim dataItem As String = ""
            For Each item As DataRow In data.Tables(0).Rows
                dataItem += Convert.ToString(item(0)) & "|"
            Next
            Return dataItem.TrimEnd("|"c).Split("|"c)
        End Function

        Private Sub Spread_TextTipFetch(sender As Object, e As TextTipFetchEventArgs)
            If e.Column = introductionSheet.ColumnCount - 1 AndAlso Not e.ColumnHeader Then
                Dim productName As String = introductionSheet.Cells(e.Row, 1).Text
                e.TipText = "Place order for 10 units of {productName}"
                e.WrapText = True
                e.ShowTip = True
            End If
        End Sub

        Private Sub Spread_ButtonClicked(sender As Object, e As EditorNotifyEventArgs)
            If e.Column = introductionSheet.ColumnCount - 1 Then
                introductionSheet.Cells(e.Row, 6).Value = 10 + Convert.ToInt32(introductionSheet.Cells(e.Row, 6).Value)
            End If
        End Sub
    End Class
End Namespace

'
'
'

Namespace Features
	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 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.spread = New FarPoint.Win.Spread.FpSpread()
			Me.introductionSheet = New FarPoint.Win.Spread.SheetView()
			Me.pnlMain.SuspendLayout()
			DirectCast(Me.spread, System.ComponentModel.ISupportInitialize).BeginInit()
			DirectCast(Me.introductionSheet, System.ComponentModel.ISupportInitialize).BeginInit()
			Me.SuspendLayout()
			' 
			' pnlMain
			' 
			Me.pnlMain.Controls.Add(Me.spread)
			' 
			' spread
			' 
			Me.spread.AccessibleDescription = "spread, Introduction, Row 0, Column 0"
			Me.spread.Dock = System.Windows.Forms.DockStyle.Fill
			Me.spread.Location = New System.Drawing.Point(0, 0)
			Me.spread.Name = "spread"
			Me.spread.Sheets.AddRange(New FarPoint.Win.Spread.SheetView() {Me.introductionSheet})
			Me.spread.Size = New System.Drawing.Size(477, 487)
			Me.spread.TabIndex = 0
			' 
			' introductionSheet
			' 
			Me.introductionSheet.Reset()
			Me.introductionSheet.SheetName = "Introduction"
			' Formulas and custom names must be loaded with R1C1 reference style
			Me.introductionSheet.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1
			Me.introductionSheet.RowHeader.Columns.[Default].Width = 35F
			Me.introductionSheet.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.spread, System.ComponentModel.ISupportInitialize).EndInit()
			DirectCast(Me.introductionSheet, System.ComponentModel.ISupportInitialize).EndInit()
			Me.ResumeLayout(False)

		End Sub

		#End Region

		Private spread As FarPoint.Win.Spread.FpSpread
		Private introductionSheet As FarPoint.Win.Spread.SheetView
	End Class
End Namespace
