Imports System.Windows.Forms
Imports FarPoint.Excel
Imports Spread.Common.Features.Spread.Common.Features.Sample

Partial Public Class DocumentCaching
    Inherits BaseSampleControl
    Public Sub New()
        InitializeComponent()
        Name = "Document Caching"
        InitializeControls()
        SetupSpreadSheet()
    End Sub
    Private Sub InitializeControls()
        openComboBox.DroppedDown = False
        saveComboBox.DroppedDown = False
        For Each item As Object In [Enum].GetValues(GetType(ExcelSaveFlags))
            saveListBox.Items.Add(item)
        Next
        For Each item As Object In [Enum].GetValues(GetType(ExcelOpenFlags))
            openListBox.Items.Add(item)
        Next
    End Sub
    Private Sub PnlControls_MouseClick(sender As Object, e As MouseEventArgs)
        If Not saveListBox.Bounds.Contains(e.Location) Then
            saveListBox.Visible = False
        End If
        If Not openListBox.Bounds.Contains(e.Location) Then
            openListBox.Visible = False
        End If
    End Sub

    Private Sub SaveComboBox_MouseClick(sender As Object, e As MouseEventArgs)
        saveComboBox.DroppedDown = False
        saveListBox.Visible = Not saveListBox.Visible
    End Sub

    Private Sub OpenComboBox_MouseClick(sender As Object, e As MouseEventArgs)
        openComboBox.DroppedDown = False
        openListBox.Visible = Not openListBox.Visible
    End Sub

    Private Sub SetupSpreadSheet()
        spread.OpenExcel("Template\AnnualFinancialReport.xlsx", ExcelOpenFlags.NoFlagsSet)
    End Sub

    Private Sub FlowPanel_SizeChanged(sender As Object, e As EventArgs)
        For Each control As Control In flowPanel.Controls
            control.Width = flowPanel.Width - 10
        Next
    End Sub

    Private Sub saveButton_Click(sender As Object, e As EventArgs)
        saveListBox.Visible = False
        Dim saveFileDialog As New SaveFileDialog()
        saveFileDialog.Filter = "Excel 97-2003 Workboox(*.xls) | *.xls | Excel Workbook(*.xlsx) | *.xlsx"
        saveFileDialog.FilterIndex = -1
        If saveFileDialog.ShowDialog() = DialogResult.OK Then
            Dim filename As String = saveFileDialog.FileName
            spread.SaveExcel(filename, GetSaveFlag() Or ExcelSaveFlags.UseOOXMLFormat)
        End If
    End Sub

    Private Sub openButton_Click(sender As Object, e As EventArgs)
        openListBox.Visible = False
        Dim openFileDialog As New OpenFileDialog()
        openFileDialog.Filter = "Excel 97-2007 Workbook(*.xls)|*.xls|Excel Workbook(*.xlsx)|*.xlsx"
        openFileDialog.FilterIndex = 1
        openFileDialog.Multiselect = False
        If openFileDialog.ShowDialog() = DialogResult.OK Then
            Dim fileName As String = openFileDialog.FileName
            spread.OpenExcel(fileName, GetOpenFlag())
        End If
    End Sub
    Private Function GetSaveFlag() As ExcelSaveFlags
        Dim result As ExcelSaveFlags = ExcelSaveFlags.NoFlagsSet
        For Each item As Object In saveListBox.CheckedItems
            result = result Or CType(item, ExcelSaveFlags)
        Next
        Return result
    End Function
    Private Function GetOpenFlag() As ExcelOpenFlags
        Dim result As ExcelOpenFlags = ExcelOpenFlags.NoFlagsSet
        For Each item As Object In openListBox.CheckedItems
            result = result Or CType(item, ExcelOpenFlags)
        Next
        Return result
    End Function
End Class

'
'
'

Partial Class DocumentCaching
	''' <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.openComboBox = New System.Windows.Forms.ComboBox()
		Me.openListBox = New System.Windows.Forms.CheckedListBox()
		Me.openButton = New System.Windows.Forms.Button()
		Me.saveLabel = New System.Windows.Forms.Label()
		Me.saveComboBox = New System.Windows.Forms.ComboBox()
		Me.saveListBox = New System.Windows.Forms.CheckedListBox()
		Me.saveButton = New System.Windows.Forms.Button()
		Me.openLabel = New System.Windows.Forms.Label()
		Me.flowPanel = New System.Windows.Forms.FlowLayoutPanel()
		Me.spread = New FarPoint.Win.Spread.FpSpread()
		Me.sheetView = New FarPoint.Win.Spread.SheetView()
		Me.pnlMain.SuspendLayout()
		Me.pnlControls.SuspendLayout()
		Me.flowPanel.SuspendLayout()
		DirectCast(Me.spread, System.ComponentModel.ISupportInitialize).BeginInit()
		DirectCast(Me.sheetView, System.ComponentModel.ISupportInitialize).BeginInit()
		Me.SuspendLayout()
		' 
		' pnlMain
		' 
		Me.pnlMain.Controls.Add(Me.spread)
		Me.pnlMain.Size = New System.Drawing.Size(765, 487)
		' 
		' pnlControls
		' 
		Me.pnlControls.Controls.Add(Me.flowPanel)
		Me.pnlControls.Size = New System.Drawing.Size(253, 487)
		' 
		' openComboBox
		' 
		Me.openComboBox.DropDownHeight = 1
		Me.openComboBox.FormattingEnabled = True
		Me.openComboBox.IntegralHeight = False
		Me.openComboBox.Location = New System.Drawing.Point(3, 16)
		Me.openComboBox.Margin = New System.Windows.Forms.Padding(3, 3, 3, 0)
		Me.openComboBox.MinimumSize = New System.Drawing.Size(10, 0)
		Me.openComboBox.Name = "openComboBox"
		Me.openComboBox.Size = New System.Drawing.Size(239, 21)
		Me.openComboBox.TabIndex = 22
		AddHandler Me.openComboBox.MouseClick, New System.Windows.Forms.MouseEventHandler(AddressOf Me.OpenComboBox_MouseClick)
		' 
		' openListBox
		' 
		Me.openListBox.FormattingEnabled = True
		Me.openListBox.Location = New System.Drawing.Point(3, 37)
		Me.openListBox.Margin = New System.Windows.Forms.Padding(3, 0, 3, 3)
		Me.openListBox.Name = "openListBox"
		Me.openListBox.Size = New System.Drawing.Size(239, 94)
		Me.openListBox.TabIndex = 23
		Me.openListBox.Visible = False
		' 
		' openButton
		' 
		Me.openButton.Location = New System.Drawing.Point(3, 144)
		Me.openButton.Margin = New System.Windows.Forms.Padding(3, 10, 3, 10)
		Me.openButton.Name = "openButton"
		Me.openButton.Size = New System.Drawing.Size(239, 23)
		Me.openButton.TabIndex = 19
		Me.openButton.Text = "Open Excel"
		Me.openButton.UseVisualStyleBackColor = True
		AddHandler Me.openButton.Click, New System.EventHandler(AddressOf Me.openButton_Click)
		' 
		' saveLabel
		' 
		Me.saveLabel.Location = New System.Drawing.Point(3, 187)
		Me.saveLabel.Margin = New System.Windows.Forms.Padding(3, 10, 3, 0)
		Me.saveLabel.Name = "saveLabel"
		Me.saveLabel.Size = New System.Drawing.Size(239, 13)
		Me.saveLabel.TabIndex = 20
		Me.saveLabel.Text = "Save Option:"
		' 
		' saveComboBox
		' 
		Me.saveComboBox.DropDownHeight = 1
		Me.saveComboBox.FormattingEnabled = True
		Me.saveComboBox.IntegralHeight = False
		Me.saveComboBox.Location = New System.Drawing.Point(3, 203)
		Me.saveComboBox.Margin = New System.Windows.Forms.Padding(3, 3, 3, 0)
		Me.saveComboBox.Name = "saveComboBox"
		Me.saveComboBox.Size = New System.Drawing.Size(239, 21)
		Me.saveComboBox.TabIndex = 24
		AddHandler Me.saveComboBox.MouseClick, New System.Windows.Forms.MouseEventHandler(AddressOf Me.SaveComboBox_MouseClick)
		' 
		' saveListBox
		' 
		Me.saveListBox.FormattingEnabled = True
		Me.saveListBox.Location = New System.Drawing.Point(3, 224)
		Me.saveListBox.Margin = New System.Windows.Forms.Padding(3, 0, 3, 3)
		Me.saveListBox.Name = "saveListBox"
		Me.saveListBox.Size = New System.Drawing.Size(239, 94)
		Me.saveListBox.TabIndex = 25
		Me.saveListBox.Visible = False
		' 
		' saveButton
		' 
		Me.saveButton.Location = New System.Drawing.Point(3, 331)
		Me.saveButton.Margin = New System.Windows.Forms.Padding(3, 10, 3, 3)
		Me.saveButton.Name = "saveButton"
		Me.saveButton.Size = New System.Drawing.Size(239, 23)
		Me.saveButton.TabIndex = 21
		Me.saveButton.Text = "Save Excel"
		Me.saveButton.UseVisualStyleBackColor = True
		AddHandler Me.saveButton.Click, New System.EventHandler(AddressOf Me.saveButton_Click)
		' 
		' openLabel
		' 
		Me.openLabel.Location = New System.Drawing.Point(3, 0)
		Me.openLabel.Name = "openLabel"
		Me.openLabel.Size = New System.Drawing.Size(239, 13)
		Me.openLabel.TabIndex = 18
		Me.openLabel.Text = "Open Option:"
		' 
		' flowPanel
		' 
		Me.flowPanel.Controls.Add(Me.openLabel)
		Me.flowPanel.Controls.Add(Me.openComboBox)
		Me.flowPanel.Controls.Add(Me.openListBox)
		Me.flowPanel.Controls.Add(Me.openButton)
		Me.flowPanel.Controls.Add(Me.saveLabel)
		Me.flowPanel.Controls.Add(Me.saveComboBox)
		Me.flowPanel.Controls.Add(Me.saveListBox)
		Me.flowPanel.Controls.Add(Me.saveButton)
		Me.flowPanel.Dock = System.Windows.Forms.DockStyle.Fill
		Me.flowPanel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown
		Me.flowPanel.Location = New System.Drawing.Point(0, 0)
		Me.flowPanel.Name = "flowPanel"
		Me.flowPanel.Size = New System.Drawing.Size(253, 487)
		Me.flowPanel.TabIndex = 0
		AddHandler Me.flowPanel.SizeChanged, New System.EventHandler(AddressOf Me.FlowPanel_SizeChanged)
		AddHandler Me.flowPanel.MouseClick, New System.Windows.Forms.MouseEventHandler(AddressOf Me.PnlControls_MouseClick)
		' 
		' spread
		' 
		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.sheetView})
		Me.spread.Size = New System.Drawing.Size(765, 487)
		Me.spread.TabIndex = 0
		' 
		' sheetView
		' 
		Me.sheetView.Reset()
		Me.sheetView.SheetName = "DocumentCaching"
		' Formulas and custom names must be loaded with R1C1 reference style
		Me.sheetView.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1
		Me.sheetView.RowHeader.Columns.[Default].Width = 35F
		Me.sheetView.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1
		' 
		' DocumentCaching
		' 
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.Name = "DocumentCaching"
		Me.Size = New System.Drawing.Size(1019, 487)
		Me.pnlMain.ResumeLayout(False)
		Me.pnlControls.ResumeLayout(False)
		Me.flowPanel.ResumeLayout(False)
		DirectCast(Me.spread, System.ComponentModel.ISupportInitialize).EndInit()
		DirectCast(Me.sheetView, System.ComponentModel.ISupportInitialize).EndInit()
		Me.ResumeLayout(False)

	End Sub

	#End Region
	Private openComboBox As System.Windows.Forms.ComboBox
	Private openListBox As System.Windows.Forms.CheckedListBox
	Private openButton As System.Windows.Forms.Button
	Private saveLabel As System.Windows.Forms.Label
	Private saveComboBox As System.Windows.Forms.ComboBox
	Private saveListBox As System.Windows.Forms.CheckedListBox
	Private saveButton As System.Windows.Forms.Button
	Private openLabel As System.Windows.Forms.Label
	Private flowPanel As System.Windows.Forms.FlowLayoutPanel
	Private spread As FarPoint.Win.Spread.FpSpread
	Private sheetView As FarPoint.Win.Spread.SheetView
End Class
