【问题标题】:F# Button click eventF# 按钮点击事件
【发布时间】:2012-02-22 16:41:42
【问题描述】:

我是 F# 的初学者,但之前用 C# 编写过一些程序。 我试图弄清楚如何编写一个 ButtonClicEvent,它将从按钮(或其他地方)追加文本到文本框中的现有文本..

这是来自 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);
}

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

【问题讨论】:

标签: button f# click


【解决方案1】:
btn.Click.Add(fun _ -> textBox.AppendText(btn.Text))

【讨论】:

  • 谢谢,它成功了。问起来太容易了 :-) .. 早些时候我收到关于 AppendText 未定义的错误消息
  • @ISo,如果对您有帮助,请不要忘记将答案标记为正确。
【解决方案2】:

有个不错的网站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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-28
    • 2011-12-30
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多