PowerShell - DataGridView Windows 窗体拖放问题 [英] PowerShell - DataGridView Windows Form Drag and Drop Issue

查看:66
本文介绍了PowerShell - DataGridView Windows 窗体拖放问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好 StackOverflow 成员,

Hello StackOverflow members,

希望得到有关 Windows 窗体 DataGridView 控件问题的一些指导.我偶然创建了一个简单的基于 Windows 窗体的 PowerShell 脚本,它应该使用拖放事件.我面临的问题是,当我在 PowerShell ISE 界面中加载/运行 PowerShell 脚本时,拖动事件似乎不起作用.但是,如果我再次运行它(不做任何其他事情),拖拽事件似乎有效(我只是将一些文件从文件资源管理器拖到 DataGridView 表单控件上).

Hoping for some guidance with a Windows Form DataGridView Control issue. I've stumbled to create a simple Windows Form-based PowerShell Script that should make use of Drag and Drop Events. The issue that I am facing is that when I load/run the PowerShell Script within PowerShell ISE interface, the drag over event does not seem to function. However, if I run it again (without doing anything else), the drag over event seems to work (I am simply dragging a few Files from File Explorer over and to a DataGridView Form Control).

这是我的代码:

<#==============================================+
 |  BEGIN SECTION:  Form Control Declarations.  |
 +==============================================#>

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

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

# Create the "ProjectWise File Replacer" Form Control.
$ProjectWiseFileReplacer = New-Object System.Windows.Forms.Form
$ProjectWiseFileReplacer.ClientSize = New-Object System.Drawing.Point(604,460)
$ProjectWiseFileReplacer.Text = "ProjectWise File Replacer (Version 1.0.0) - By Patel, Greene & Associates, LLC"
$ProjectWiseFileReplacer.TopMost = $True
$ProjectWiseFileReplacer.MinimumSize = $ProjectWiseFileReplacer.ClientSize
$ProjectWiseFileReplacer.FormBorderStyle = 'FixedDialog'
$ProjectWiseFileReplacer.Icon = "C:\PGA\Information Technology\ProjectWise\Administration\PowerShell\Scripts\ProjectWise_Icon.ico"
$ProjectWiseFileReplacer.StartPosition = "CenterScreen"

# Create "Files" Data Grid View Form Control.
$DataGridView_Files = New-Object System.Windows.Forms.DataGridView
$DataGridView_Files.Width = 572
$DataGridView_Files.Height = 213
$DataGridView_Files.ColumnCount = 2
$DataGridView_Files.ColumnHeadersVisible = $True
$DataGridView_Files.Columns[0].Name = "Path and File Name"
$DataGridView_Files.Columns[1].Name = "Status"
$DataGridView_Files.Location = New-Object System.Drawing.Point(16,107)
$DataGridView_Files.SelectionMode = 'FullRowSelect'
$DataGridView_Files.MultiSelect = $False
$DataGridView_Files.TabIndex = 0
$DataGridView_Files.RowHeadersVisible = $False
$DataGridView_Files.AutoSizeColumnsMode = 'Fill'
$DataGridView_Files.AllowUserToAddRows = $False
$DataGridView_Files.AllowUserToDeleteRows = $True
$DataGridView_Files.AllowUserToResizeRows = $False
$DataGridView_Files.ReadOnly = $True
$DataGridView_Files.AllowDrop = $True
$DataGridView_Files.RowTemplate.Height = 17
$DataGridView_Files.ColumnHeadersHeight = 22
$DataGridView_Files.Enabled = $True
$DataGridView_Files.Add_DragDrop($DataGridView_Files_DragDrop)
$DataGridView_Files.Add_DragOver($DataGridView_Files_DragOver)

# Add Form Controls to the "ProjectWise File Replacer" Form.
$ProjectWiseFileReplacer.Controls.AddRange(@($DataGridView_Files))

<#================================================+
 |  BEGIN SECTION:  Declare Form Control Events.  |
 +================================================#>

# "Files" Data Grid View Form Control (Drag Over Event).
$DataGridView_Files_DragOver=[System.Windows.Forms.DragEventHandler]{
    # Files have been selected to drag over the "Files" Data Grid View Form Control.
    If ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop))
        {
            # Set Drag Over Event Handler Effect.
            $_.Effect = 'Copy'
        }
    # Files have not been selected to drap over the "Files" Data Grid View Form Control.
    Else
        {
            # Set Drag Over Event Handler Effect.
            $_.Effect = 'None'
        }
}

# "Files" Data Grid View Form Control (Drag Drop Event).
$DataGridView_Files_DragDrop=[System.Windows.Forms.DragEventHandler]{
    # Create a String Array for File Collection.
    $Files = $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)
    
    # Files have been selected, dragged and dropped.
    If ($Files)
        {
            # Loop through each File within the File Collection.
            ForEach ($File in $Files)
          {
                    # Add File to "Files" Data Grid View Control.
                    [void]$DataGridView_Files.Rows.Add($File,"")
                }
        }
}

<#========================================+
 |  BEGIN SECTION:  Main Body of Script.  |
 +========================================#>

# Display (Show) the "ProjectWise File Replacer" Form Window.
[void]$ProjectWiseFileReplacer.ShowDialog()

关于为什么这在我第一次运行时不起作用的任何想法?

Any ideas as to why this won't work the first time I run it?

P.S...从 PowerShell ISE 程序外部运行脚本时,我得到了相同的结果(未按预期工作).

P.S...I get the same result (of not working as expected) when running the script from outside of the PowerShell ISE program.

注意事项:

  • Windows 10 专业版(64 位)
  • 在 64 位模式下运行脚本.

推荐答案

$DataGridView_Files_DragOver$DataGridView_Files_DragDrop的定义移到上方的地方你在哪里打电话给他们.现在您在它们尚未定义时使用它们.

Move the definitions for $DataGridView_Files_DragOver and $DataGridView_Files_DragDrop above the place where you call on them. Now you are using them when they are not yet defined.

第二次运行代码时,它们是已知的并且功能正常.

The second time the code runs, they are known and the functionality works.

$DataGridView_Files.Enabled = $True

**Here would be a good spot**

$DataGridView_Files.Add_DragDrop($DataGridView_Files_DragDrop)
$DataGridView_Files.Add_DragOver($DataGridView_Files_DragOver)

这篇关于PowerShell - DataGridView Windows 窗体拖放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆