Visual F # Closing windows form after showing

Good day,

I've just started learning visual F # and it looks amazingly fun. For my first project, I got my hands dirty by immediately creating a window form to load information from a page and display it in a RichTextBox on a form. The problem is that as soon as the form is displayed and the information is loaded, it is immediately closed. How do I open my masterpiece for viewing? Any advice?

I have 2 files currently:

  • Program.fs
  • Script1.fs

Program.fs should "create" a form, where Script1.fs is just the entry point 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 repeat, I started with F #. This is my first application that I have written. How do I keep the form open?

+3


source to share


1 answer


You need to call Application.Run

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

This will create a message loop and save your application until the form is closed.



open Program1

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

      

+5


source







All Articles