Tipp 0101 Ereignisse zentral mit AddHandler auswerten
Autor/Einsender:
Datum:
  Klaus D. Raudszus
10.01.2006
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
Hier ein kleines Programm über den Einsatz des AddHandlers. Das Beispiel demonstriert, wie mittels Addhandler eine Codierung vereinfacht werden kann. Die beiden Methoden ersetzen immerhin 24 andere Prozeduren. Außerdem zeigen die beiden Prozeduren, warum überhaupt dieses GetType da ist.
 
Public Class frmMain
  Inherits System.Windows.Forms.Form

#Region " Vom Windows Form Designer generierter Code "
  Public Sub New()
    MyBase.New()
    InitializeComponent()

    AddHandler TextBox1.MouseLeave, AddressOf myLeave
    AddHandler TextBox2.MouseLeave, AddressOf myLeave
    AddHandler TextBox3.MouseLeave, AddressOf myLeave
    AddHandler TextBox4.MouseLeave, AddressOf myLeave
    AddHandler RadioButton1.MouseLeave, AddressOf myLeave
    AddHandler RadioButton2.MouseLeave, AddressOf myLeave
    AddHandler RadioButton3.MouseLeave, AddressOf myLeave
    AddHandler RadioButton4.MouseLeave, AddressOf myLeave
    AddHandler Label1.MouseLeave, AddressOf myLeave
    AddHandler Label2.MouseLeave, AddressOf myLeave
    AddHandler Label3.MouseLeave, AddressOf myLeave
    AddHandler Label4.MouseLeave, AddressOf myLeave
    AddHandler TextBox1.MouseEnter, AddressOf myEnter
    AddHandler TextBox2.MouseEnter, AddressOf myEnter
    AddHandler TextBox3.MouseEnter, AddressOf myEnter
    AddHandler TextBox4.MouseEnter, AddressOf myEnter
    AddHandler RadioButton1.MouseEnter, AddressOf myEnter
    AddHandler RadioButton2.MouseEnter, AddressOf myEnter
    AddHandler RadioButton3.MouseEnter, AddressOf myEnter
    AddHandler RadioButton4.MouseEnter, AddressOf myEnter
    AddHandler Label1.MouseEnter, AddressOf myEnter
    AddHandler Label2.MouseEnter, AddressOf myEnter
    AddHandler Label3.MouseEnter, AddressOf myEnter
    AddHandler Label4.MouseEnter, AddressOf myEnter
  End Sub
'...
#End Region

  Private Sub myLeave(ByVal sender As Object, ByVal e As EventArgs)
    Dim LeaveTextBox As TextBox = New TextBox()
    Dim LeaveRadioButton As RadioButton = New RadioButton()
    Dim LeaveLabel As Label = New Label()

    If sender.GetType Is LeaveTextbox.GetType Then
      LeaveTextBox = sender
      LeaveTextBox.ForeColor = Color.Black
      LeaveTextBox.Text = "Verlassen"
    ElseIf sender.GetType Is LeaveRadioButton.GetType Then
      LeaveRadioButton = sender
      LeaveRadioButton.ForeColor = Color.Black
      LeaveRadioButton.Text = "Verlassen"
    ElseIf sender.GetType Is LeaveLabel.GetType Then
      LeaveLabel = sender
      LeaveLabel.ForeColor = Color.Black
      LeaveLabel.Text = "Verlassen"
    End If
  End Sub

  Private Sub myEnter(ByVal sender As Object, ByVal e As EventArgs)
    Dim EnterTextBox As TextBox = New TextBox()
    Dim EnterRadioButton As RadioButton = New RadioButton()
    Dim EnterLabel As Label = New Label()

    If sender.GetType Is EnterTextbox.GetType Then
      EnterTextBox = sender
      EnterTextBox.ForeColor = Color.Red
      EnterTextBox.Text = "Eingetreten"
    ElseIf sender.GetType Is EnterRadioButton.GetType Then
      EnterRadioButton = sender
      EnterRadioButton.ForeColor = Color.Red
      EnterRadioButton.Text = "Eingetreten"
    ElseIf sender.GetType Is EnterLabel.GetType Then
      EnterLabel = sender
      EnterLabel.ForeColor = Color.Red
      EnterLabel.Text = "Eingetreten"
    End If
  End Sub
End Class
 

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (6,9 kB) Downloads bisher: [ 428 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Dienstag, 24. Januar 2012