F# 按钮点击事件

发布于 2025-01-08 06:36:49 字数 388 浏览 0 评论 0原文

我是 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);
}

需要知道如何在 F# 中执行此操作。

I am a beginner in F#, but have programmed a little in C # earlier.
I am trying to figure out how to write a ButtonClicEvent that will AppendText from a button (Or somewhere else) to a existing text in a textbox..

This is from 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);
}

Need to know how to do that in F#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错々过的事 2025-01-15 06:36:49
btn.Click.Add(fun _ -> textBox.AppendText(btn.Text))
btn.Click.Add(fun _ -> textBox.AppendText(btn.Text))
美人骨 2025-01-15 06:36:49

有一个很好的网站 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)

There's a good site F# Snippets

A relevant example from that site:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文