VB.Net Tutorial/Event/AddressOf

Материал из VB Эксперт
Перейти к: навигация, поиск

AddHandler and AddressOf

Option Strict On
imports System
imports System.Drawing
imports System.Windows.Forms
public class MouseEnterHoverLeave : inherits Form
  private btn as Button
  public sub New()
    Size = new Size(400,400)
    btn = new Button()
    btn.Parent = me
    btn.Location = new Point(50,50)
    btn.Size = new Size(150,25)
    AddHandler btn.MouseEnter, AddressOf btn_MouseEnter
    AddHandler btn.MouseHover, AddressOf btn_MouseHover
    AddHandler btn.MouseLeave, AddressOf btn_MouseLeave
  end sub
  public shared sub Main() 
    Application.Run(new MouseEnterHoverLeave())
  end sub
  private sub btn_MouseEnter(ByVal sender as object,ByVal e as EventArgs)
    btn.Text = "MouseEnter"
    Console.WriteLine("Button MouseEnter")
  end sub
  private sub btn_MouseHover(ByVal sender as object,ByVal e as EventArgs)
    btn.Text = "MouseHover"
    Console.WriteLine("Button MouseHover")
  end sub
  private sub btn_MouseLeave(ByVal sender as object,ByVal e as EventArgs)
    btn.Text = "MouseLeave"
    Console.WriteLine("Button MouseLeave")
  end sub
  protected overrides sub OnMouseEnter(ByVal e as EventArgs)
    myBase.OnMouseEnter(e)
    Console.WriteLine("Form MouseEnter")
  end sub
  protected overrides sub OnMouseHover(ByVal e as EventArgs)
    myBase.OnMouseHover(e)
    Console.WriteLine("Form MouseHover")
  end sub
  protected overrides sub OnMouseLeave(ByVal e as EventArgs)
    myBase.OnMouseLeave(e)
    Console.WriteLine("Form MouseLeave")
  end sub
end class

AddressOf operator

Module Module1
    Delegate Sub GreetingMethod()
    Dim Greeting As New GreetingMethod(AddressOf MorningEnglish)
    Sub MorningEnglish()
        Console.WriteLine("Good morning")
    End Sub
    Sub AfternoonEnglish()
        Console.WriteLine("Good afternoon")
    End Sub
    Sub EveningEnglish()
        Console.WriteLine("Good evening")
    End Sub
    Sub MorningSpanish()
        Console.WriteLine("Buenas dias")
    End Sub
    Sub AfternoonSpanish()
        Console.WriteLine("Buenos tardes")
    End Sub
    Sub EveningSpanish()
        Console.WriteLine("Buenos noches")
    End Sub
    Sub Main()
        Greeting = AddressOf MorningEnglish
        Greeting = AddressOf AfternoonEnglish
        Greeting = AddressOf EveningEnglish
        Greeting = AddressOf MorningSpanish
        Greeting = AddressOf AfternoonSpanish
        Greeting = AddressOf EveningSpanish
        Greeting.Invoke()
    End Sub
End Module
Buenos noches

Use EventHandler with AddressOf

Module Module1
  Sub AHandler(ByVal o As Object, ByVal e As System.EventArgs)
    Console.WriteLine("handler")
  End Sub

  Sub Main()
    Dim TodayDate As DateTime = Today()
    Console.WriteLine(TodayDate)
    Dim MyArray() As Double = {3.159, System.Math.Sqrt(2)}
    Console.WriteLine(MyArray(1))
    Dim MyHandler As EventHandler " = AddressOf AHandler
    MyHandler(Nothing, Nothing)
    Try
      Dim MyException As Exception = New Exception("Raise an exception!")
      Throw MyException
    Catch e As Exception
      Console.WriteLine(e.StackTrace())
    End Try
  End Sub
End Module
11/05/2007 12:00:00 AM
1.4142135623731
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an ob
ject.
   at Module1.Main()