VB.NET 2008 - Anonymous Function -


On a form load, I populate a menu with all possible colors so that users can choose a color. However, when they select a color, the ForeColor of my label has not changed.

  private sub MainForm_Load (ByVal in as the System.Object, ByVal e System.EventArgs) Handles MyBase.Load 'when the form loads, but before color menu with all possible colors We want to populate, so that we can change the label. The name for this - "We [Enum] .GetValues ​​in VB.NET Go 'dim as to representatives anonymous actualCurrentColor should do to be able KnownColor = currentColor again knowColor declared each currentColor as KnownColor (GetType (KnownColor) for the color dim as the colorName string = [Enum] .GetName (GetType (KnownColor), actualCurrentColor) ToolStripMenuItem = New ToolStripMenuItem (colorName) as this create a new menu item for the color dim newMenuItem ) 'Handler added in this menu item so when it is clicked Land, but as the title color AddHandler newMenuItem.Click, function (s SystemkObject, events changed as System.EventArgs) (HeadingLabel.ForeColor = Color.FromKnownColor (actualCurrentColor)) 'menu item color menu ColorToolStripMenuItem.DropDownItems Adds (New menu item) Next end sub  

What am I doing wrong? Thank you

Try it (using right handler):

  public class MainForm Private sub MainForm_Load (ByVal as the System.Object, ByVal e System.EventArgs) in MyBase.Load handles KnownColor as each currentColor [Enum] .GetValues ​​(GetType (KnownColor) ) As a dim colorName string = [Enum]. GetName (GetType (KnownColor, CurrentColor) Dim NewMenuItem ToolStripMenuItem = New ToolStripMenuItem (colorName) ColorToolStripMenuItem.DropDownItems.Add (newMenuItem) Next End Sub Personal Sub ColorToolStripMenuItem_DropDownItemClicked (ByVal Handles this System.Object, as byVal e ToolStripItemClickedEventArgs) as ColorToolStripMenuItem.DropDownItemClicked HeadingLabel.ForeColor = Color.FromName (e.ClickedItem.Text) end Sub end class  

However, you should think about using touch a dedicated color Using hundreds of unknown methods, instead of a drop down menu with hundreds of colors Security control.


Comments