VB.NET 2008 - 匿名函数

发布于 2024-09-02 09:39:02 字数 1230 浏览 3 评论 0原文

在表单加载时,我用所有可能的颜色填充菜单,以便用户可以选择颜色。然而,当他们选择颜色时,我的标签的前色不会改变。

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' When the form loads, we want to populate the color menu item with all the possible colors that we could change the label to.
    For Each currentColor As KnownColor In [Enum].GetValues(GetType(KnownColor))
        ' Declare the knowColor again - we must do this to be able to do anonymous delegates in VB.NET
        Dim actualCurrentColor As KnownColor = currentColor

        ' Get the name for this color
        Dim colorName As String = [Enum].GetName(GetType(KnownColor), actualCurrentColor)

        ' Create a new menu item for this color
        Dim newMenuItem As ToolStripMenuItem = New ToolStripMenuItem(colorName)

        ' Add a handler to this menu item so when it is clicked, we change the heading color
        AddHandler newMenuItem.Click, Function(s As System.Object, events As System.EventArgs) (HeadingLabel.ForeColor = Color.FromKnownColor(actualCurrentColor))

        ' Add the menu item to the colors menu
        ColorToolStripMenuItem.DropDownItems.Add(newMenuItem)
    Next
End Sub

我做错了什么?谢谢

On Form Load I populate a menu with all possible colors so they user can pick a color. However when they pick a color the forecolor of my label is not changed.

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' When the form loads, we want to populate the color menu item with all the possible colors that we could change the label to.
    For Each currentColor As KnownColor In [Enum].GetValues(GetType(KnownColor))
        ' Declare the knowColor again - we must do this to be able to do anonymous delegates in VB.NET
        Dim actualCurrentColor As KnownColor = currentColor

        ' Get the name for this color
        Dim colorName As String = [Enum].GetName(GetType(KnownColor), actualCurrentColor)

        ' Create a new menu item for this color
        Dim newMenuItem As ToolStripMenuItem = New ToolStripMenuItem(colorName)

        ' Add a handler to this menu item so when it is clicked, we change the heading color
        AddHandler newMenuItem.Click, Function(s As System.Object, events As System.EventArgs) (HeadingLabel.ForeColor = Color.FromKnownColor(actualCurrentColor))

        ' Add the menu item to the colors menu
        ColorToolStripMenuItem.DropDownItems.Add(newMenuItem)
    Next
End Sub

What am I doing wrong? Thanks

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

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

发布评论

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

评论(1

み青杉依旧 2024-09-09 09:39:02

试试这个(使用正确的处理程序):

Public Class MainForm

        Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For Each currentColor As KnownColor In [Enum].GetValues(GetType(KnownColor))
                Dim colorName As String = [Enum].GetName(GetType(KnownColor), currentColor)
                Dim newMenuItem As ToolStripMenuItem = New ToolStripMenuItem(colorName)
                ColorToolStripMenuItem.DropDownItems.Add(newMenuItem)
            Next
        End Sub

        Private Sub ColorToolStripMenuItem_DropDownItemClicked(ByVal sender As System.Object, ByVal e As ToolStripItemClickedEventArgs) Handles ColorToolStripMenuItem.DropDownItemClicked
            HeadingLabel.ForeColor = Color.FromName(e.ClickedItem.Text)
        End Sub
    End Class

不过,您应该考虑使用专用的颜色选择器控件,而不是使用数百种匿名方法的具有数百种颜色的下拉菜单。

Try this (using the correct handler):

Public Class MainForm

        Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For Each currentColor As KnownColor In [Enum].GetValues(GetType(KnownColor))
                Dim colorName As String = [Enum].GetName(GetType(KnownColor), currentColor)
                Dim newMenuItem As ToolStripMenuItem = New ToolStripMenuItem(colorName)
                ColorToolStripMenuItem.DropDownItems.Add(newMenuItem)
            Next
        End Sub

        Private Sub ColorToolStripMenuItem_DropDownItemClicked(ByVal sender As System.Object, ByVal e As ToolStripItemClickedEventArgs) Handles ColorToolStripMenuItem.DropDownItemClicked
            HeadingLabel.ForeColor = Color.FromName(e.ClickedItem.Text)
        End Sub
    End Class

Still, you should think about using a dedicated color chooser control, rather than a drop down menu with hundreds of colors, using hundreds of anonymous methods.

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