Winforms拖放不再工作

72qzrwbm  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(130)

我创建了一个新的项目,并把一个新的形式,允许拖放的文件。在设计器中,设置:Me.AllowDrop = True但它似乎是不工作。下面是表单的代码:

Imports System.IO
Imports System.Windows.Forms

Public Class frmDragNDropTesting
    Inherits System.Windows.Forms.Form

    Public Sub New()
        ' Set up the form
        InitializeComponent()

        ' Register event handlers
        'AddHandler Me.DragEnter, AddressOf MainForm_DragEnter
        'AddHandler Me.DragDrop, AddressOf MainForm_DragDrop
    End Sub

    ' Event handler for when a file is dragged over the form
    Private Sub MainForm_DragEnter(sender As Object, e As DragEventArgs) Handles MyBase.DragEnter
        e.Effect = DragDropEffects.Link

        If e.Data.GetDataPresent(DataFormats.Text) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub

    ' Event handler for when a file is dropped onto the form
    Private Sub MainForm_DragDrop(sender As Object, e As DragEventArgs) Handles MyBase.DragDrop
        MessageBox.Show("Something was dropped!")
        Dim files As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())

        If files.Length > 0 Then
            Dim file As String = files(0)
            Dim extension As String = Path.GetExtension(file).ToLower()

            ' Process the dropped file
            ' Add your code here to handle the dropped file as needed
            MessageBox.Show("Dropped file path: " & file)
        End If
    End Sub

End Class

我在Windows 11中使用VS 2022。当我运行应用程序并尝试将文件拖动到窗体上时,会出现一个红色的“禁止输入”图标,并且不会触发DragEnter和DragDrop事件。
任何想法为什么它不工作,请?

zbwhf8kr

zbwhf8kr1#

感谢Hans Passant解决了这个问题。VS在提升模式下运行,看起来拖放在提升模式下将不起作用。

相关问题