Eon
Eon

Reputation: 3974

Visual F# Windows form closing after showing

Good Day,

I have just started learning visual F#, and it looks surprisingly fun to do. For my first project I got my hands dirty by immediately make a windows form to download info from a page and display it in a RichTextBox on the form. Problem is, once the form shows and information is downloaded, it immediately closes. How do I keep my masterpiece open for viewing? Any advice?

I have 2 Files currently:

Program.fs is supposed to "create" the form, where Script1.fs is merely the entrypoint for the application.

Program.fs

namespace Program1
    open System.Windows.Forms

    module public HelloWorld =
        let form = new Form(Visible = true, TopMost = true, Text = "Welcome to F#")

        let textB = new RichTextBox(Dock = DockStyle.Fill, Text = "Initial Text")
        form.Controls.Add textB

        open System.IO
        open System.Net

        /// Get the contents of the URL via a web request
        let http (url: string) =
         let req = System.Net.WebRequest.Create(url)
         let resp = req.GetResponse()
         let stream = resp.GetResponseStream()
         let reader = new StreamReader(stream)
         let html = reader.ReadToEnd()
         resp.Close()
         html
        textB.Text <- http "http://www.google.com"

Script1.fs

open Program1

    [<EntryPoint>]
    let main argv= 
        printfn "Running F# App"
        HelloWorld.form.Show();
        0

I need to reiterate, I started with F#. This is my first application I wrote. How do I keep the form open?

Upvotes: 3

Views: 864

Answers (1)

MattDavey
MattDavey

Reputation: 9017

You need to call Application.Run and pass your form object into it. http://msdn.microsoft.com/en-us/library/system.windows.forms.application.run(v=vs.110).aspx

This will create a message loop, and keep your application alive until the form is closed.

open Program1

[<EntryPoint>]
let main argv= 
    printfn "Running F# App"
    System.Windows.Forms.Application.Run(HelloWorld.form)
    0

Upvotes: 5

Related Questions