Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very simple F# form locks up on keyboard input

Tags:

.net

f#

Using the ultra-simple code below, as soon as I press a key on my keyboard while the form is in focus, the form completely locks up. I'm running this inside of F# interactive. The only way to close the form is by clicking "Reset Session" in F# interactive. I've tried adding event handlers to KeyPress, with the same results. I've had no problem adding mouse event handlers, menus, combo boxes etc.

I must be doing something wrong, as something as obvious as pressing a key on a keyboard probably shouldn't be a bug at this point for F#. Any ideas?

// Add reference to System.Windows.Forms to project
open System.Windows.Forms

let a = new Form()
a.Visible <- true

I'm using F# 2.0 for Windows + Visual Studio 2008 (April 2010 release) on Windows XP.

Thanks!

like image 620
Dave Avatar asked Jul 07 '10 18:07

Dave


2 Answers

I think you need a call to

Application.Run(a)

but I don't have time to try and verify right now.

EDIT:

A useful thing to do is: create a C# Windows Form project, and see what code it starts you off with. It gives you this:

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

so of course you can do the same in F#:

open System.Windows.Forms 

let Main() =
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(false)
    Application.Run(new Form())

[<System.STAThread>]
do
    Main()
like image 132
Brian Avatar answered Sep 20 '22 19:09

Brian


You definitely don't need to call Application.Run(a) in F# Interactive, because it manages it's own message loop (in fact, you cannot do that).

Creating a form and setting Visible to true should work (and it does work on my machine!) Unfortunatelly, I'm not sure what could cause the problem that you reported, but it is definitely not the expected behavior (it seems to be some bug).

Using ShowDialog is not a good idea, because when you call it, it blocks you from entering further commands in F# Interactive until the dialog closes. This is very unfortunate - typical usage of F# Interactive is to create and show a form and then modify it by entering other commands. For example:

> let a = new Form();;
val a : Form = System.Windows.Forms.Form, Text: 

> a.Visible <- true;;  // Displays the form 
val it : unit = ()

> a.Text <- "Hello";;  // Changes title of the form
val it : unit = ()

(Marked as community wiki, because I didn't really answer the question.)

like image 35
Tomas Petricek Avatar answered Sep 23 '22 19:09

Tomas Petricek