Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DmitriyVlasov/5ad493eef672136eebff03c418cdadb7 to your computer and use it in GitHub Desktop.
Save DmitriyVlasov/5ad493eef672136eebff03c418cdadb7 to your computer and use it in GitHub Desktop.
Пример работы с классом PowerStatus из WinForms
// https://msdn.microsoft.com/ru-ru/library/system.windows.forms.powerstatus(v=vs.110).aspx
// http://www.codeproject.com/Articles/30414/Getting-Started-in-F-A-Windows-Forms-Application
open System
open System.Drawing
open System.Windows.Forms
type MainForm() as form =
inherit Form()
// Define private variables
// Define the controls for this Form
let listBox = new ListBox()
let textBox = new TextBox()
// Private functions
// The constructor simply initializes the form
do
form.SuspendLayout()
form.InitializeForm()
let t = typedefof<PowerStatus>
let pi = t.GetProperties()
for p in pi do
listBox.Items.Add(p.Name) |> ignore
textBox.Text <- sprintf "The PowerStatus class has %d properties.\r\n" (pi.Length)
listBox.SelectedIndexChanged.AddHandler(new EventHandler
(fun s e -> form.listBox_SelectedIndexChanged(s,e)))
form.ResumeLayout(false)
// member definitions
member form.listBox_SelectedIndexChanged(sender, e:EventArgs) =
if (listBox.SelectedIndex = -1 ) then ()
else
let t = typedefof<PowerStatus>
let propName = listBox.Text
let prop =
t.GetProperties()
|> Seq.tryFind (fun p ->
p.Name = propName
)
let propVal = string <| (Option.get prop).GetValue(SystemInformation.PowerStatus, null)
textBox.Text <- sprintf "\r\nThe value of the %s property is: %s" propName propVal
member form.InitializeForm() =
// Set form attribures
form.FormBorderStyle <- FormBorderStyle.Sizable
listBox.Anchor <- AnchorStyles.Top ||| AnchorStyles.Left ||| AnchorStyles.Right
listBox.Location <- new Point(8,16)
listBox.Size <- new Size(172, 496)
listBox.TabIndex <- 0
textBox.Anchor <- AnchorStyles.Top ||| AnchorStyles.Right
textBox.Location <- new Point(188, 16)
textBox.Multiline <- true
textBox.ScrollBars <- ScrollBars.Vertical
textBox.Size <- new Size(420,496)
textBox.TabIndex <- 1
form.ClientSize <- new Size(616,525)
form.Controls.Add textBox
form.Controls.Add listBox
form.Text <- "Select a Power Status property to get the value of"
[<STAThread>]
do
let form = new MainForm()
form.ShowDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment