f6a4
f6a4

Reputation: 1782

Why is drag and drop only working in ISE, do I miss an assembly? (Windows forms in powershell)

I have a larger powershell application which is using drag & drop to process quotes . All is working fine, but only in ISE. When I'm using the script by shortcut on the desktop (C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -NoProfile -file "D:\Powershell\test\dragndrop.ps1") drag and drop is not working.

The error message is:

Type [System.Windows.DragDropEffects] not found

My powershell version:

Name                           Value                                                                                                                                                                                                
----                           -----                                                                                                                                                                                                
PSVersion                      5.1.18362.145                                                                                                                                                                                        
PSEdition                      Desktop                                                                                                                                                                                              
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                              
BuildVersion                   10.0.18362.145                                                                                                                                                                                       
CLRVersion                     4.0.30319.42000                                                                                                                                                                                      
WSManStackVersion              3.0                                                                                                                                                                                                  
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                  
SerializationVersion           1.1.0.1            

Running on Windows 10 64 bit, 1903

This is the form I'm using with the application. It's modified so you can copy & paste the script for testing without any modification.

Add-Type -AssemblyName System.Collections
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName Microsoft.Office.Interop.Excel


[System.Windows.Forms.Application]::EnableVisualStyles();

    $form = New-Object System.Windows.Forms.Form 
    $form.Visible = $false
    [void]$form.SuspendLayout()
    $form.ClientSize = New-Object System.Drawing.Size(320,380) 
    $form.StartPosition = 'CenterScreen'
    $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
    $form.MaximizeBox = $false
    $form.MinimizeBox = $false
    $form.HelpButton  = $true

    $CTRL_TabCtrl = New-Object System.Windows.Forms.TabControl
    $CTRL_TabCtrl.Location = New-Object System.Drawing.Point(5,5)
    $CTRL_TabCtrl.Size = New-Object System.Drawing.Size(310,370)
    [void]$form.Controls.Add($CTRL_TabCtrl)

    $CTRL_Tab1 = New-Object System.Windows.Forms.TabPage
    $CTRL_Tab1.AutoSize = $true
    $CTRL_Tab1.Text = 'Chose Quote'
    $CTRL_Tab1.TabIndex = 1
    [void]$CTRL_TabCtrl.Controls.Add($CTRL_Tab1)

    $CTRL_Tab2 = New-Object System.Windows.Forms.TabPage
    $CTRL_Tab2.AutoSize = $true
    $CTRL_Tab2.Text = 'Settings'
    $CTRL_Tab2.TabIndex = 2
    [void]$CTRL_TabCtrl.Controls.Add($CTRL_Tab2)

    $CTRL_label1 = New-Object System.Windows.Forms.Label
    $CTRL_label1.Location = New-Object System.Drawing.Point(10,10) 
    $CTRL_label1.Size = New-Object System.Drawing.Size(280,20) 
    $CTRL_label1.Text = 'Please select quote (PDF or Excel):'
    $CTRL_label1.Name = 'Label1'
    $CTRL_label1.Add_MouseHover( $showHelp )
    [void]$CTRL_Tab1.Controls.Add($CTRL_label1) 

    $CTRL_PathQuote = New-Object System.Windows.Forms.TextBox 
    $CTRL_PathQuote.Location = New-Object System.Drawing.Point(10,30) 
    $CTRL_PathQuote.Size = New-Object System.Drawing.Size(275,20) 
    $CTRL_PathQuote.MaxLength = 250
    $CTRL_PathQuote.Name = 'PathQuote'
    $CTRL_PathQuote.Add_MouseHover( $showHelp )
    [void]$CTRL_Tab1.Controls.Add($CTRL_PathQuote) 

    $CTRL_UploadButton = New-Object System.Windows.Forms.Button
    $CTRL_UploadButton.Location = New-Object System.Drawing.Point(10,60)
    $CTRL_UploadButton.Size = New-Object System.Drawing.Size(120,23)
    $CTRL_UploadButton.Text = 'Select File...'
    $CTRL_UploadButton.Name = 'UploadButton'
    $CTRL_UploadButton.Add_MouseHover( $showHelp )
    #$CTRL_UploadButton.Add_Click( { Get-FileName-Dialog -textBox $CTRL_PathQuote } )
    [void]$CTRL_Tab1.Controls.Add($CTRL_UploadButton)

    $CTRL_label3 = New-Object System.Windows.Forms.Label
    $CTRL_label3.Location = New-Object System.Drawing.Point(10,100) 
    $CTRL_label3.Size = New-Object System.Drawing.Size(280,20) 
    $CTRL_label3.Text = 'Or drop quote here: (drag && drop):'
    $CTRL_label3.Name = 'Label3'
    $CTRL_label3.Add_MouseHover( $showHelp )
    [void]$CTRL_Tab1.Controls.Add($CTRL_label3) 

    $CTRL_PictureBox = New-Object Windows.Forms.PictureBox
    $CTRL_PictureBox.Location = New-Object System.Drawing.Point(10,120)
    $CTRL_PictureBox.Size = New-Object System.Drawing.Size(280,174)
    $CTRL_PictureBox.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
    $CTRL_PictureBox.AllowDrop = $true

    $CTRL_PictureBox.Add_DragOver({
        if ($_.Data.GetDataPresent([System.Windows.Forms.DataFormats]::FileDrop)) {
            foreach ($filename in $_.Data.GetData([System.Windows.Forms.DataFormats]::FileDrop)) {
                if( @( '.pdf', '.xlsx').Contains( [System.IO.Path]::GetExtension($filename).ToLower() ) ) {
                    $_.Effect = [System.Windows.DragDropEffects]::Copy
                    $CTRL_PictureBox.BackColor = [System.Drawing.Color]::DarkGray
                }
                else {
                    $_.Effect = [System.Windows.DragDropEffects]::None
                    $CTRL_PictureBox.BackColor = [System.Drawing.Color]::Transparent
                }
                break #only first file
            }
        }
    })

    $CTRL_PictureBox.Add_DragDrop({
        if ($_.Data.GetDataPresent([System.Windows.Forms.DataFormats]::FileDrop)) {
            foreach ($filename in $_.Data.GetData([System.Windows.Forms.DataFormats]::FileDrop)) {
                if( @( '.pdf', '.xlsx').Contains( [System.IO.Path]::GetExtension($filename).ToLower() ) ) {
                    $_.Effect = [System.Windows.DragDropEffects]::All
                    $CTRL_PathQuote.Text   = $filename
                }
                else {
                    $_.Effect = [System.Windows.DragDropEffects]::None
                }
                break #only first file
            }
        }
    })

    [void]$CTRL_Tab1.Controls.Add($CTRL_PictureBox)

    $CTRL_OKButton1 = New-Object System.Windows.Forms.Button
    $CTRL_OKButton1.Location = New-Object System.Drawing.Point(70,310)
    $CTRL_OKButton1.Size = New-Object System.Drawing.Size(75,23)
    $CTRL_OKButton1.Text = 'Start'
    $CTRL_OKButton1.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.AcceptButton = $CTRL_OKButton1
    [void]$CTRL_Tab1.Controls.Add($CTRL_OKButton1)
    $CTRL_OKButton1.Enabled = $false

    $CTRL_CancelButton1 = New-Object System.Windows.Forms.Button
    $CTRL_CancelButton1.Location = New-Object System.Drawing.Point(150,310)
    $CTRL_CancelButton1.Size = New-Object System.Drawing.Size(75,23)
    $CTRL_CancelButton1.Text = 'Abbrechen'
    $CTRL_CancelButton1.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.CancelButton = $CTRL_CancelButton1
    [void]$CTRL_Tab1.Controls.Add($CTRL_CancelButton1)



    $scriptBlock_OkButton1        = {   $CTRL_PathQuote.Text.Trim().Length -gt 0 -and (Test-Path -Path $CTRL_PathQuote.Text -PathType leaf) }

    $scriptBlock_PathQuoteChanged = {
                                        $CTRL_OKButton1.Enabled = & $scriptBlock_OkButton1; 
                                        $CTRL_PictureBox.BackColor = [System.Drawing.Color]::Transparent
                                        if( !$CTRL_OKButton1.Enabled ) {
                                            $CTRL_PictureBox.Image = $dragnDropImg
                                        }
                                        else {
                                            $CTRL_PictureBox.Image = $dragnDropOkImg
                                        }
                                    }


    $CTRL_OKButton1.Enabled = & $scriptBlock_OkButton1 

    [void]$CTRL_PathQuote.Add_TextChanged( { & $scriptBlock_PathQuoteChanged } )
    [void]$CTRL_PictureBox.Add_DragLeave( { & $scriptBlock_PathQuoteChanged } )

    [void]$form.ResumeLayout()

    $userInput = $form.ShowDialog()

Upvotes: 1

Views: 160

Answers (1)

Sid
Sid

Reputation: 2676

Use this [System.Windows.Forms.DragDropEffects] instead of [System.Windows.DragDropEffects] in all the places you are using it..

Upvotes: 3

Related Questions