Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Imports FarPoint.Win.Spread
Imports FarPoint.Win.Spread.Model
Imports Spread.Common.Features.Spread.Common.Features.Sample
Imports Spread.FloatingObjects.Spread.FloatingObjects

Namespace Features
    Partial Public Class ChildControls
        Inherits BaseSampleControl
        Private _instanceCounters As Dictionary(Of String, Integer)

        Public Sub New()
            InitializeComponent()
            Name = "Child Controls"

            _instanceCounters = New Dictionary(Of String, Integer)() From {
                {"SpreadButton", 0},
                {"SpreadTextBox", 0},
                {"SpreadCheckBox", 0},
                {"SpreadRadioButton", 0},
                {"SpreadComboBox", 0}
            }

            comboControls.SelectedIndex = -1

            comboControls.Items.AddRange(New Object() {"Button", "TextBox", "CheckBox", "RadioButton", "ComboBox"})

            InitSpreadSheet(fpSpread.ActiveSheet)

            AddHandler comboControls.SelectedIndexChanged, AddressOf ComboControls_SelectedIndexChanged
            AddHandler btnAddControl.Click, AddressOf BtnAddControl_Click
            AddHandler fpSpread.EnterCell, AddressOf FpSpread_EnterCell
        End Sub

        Private Sub InitSpreadSheet(sheet As SheetView)
            fpSpread.ActiveSheet.SetMultipleRowHeights(0, fpSpread.ActiveSheet.RowCount, 25)
            fpSpread.ActiveSheet.SetMultipleColumnWidths(0, fpSpread.ActiveSheet.ColumnCount, 90)

            AddSpreadControl(sheet, GetType(SpreadButton), 1, 1)
            AddSpreadControl(sheet, GetType(SpreadTextBox), 1, 3)
            AddSpreadControl(sheet, GetType(SpreadCheckBox), 1, 5)
            AddSpreadControl(sheet, GetType(SpreadRadioButton), 3, 2)
            AddSpreadControl(sheet, GetType(SpreadComboBox), 3, 4)
        End Sub

#Region "Event handlers"
        Private Sub FpSpread_EnterCell(sender As Object, e As EnterCellEventArgs)
            Dim rectangle As Rectangle = fpSpread.GetCellRectangle(0, 0, e.Row, e.Column)
            Dim control As Control = fpSpread.GetChildAtPoint(rectangle.Location)
            propertyGrid.SelectedObject = control
        End Sub

        Private Sub ComboControls_SelectedIndexChanged(sender As Object, e As EventArgs)
            btnAddControl.Enabled = (comboControls.SelectedItem IsNot Nothing)
        End Sub

        Private Sub BtnAddControl_Click(sender As Object, e As EventArgs)
            Dim sheet As SheetView = fpSpread.ActiveSheet

            Dim controlName As String = comboControls.SelectedItem.ToString()

            Dim type As Type = Nothing
            Select Case controlName
                Case "Button"
                    type = GetType(SpreadButton)
                    Exit Select
                Case "TextBox"
                    type = GetType(SpreadTextBox)
                    Exit Select
                Case "CheckBox"
                    type = GetType(SpreadCheckBox)
                    Exit Select
                Case "RadioButton"
                    type = GetType(SpreadRadioButton)
                    Exit Select
                Case "ComboBox"
                    type = GetType(SpreadComboBox)
                    Exit Select
            End Select

            Dim range As CellRange = fpSpread.ActiveSheet.GetSelection(0)
            Dim control As Control = Nothing
            If range.IsValidRange(fpSpread.ActiveSheet) Then
                For row As Integer = range.Row To range.Row + (range.RowCount - 1)
                    For col As Integer = range.Column To range.Column + (range.ColumnCount - 1)
                        control = AddSpreadControl(sheet, type, row, col)
                    Next
                Next
            End If
            propertyGrid.SelectedObject = control
        End Sub

        Private Function AddSpreadControl(sheet As SheetView, type As Type, row As Integer, col As Integer) As Control
            Dim control As Control = TryCast(Activator.CreateInstance(type.Assembly.FullName, type.FullName).Unwrap(), Control)
            _instanceCounters(type.Name) = _instanceCounters(type.Name) + 1
            control.Name = " " & type.Name & "" & _instanceCounters(type.Name) & " "
            control.Text = control.Name
            control.Size = New Size(CInt(Math.Truncate(sheet.Columns(col).Width)), CInt(Math.Truncate(sheet.Rows(row).Height)))
            AddHandler control.GotFocus, AddressOf Control_GotFocus
            control.Left = 50
            control.Top = 75

            fpSpread.AddControl(control, fpSpread.ActiveSheetIndex, row, col)
            listBox.Items.Add("Added: " & control.Name & " ")

            Return control
        End Function

        Private Sub Control_GotFocus(sender As Object, e As EventArgs)
            propertyGrid.SelectedObject = sender
        End Sub
#End Region
    End Class
End Namespace

'
'ChildControls.Designer.vb
'

Namespace Features
	Partial Class ChildControls
		''' <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 Overloads 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.comboControls = New System.Windows.Forms.ComboBox()
			Me.label1 = New System.Windows.Forms.Label()
			Me.listBox = New System.Windows.Forms.ListBox()
			Me.propertyGrid = New System.Windows.Forms.PropertyGrid()
			Me.fpSpread = New FarPoint.Win.Spread.FpSpread()
			Me.fpSpread_Sheet1 = New FarPoint.Win.Spread.SheetView()
			Me.btnAddControl = New System.Windows.Forms.Button()
			Me.pnlMain.SuspendLayout()
			Me.pnlControls.SuspendLayout()
			DirectCast(Me.fpSpread, System.ComponentModel.ISupportInitialize).BeginInit()
			DirectCast(Me.fpSpread_Sheet1, System.ComponentModel.ISupportInitialize).BeginInit()
			Me.SuspendLayout()
			' 
			' pnlMain
			' 
			Me.pnlMain.Controls.Add(Me.fpSpread)
			' 
			' pnlControls
			' 
			Me.pnlControls.Controls.Add(Me.btnAddControl)
			Me.pnlControls.Controls.Add(Me.propertyGrid)
			Me.pnlControls.Controls.Add(Me.listBox)
			Me.pnlControls.Controls.Add(Me.label1)
			Me.pnlControls.Controls.Add(Me.comboControls)
			' 
			' comboControls
			' 
			Me.comboControls.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.comboControls.FormattingEnabled = True
			Me.comboControls.Location = New System.Drawing.Point(10, 35)
			Me.comboControls.Name = "comboControls"
			Me.comboControls.Size = New System.Drawing.Size(133, 21)
			Me.comboControls.TabIndex = 0
			' 
			' label1
			' 
			Me.label1.AutoSize = True
			Me.label1.Location = New System.Drawing.Point(10, 16)
			Me.label1.Name = "label1"
			Me.label1.Size = New System.Drawing.Size(76, 13)
			Me.label1.TabIndex = 1
			Me.label1.Text = "Select Control:"
			' 
			' listBox
			' 
			Me.listBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.listBox.FormattingEnabled = True
			Me.listBox.Location = New System.Drawing.Point(10, 100)
			Me.listBox.Name = "listBox"
			Me.listBox.Size = New System.Drawing.Size(133, 134)
			Me.listBox.TabIndex = 2
			' 
			' propertyGrid
			' 
			Me.propertyGrid.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.propertyGrid.Location = New System.Drawing.Point(10, 252)
			Me.propertyGrid.Name = "propertyGrid"
			Me.propertyGrid.Size = New System.Drawing.Size(133, 221)
			Me.propertyGrid.TabIndex = 3
			' 
			' fpSpread
			' 
			Me.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill
			Me.fpSpread.Location = New System.Drawing.Point(0, 0)
			Me.fpSpread.Name = "fpSpread"
			Me.fpSpread.Sheets.AddRange(New FarPoint.Win.Spread.SheetView() {Me.fpSpread_Sheet1})
			Me.fpSpread.Size = New System.Drawing.Size(477, 487)
			Me.fpSpread.Skin = FarPoint.Win.Spread.DefaultSpreadSkins.Office2016White
			Me.fpSpread.StatusBarVisible = True
			Me.fpSpread.TabIndex = 0
			' 
			' fpSpread_Sheet1
			' 
			Me.fpSpread_Sheet1.Reset()
			Me.fpSpread_Sheet1.SheetName = "Sheet1"
			' Formulas and custom names must be loaded with R1C1 reference style
			Me.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1
			Me.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1
			' 
			' btnAddControl
			' 
			Me.btnAddControl.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.btnAddControl.Enabled = False
			Me.btnAddControl.Location = New System.Drawing.Point(36, 64)
			Me.btnAddControl.Name = "btnAddControl"
			Me.btnAddControl.Size = New System.Drawing.Size(75, 23)
			Me.btnAddControl.TabIndex = 4
			Me.btnAddControl.Text = "Add Control"
			Me.btnAddControl.UseVisualStyleBackColor = True
			' 
			' ChildControls
			' 
			Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
			Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
			Me.Name = "ChildControls"
			Me.pnlMain.ResumeLayout(False)
			Me.pnlControls.ResumeLayout(False)
			Me.pnlControls.PerformLayout()
			DirectCast(Me.fpSpread, System.ComponentModel.ISupportInitialize).EndInit()
			DirectCast(Me.fpSpread_Sheet1, System.ComponentModel.ISupportInitialize).EndInit()
			Me.ResumeLayout(False)

		End Sub

		#End Region

		Private comboControls As System.Windows.Forms.ComboBox
		Private fpSpread As FarPoint.Win.Spread.FpSpread
		Private fpSpread_Sheet1 As FarPoint.Win.Spread.SheetView
		Private propertyGrid As System.Windows.Forms.PropertyGrid
		Private listBox As System.Windows.Forms.ListBox
		Private label1 As System.Windows.Forms.Label
		Private btnAddControl As System.Windows.Forms.Button
	End Class
End Namespace
