1

Form Load では、ユーザーが色を選択できるように、可能なすべての色をメニューに入力します。しかし、彼らが色を選ぶとき、私のラベルの前色は変わりません。

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

私は何を間違っていますか?ありがとう

4

1 に答える 1

0

これを試してください(正しいハンドラーを使用して):

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

それでも、何百もの匿名メソッドを使用して、何百もの色のドロップダウン メニューではなく、専用のカラー チューザー コントロールを使用することを検討する必要があります。

于 2010-05-17T01:32:54.000 に答える