Imports System.Drawing
Imports System.Windows.Forms
Imports Spread.Common.DataStore
Imports FarPoint.Win.Spread.Model
Imports Spread.Common.Features.Spread.Common.Features.Sample

Namespace Features
    Partial Public Class DragDropDemo
        Inherits BaseSampleControl
        Private s As String
        Private _range As CellRange
        Private _currentSelection As CellRange
        Public Sub New()
            InitializeComponent()
            InitializeControls()
            SetupSpreadSheet()
        End Sub
        Private Sub InitializeControls()
            Name = "Drag and Drop"
            spreadDestination.Sheets(0).Rows.[Default].Height = 25
            spreadSource.Sheets(0).Rows.[Default].Height = 25
            spreadDestination.AllowDrop = True
        End Sub
        Private Sub SetupSpreadSheet()
            spreadSource.DataSource = New DataService().AccessDataSource.GetNWINDData("Customers")
            sheetViewDestination.Columns.Count = sheetViewSource.Columns.Count
            For _column As Integer = 0 To sheetViewSource.Columns.Count - 1
                sheetViewDestination.Columns(_column).Width = sheetViewSource.Columns(_column).Width
            Next
        End Sub

        Private Sub SpreadDestination_DragOver(sender As Object, e As DragEventArgs)
            Dim _point As Point
            Dim _previousSelection As CellRange

            _point = spreadDestination.PointToClient(New Point(e.X, e.Y))
            _currentSelection = spreadDestination.GetCellFromPixel(0, 0, _point.X, _point.Y)
            If _currentSelection.ColumnCount <> -1 AndAlso _currentSelection.RowCount <> -1 Then
                _previousSelection = sheetViewDestination.GetSelection(0)

                If Not (_previousSelection Is Nothing) Then
                    sheetViewDestination.RemoveSelection(_previousSelection.Row, _previousSelection.Column, _previousSelection.RowCount, _previousSelection.ColumnCount)
                End If

                sheetViewDestination.AddSelection(_currentSelection.Row, _currentSelection.Column, _range.RowCount, _range.ColumnCount)
            End If
        End Sub
        Private Sub SpreadDestination_DragDrop(sender As Object, e As DragEventArgs)
            Dim _selection As CellRange
            _selection = sheetViewDestination.GetSelection(0)
            If _currentSelection IsNot Nothing Then
                sheetViewDestination.SetClip(_selection.Row, _selection.Column, _selection.RowCount, _selection.ColumnCount, s)
                sheetViewSource.RemoveSelection(_range.Row, _range.Column, _range.RowCount, _range.ColumnCount)
            End If
        End Sub
        Private Sub SpreadDestination_DragEnter(sender As Object, e As DragEventArgs)
            e.Effect = DragDropEffects.Move
        End Sub

        Private Sub SpreadSource_MouseDown(sender As Object, e As MouseEventArgs)
            If e.Button = MouseButtons.Right AndAlso s IsNot Nothing Then
                spreadSource.DoDragDrop(s, (DragDropEffects.Copy Or DragDropEffects.Move))
            End If
        End Sub
        Private Sub SpreadSource_MouseUp(sender As Object, e As MouseEventArgs)
            Try
                _range = sheetViewSource.GetSelection(0)
                If _range Is Nothing Then
                    Return
                End If

                s = sheetViewSource.GetClip(_range.Row, _range.Column, _range.RowCount, _range.ColumnCount)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try
            indexTextBox.Text = String.Format("Row[{0}],Col[{1}]", _range.Row, _range.Column)
            countTextBox.Text = String.Format("RowCount[{0}],ColumnCount[{1}]", _range.RowCount, _range.ColumnCount)

        End Sub

        Private Sub resetButton_Click(sender As Object, e As EventArgs)
            spreadDestination.Reset()
            spreadDestination.AllowDrop = True
            For _column As Integer = 0 To sheetViewSource.Columns.Count - 1
                sheetViewDestination.Columns(_column).Width = sheetViewSource.Columns(_column).Width
            Next
        End Sub

        Public Overrides Function GetImage(Optional width As Integer = 200, Optional height As Integer = 200) As Image
            spreadDestination.AllowDrop = False
            Return MyBase.GetImage(width, height)
        End Function
    End Class
End Namespace

'
'DragDropDemo.Designer.vb
'

Namespace Features
	Partial Class DragDropDemo
		''' <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.tablePanel = New System.Windows.Forms.TableLayoutPanel()
			Me.spreadSource = New FarPoint.Win.Spread.FpSpread()
			Me.sheetViewSource = New FarPoint.Win.Spread.SheetView()
			Me.spreadDestination = New FarPoint.Win.Spread.FpSpread()
			Me.sheetViewDestination = New FarPoint.Win.Spread.SheetView()
			Me.selectionLabel = New System.Windows.Forms.Label()
			Me.indexTextBox = New System.Windows.Forms.TextBox()
			Me.countTextBox = New System.Windows.Forms.TextBox()
			Me.resetButton = New System.Windows.Forms.Button()
			Me.detailTextBox = New System.Windows.Forms.TextBox()
			Me.pnlMain.SuspendLayout()
			Me.pnlControls.SuspendLayout()
			Me.tablePanel.SuspendLayout()
			DirectCast(Me.spreadSource, System.ComponentModel.ISupportInitialize).BeginInit()
			DirectCast(Me.sheetViewSource, System.ComponentModel.ISupportInitialize).BeginInit()
			DirectCast(Me.spreadDestination, System.ComponentModel.ISupportInitialize).BeginInit()
			DirectCast(Me.sheetViewDestination, System.ComponentModel.ISupportInitialize).BeginInit()
			Me.SuspendLayout()
			' 
			' pnlMain
			' 
			Me.pnlMain.Controls.Add(Me.tablePanel)
			' 
			' pnlControls
			' 
			Me.pnlControls.Controls.Add(Me.detailTextBox)
			Me.pnlControls.Controls.Add(Me.resetButton)
			Me.pnlControls.Controls.Add(Me.countTextBox)
			Me.pnlControls.Controls.Add(Me.indexTextBox)
			Me.pnlControls.Controls.Add(Me.selectionLabel)
			' 
			' tablePanel
			' 
			Me.tablePanel.ColumnCount = 1
			Me.tablePanel.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F))
			Me.tablePanel.Controls.Add(Me.spreadSource, 0, 0)
			Me.tablePanel.Controls.Add(Me.spreadDestination, 0, 1)
			Me.tablePanel.Dock = System.Windows.Forms.DockStyle.Fill
			Me.tablePanel.Location = New System.Drawing.Point(0, 0)
			Me.tablePanel.Name = "tablePanel"
			Me.tablePanel.RowCount = 2
			Me.tablePanel.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F))
			Me.tablePanel.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F))
			Me.tablePanel.Size = New System.Drawing.Size(477, 487)
			Me.tablePanel.TabIndex = 0
			' 
			' spreadSource
			' 
			Me.spreadSource.Dock = System.Windows.Forms.DockStyle.Fill
			Me.spreadSource.Location = New System.Drawing.Point(3, 3)
			Me.spreadSource.Name = "spreadSource"
			Me.spreadSource.Sheets.AddRange(New FarPoint.Win.Spread.SheetView() {Me.sheetViewSource})
			Me.spreadSource.Size = New System.Drawing.Size(471, 237)
			Me.spreadSource.TabIndex = 0
			AddHandler Me.spreadSource.MouseDown, New System.Windows.Forms.MouseEventHandler(AddressOf Me.SpreadSource_MouseDown)
			AddHandler Me.spreadSource.MouseUp, New System.Windows.Forms.MouseEventHandler(AddressOf Me.SpreadSource_MouseUp)
			' 
			' sheetViewSource
			' 
			Me.sheetViewSource.Reset()
			Me.sheetViewSource.SheetName = "Sheet1"
			' Formulas and custom names must be loaded with R1C1 reference style
			Me.sheetViewSource.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1
			Me.sheetViewSource.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1
			' 
			' spreadDestination
			' 
			Me.spreadDestination.Dock = System.Windows.Forms.DockStyle.Fill
			Me.spreadDestination.Location = New System.Drawing.Point(3, 246)
			Me.spreadDestination.Name = "spreadDestination"
			Me.spreadDestination.Sheets.AddRange(New FarPoint.Win.Spread.SheetView() {Me.sheetViewDestination})
			Me.spreadDestination.Size = New System.Drawing.Size(471, 238)
			Me.spreadDestination.TabIndex = 1
			AddHandler Me.spreadDestination.DragDrop, New System.Windows.Forms.DragEventHandler(AddressOf Me.SpreadDestination_DragDrop)
			AddHandler Me.spreadDestination.DragEnter, New System.Windows.Forms.DragEventHandler(AddressOf Me.SpreadDestination_DragEnter)
			AddHandler Me.spreadDestination.DragOver, New System.Windows.Forms.DragEventHandler(AddressOf Me.SpreadDestination_DragOver)
			' 
			' sheetViewDestination
			' 
			Me.sheetViewDestination.Reset()
			Me.sheetViewDestination.SheetName = "Sheet1"
			' Formulas and custom names must be loaded with R1C1 reference style
			Me.sheetViewDestination.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1
			Me.sheetViewDestination.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1
			' 
			' selectionLabel
			' 
			Me.selectionLabel.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.selectionLabel.AutoSize = True
			Me.selectionLabel.ForeColor = System.Drawing.Color.Blue
			Me.selectionLabel.Location = New System.Drawing.Point(2, 13)
			Me.selectionLabel.Name = "selectionLabel"
			Me.selectionLabel.Size = New System.Drawing.Size(96, 13)
			Me.selectionLabel.TabIndex = 0
			Me.selectionLabel.Text = "You have selected"
			' 
			' indexTextBox
			' 
			Me.indexTextBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.indexTextBox.BackColor = System.Drawing.SystemColors.Window
			Me.indexTextBox.Location = New System.Drawing.Point(5, 47)
			Me.indexTextBox.Name = "indexTextBox"
			Me.indexTextBox.[ReadOnly] = True
			Me.indexTextBox.Size = New System.Drawing.Size(130, 20)
			Me.indexTextBox.TabIndex = 1
			' 
			' countTextBox
			' 
			Me.countTextBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.countTextBox.BackColor = System.Drawing.SystemColors.Window
			Me.countTextBox.Location = New System.Drawing.Point(5, 85)
			Me.countTextBox.Name = "countTextBox"
			Me.countTextBox.[ReadOnly] = True
			Me.countTextBox.Size = New System.Drawing.Size(130, 20)
			Me.countTextBox.TabIndex = 2
			' 
			' resetButton
			' 
			Me.resetButton.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.resetButton.Location = New System.Drawing.Point(5, 122)
			Me.resetButton.Name = "resetButton"
			Me.resetButton.Size = New System.Drawing.Size(130, 23)
			Me.resetButton.TabIndex = 3
			Me.resetButton.Text = "Reset"
			Me.resetButton.UseVisualStyleBackColor = True
			AddHandler Me.resetButton.Click, New System.EventHandler(AddressOf Me.resetButton_Click)
			' 
			' detailTextBox
			' 
			Me.detailTextBox.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
			Me.detailTextBox.BackColor = System.Drawing.SystemColors.Window
			Me.detailTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None
			Me.detailTextBox.ForeColor = System.Drawing.Color.FromArgb(CInt(CByte(0)), CInt(CByte(0)), CInt(CByte(192)))
			Me.detailTextBox.Location = New System.Drawing.Point(6, 152)
			Me.detailTextBox.Multiline = True
			Me.detailTextBox.Name = "detailTextBox"
			Me.detailTextBox.[ReadOnly] = True
			Me.detailTextBox.Size = New System.Drawing.Size(129, 319)
			Me.detailTextBox.TabIndex = 4
			Me.detailTextBox.Text = "The row and columns are 0-based.  The selection above represents the starting row" & ", starting column, the number of rows selected and the number of columns selecte" & "d."
			' 
			' DragDropDemo
			' 
			Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
			Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
			Me.Name = "DragDropDemo"
			Me.pnlMain.ResumeLayout(False)
			Me.pnlControls.ResumeLayout(False)
			Me.pnlControls.PerformLayout()
			Me.tablePanel.ResumeLayout(False)
			DirectCast(Me.spreadSource, System.ComponentModel.ISupportInitialize).EndInit()
			DirectCast(Me.sheetViewSource, System.ComponentModel.ISupportInitialize).EndInit()
			DirectCast(Me.spreadDestination, System.ComponentModel.ISupportInitialize).EndInit()
			DirectCast(Me.sheetViewDestination, System.ComponentModel.ISupportInitialize).EndInit()
			Me.ResumeLayout(False)

		End Sub

		#End Region
		Private tablePanel As System.Windows.Forms.TableLayoutPanel
		Private spreadSource As FarPoint.Win.Spread.FpSpread
		Private sheetViewSource As FarPoint.Win.Spread.SheetView
		Private resetButton As System.Windows.Forms.Button
		Private countTextBox As System.Windows.Forms.TextBox
		Private indexTextBox As System.Windows.Forms.TextBox
		Private selectionLabel As System.Windows.Forms.Label
		Private spreadDestination As FarPoint.Win.Spread.FpSpread
		Private sheetViewDestination As FarPoint.Win.Spread.SheetView
		Private detailTextBox As System.Windows.Forms.TextBox
	End Class
End Namespace
