F#按钮单击事件

ISo*_*ISo 2 f# click button

我是F#的初学者,但早些时候在C#中编程了一点.我试图弄清楚如何编写一个ButtonClicEvent,它将AppendText从按钮(或其他地方)添加到文本框中的现有文本.

这来自C#:

private void Btn_Click(object sender, EventArgs e)
{
    // if the eventhandler contains more than one button
    var btn = (sender as Button);

    textBox.AppendText(btn.Text);
}
Run Code Online (Sandbox Code Playgroud)

需要知道如何在F#中做到这一点.

Dan*_*iel 8

btn.Click.Add(fun _ -> textBox.AppendText(btn.Text))
Run Code Online (Sandbox Code Playgroud)


Dmi*_*try 5

有一个很好的网站F#Snippets

该网站的相关示例:

open System
open System.Drawing
open System.Windows.Forms

// Create form, button and add button to form
let form = new Form(Text = "Hello world!")
let btn = new Button(Text = "Click here")
form.Controls.Add(btn)

// Register event handler for button click event
btn.Click.Add(fun _ ->
  // Generate random color and set it as background
  let rnd = new Random()
  let r, g, b = rnd.Next(256), rnd.Next(256), rnd.Next(256)
  form.BackColor <- Color.FromArgb(r, g, b) )

// Show the form (in F# Interactive)
form.Show()
// Run the application (in compiled application)
Application.Run(form)
Run Code Online (Sandbox Code Playgroud)