Winforms Menus

Creating menus in Winforms is easy: Create a new MainMenu and assign it to your form as a menu. Subsequently you can add submenus, assign keybord shortcuts and event handlers.

open System
open System.Windows.Forms

let form = new Form()
form.Text <- "Form"

let mmain   = new MainMenu()
form.Menu <- mmain

let mfile   = new MenuItem("&File")
let mabout  = new MenuItem("&About")
let msep1   = new MenuItem("-")
let mquit   = new MenuItem("&Quit")

mmain.MenuItems.Add(mfile) |> ignore
mfile.MenuItems.Add(mabout) |> ignore
mfile.MenuItems.Add(msep1) |> ignore
mfile.MenuItems.Add(mquit) |> ignore

mabout.Shortcut <- Shortcut.CtrlShiftA
mquit.Shortcut <- Shortcut.CtrlQ

mabout.Click.Add(fun _ -> MessageBox.Show("F# Winforms Menu Demo",
                                          "About") |> ignore)
mquit.Click.Add(fun _ -> form.Close())

form.Show()
#if COMPILED
[<STAThread()>]
Application.Run(form)
#endif

Advertisements
This entry was posted in GUI. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s