2010-12-10 45 views
1

您知道为什么F#中的DragDrop事件在我的示例中无法正常工作吗?像DragEnter,DragLeave,DragOver等所有其他事件都以相同的方式正常工作。F#使用WinForms拖放:控件的DragDrop事件不会调用引用的成员函数

只需编译此代码并试用一下,就可以将一个文件拖到窗体中,并查看从启动可执行文件的控制台/终端中触发的事件。

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) = 
     printfn "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) 

回答

3

从onDragEnter中删除评论。除非您将e.Effect设置为e.AllowedEffects之一,否则将不允许放置。这也改变了光标。

+0

非常感谢你,完美的工作! – 2010-12-10 23:55:35