F# winforms MenuStrip 问题:不确定如何获取 DropDownItems 的句柄

发布于 2024-07-10 02:14:18 字数 965 浏览 4 评论 0原文

我最近开始学习F#,这是我第一次使用WinForms。 这是我的代码。

#light
open System
open System.Windows.Forms
let form =
    let temp = new Form()
    let ms = new MenuStrip()
    let file = new ToolStripDropDownButton("File")
    ignore(ms.Items.Add(file))
    ignore(file.DropDownItems.Add("TestItem")) \\Code of importance
    let things _ _ = ignore(MessageBox.Show("Hai"))
    let handle = new EventHandler(things)
    ignore(file.Click.AddHandler(handle))
    let stuff _ _ = ignore(MessageBox.Show("Hai thar."))
    let handler = new EventHandler(stuff)
    let myButton = new Button(Text = "My button :>", Left = 8, Top = 100, Width = 80)
    myButton.Click.AddHandler(handler)
    let dc c = (c :> Control)
    temp.Controls.AddRange([| dc myButton; dc ms |]);
    temp
do Application.Run(form)

问题是什么,我似乎无法弄清楚如何获取 DropDownItems 项目的句柄以便我可以使用它。 我确信这很简单,但我使用 F# 的时间并不长。 谢谢你的帮助。

编辑:我还想指出,我知道该代码块中有很多丑陋的语法,但整个事情只是我一直在使用的测试形式。

I have recently started learning F#, and this is the first time I've ever used WinForms. Here is my code.

#light
open System
open System.Windows.Forms
let form =
    let temp = new Form()
    let ms = new MenuStrip()
    let file = new ToolStripDropDownButton("File")
    ignore(ms.Items.Add(file))
    ignore(file.DropDownItems.Add("TestItem")) \\Code of importance
    let things _ _ = ignore(MessageBox.Show("Hai"))
    let handle = new EventHandler(things)
    ignore(file.Click.AddHandler(handle))
    let stuff _ _ = ignore(MessageBox.Show("Hai thar."))
    let handler = new EventHandler(stuff)
    let myButton = new Button(Text = "My button :>", Left = 8, Top = 100, Width = 80)
    myButton.Click.AddHandler(handler)
    let dc c = (c :> Control)
    temp.Controls.AddRange([| dc myButton; dc ms |]);
    temp
do Application.Run(form)

What the problem is, I can't seem to figure out how I would get a handle on the DropDownItems item so that I could use it. I'm sure it's something simple, but I haven't been using F# for that long. Thanks for any help.

edit: I'd also like to point out that I know there are alot of ugly syntax in that block of code, but the whole thing is just a test form I've been using.

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

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

发布评论

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

评论(1

伊面 2024-07-17 02:14:19

我认为你只需要

let ddi = file.DropDownItems.Add("TestItem") //Code of importance

问题是你忽略了 Add() 调用的结果,它返回添加的项目。

说”更惯用。

yadda |> ignore

另请注意,“说”比“

ignore(yadda)

I think you just need to

let ddi = file.DropDownItems.Add("TestItem") //Code of importance

The problem is that you are ignoring the result of the Add() call, which returns the added item.

Note also that it's more idiomatic to say

yadda |> ignore

rather than

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