Ist Geschmackssache, hab dir mal jeweils ein Beispiel gemacht:
Addhandler:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ContextMenuHide As New ContextMenuStrip
Dim Item1 As New ToolStripMenuItem
Item1.Text = "Beenden"
ContextMenuHide.Items.Add(Item1)
AddHandler Item1.Click, AddressOf Item1_Click
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.NotifyIcon1.Visible = True
NotifyIcon1.Icon = Me.Icon
Me.NotifyIcon1.ContextMenuStrip = ContextMenuHide
End Sub
Private Sub Item1_Click()
Me.Close()
End Sub
Withevents
Code:
WithEvents Item1 As New ToolStripMenuItem
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ContextMenuHide As New ContextMenuStrip
Item1.Text = "Beenden"
ContextMenuHide.Items.Add(Item1)
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.NotifyIcon1.Visible = True
NotifyIcon1.Icon = Me.Icon
Me.NotifyIcon1.ContextMenuStrip = ContextMenuHide
End Sub
Private Sub Item1_Click() Handles Item1.Click
Me.Close()
End Sub
Das Withevents hat den Vorteil, das direkt alle Events vorhanden sind. Bei Addhandler musst du das ganze für jedes Event machen, dass du brauchst.