开发者

F# Drag and Drop using WinForms: DragDrop event of a control does not call the referenced member function

do you know why the DragDrop event in F# is not working properly in my example? All other events like DragEnter, DragLeave, DragOver,... are working fine in the same way.

Just compile this code and try it out, drag a file into the form and see the events fired in the console/terminal from where you startet the executable.

open System
open System.Drawing
open System.Windows.Forms

type MainForm( args: string list ) as this =
    // subclassing
    inherit Form()

    // controls -------------------
    let dragDropImage = new PictureBox()
    // ----------------------------

    // "constructor" (not a real constructor)
    do this.initComponents()
    // link events to specific member function
    do dragDropImage.DragEnter |> Event.add this.onDragEnter
    do dragDropImage.DragDrop |> Event.add this.onDragDrop
    // this syntax don't work either: do dragDropImage.DragDrop.Add(fun _ -> printfn "dragDrop")
    do dragDropImage.DragLeave |> Event.add this.onDragLeave
    do dragDropImage.DragOver |> Event.add this.onDragOver

    member this.initComponents() =
        // main form attributes
        this.Text <- "Averest-GUI"
        this.ClientSize <- new Size(350,230)
        this.StartPosition <- FormStartPosition.CenterScreen
        // drag'n'drop field
        dragDropImage.Size <- new Size(330,210)
        dragDropImage.Location <- new Point(7,7)
        dragDropImage.AllowDrop <- true // allow Drag'n'Drop functionality
        // insert controls into MainForm
        this.Controls.Add(dragDropImage)

    member this.onDragLeave( e: EventArgs ) =
        printfn "DragLeave" //e.Effect <- DragDropEffects.Copy

    member this.onDragOver( e: DragEventArgs ) =
        printfn "DragOver" //e.Effect <- DragDropEffects.Copy

    member this.onDragEnter( e: DragEventArgs ) =
        pr开发者_如何学运维intfn "DragEnter" //e.Effect <- DragDropEffects.Copy

    member this.onDragDrop( e: DragEventArgs ) =
        printfn "DragDrop"


let testForm =
    let temp = new MainForm( ["Test"] )
    temp

// single thread apartment model (interacting with COM components)
[<STAThread>]
do Application.Run(testForm)


Remove the comment from onDragEnter. The drop won't be allowed unless you set e.Effect to one of the e.AllowedEffects. That also changes the cursor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜