I'm currently working my way through "Real World Functional Programming". I am trying to get example 1.12 working, a "hello world" program using windows forms. This is the code:-
open System.Drawing;;
open System.Windows.Forms;;
type HelloWindow() =
let frm = new Form(Width = 400, Height = 140)
let fnt = new Font("Times New Roman", 28.0f)
let lbl = new Label(Dock = DockStyle.Fill, Font = fnt,
TextAlign = ContentAlignment.MiddleCenter)
do frm.Controls.Add(lbl)
member x.SayHello(name) =
let msg = "Hello" + name + "!"
lbl.Text <- msg
member x.Run() =
Application.Run(frm);;
let hello = new HelloWindow();;
hello.SayHello("you");;
hello.Run();;
Unfortunately, this throws an error - "Starting a second message loop on a single thread is not a valid operation." So obviously there is a window opening and not terminating and that is confusing the program. I can't see how to fix the error, can anyone help me out?
I have also tried inputting the final code block as:-
let hello = new HelloWindow()
hello.SayHello("you")
hello.Run();;
But that does not help. The code runs fine but produces no result with the last line commented out.
Windows Forms (WinForms) is a free and open-source graphical (GUI) class library included as a part of Microsoft . NET, . NET Framework or Mono Framework, providing a platform to write client applications for desktop, laptop, and tablet PCs.
Windows Forms is a Graphical User Interface(GUI) class library which is bundled in . Net Framework. Its main purpose is to provide an easier interface to develop the applications for desktop, tablet, PCs. It is also termed as the WinForms.
The example was meant to compile and run as a Windows Form application. If you would like to run it in F# Interactive, you have to use frm.Show()
instead of Application.Run(frm)
.
You could make the example work both in F# Interactive and in compiled projects using compiler directives:
type HelloWindow() =
let frm = new Form(Width = 400, Height = 140)
// ...
// The same as before
member x.Run() =
#if INTERACTIVE
frm.Show()
#else
Application.Run(frm)
#endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With