Program.fs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. open Terminal.Gui
  2. type ExampleWindow() as this =
  3. inherit Window()
  4. do
  5. this.Title <- sprintf "Example App (%O to quit)" Application.QuitKey
  6. // Create input components and labels
  7. let usernameLabel = new Label(Text = "Username:")
  8. let userNameText = new TextField(X = Pos.Right(usernameLabel) + Pos.op_Implicit(1), Width = Dim.Fill())
  9. let passwordLabel = new Label(Text = "Password:", X = Pos.Left(usernameLabel), Y = Pos.Bottom(usernameLabel) + Pos.op_Implicit(1))
  10. let passwordText = new TextField(Secret = true, X = Pos.Left(userNameText), Y = Pos.Top(passwordLabel), Width = Dim.Fill())
  11. // Create login button
  12. let btnLogin = new Button(Text = "Login", Y = Pos.Bottom(passwordLabel) + Pos.op_Implicit(1), X = Pos.Center(), IsDefault = true)
  13. // When login button is clicked display a message popup
  14. btnLogin.Accepting.Add(fun _ ->
  15. if userNameText.Text = "admin" && passwordText.Text = "password" then
  16. MessageBox.Query("Logging In", "Login Successful", "Ok") |> ignore
  17. ExampleWindow.UserName <- userNameText.Text.ToString()
  18. Application.RequestStop()
  19. else
  20. MessageBox.ErrorQuery("Logging In", "Incorrect username or password", "Ok") |> ignore
  21. )
  22. // Add the views to the Window
  23. this.Add(usernameLabel, userNameText, passwordLabel, passwordText, btnLogin)
  24. static member val UserName = "" with get, set
  25. [<EntryPoint>]
  26. let main argv =
  27. Application.Init()
  28. Application.Run<ExampleWindow>().Dispose()
  29. // Before the application exits, reset Terminal.Gui for clean shutdown
  30. Application.Shutdown()
  31. // To see this output on the screen it must be done after shutdown,
  32. // which restores the previous screen.
  33. printfn "Username: %s" ExampleWindow.UserName
  34. 0 // return an integer exit code