HeaderPane
Main
Hide
Share Link
Print

EventViewer Sample (VB.NET)

This sample logs the events of two fields and a calendar. The appointment field refuses Saturdays and Sundays in its ValueValidating event, and the due date field accepts "+7" or "-3" as a number of days in its TextParsing event.

The source code is located at C:\Program Files (x86)\Softelvdm\SftDate NET 3.0\Samples\VB\EventViewer\Form1.vb.

Imports System.ComponentModel
Imports System.Reflection
Imports System.Text.RegularExpressions
Imports Softelvdm.SftDateNET

Public Class Form1

    Private strLastEvent As String = ""
    Private fLastEventDup As Boolean = False

    ' This is a small helper routine to show all properties and fields of an object
    Private Sub DumpValues(ByVal title As String, ByVal o As Object)

        Dim s As String = ""

        If strLastEvent = title Then
            If Not fLastEventDup Then
                s = "Additional " + title + " events ... now suppressed"
                fLastEventDup = True
            Else
                Return
            End If
        Else
            strLastEvent = title
            fLastEventDup = False

            Dim api() As PropertyInfo = o.GetType().GetProperties()
            For Each pi As PropertyInfo In api
                s += pi.Name + "=" + Describe(pi.GetValue(o, New Object() {})) + " "
            Next
            Dim afi() As FieldInfo = o.GetType().GetFields()
            For Each fi As FieldInfo In afi
                s += fi.Name + "=" + Describe(fi.GetValue(o)) + " "
            Next
        End If

        Dim item As ListViewItem = ListView1.Items.Add(New ListViewItem(New String() {title, s}))
        ListView1.SelectedItems.Clear()
        item.Selected = True
        item.EnsureVisible()
    End Sub

    Private Function Describe(ByVal value As Object) As String
        If value Is Nothing Then
            Return "Nothing"
        End If
        Return value.ToString()
    End Function

    Private Sub ButtonClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonClear.Click
        ListView1.Items.Clear()
        strLastEvent = ""
        fLastEventDup = False
    End Sub

    ' SftDate1 - a date and time field; appointments are only accepted on weekdays

    Private Sub SftDate1_ValueValidating(ByVal sender As Object, ByVal e As ValueValidatingEventArgs) Handles SftDate1.ValueValidating
        If e.NewValue.HasValue AndAlso (e.NewValue.Value.DayOfWeek = DayOfWeek.Saturday OrElse e.NewValue.Value.DayOfWeek = DayOfWeek.Sunday) Then
            e.Cancel = True
            DumpValues("SftDate1.ValueValidating (canceled, a weekend)", e)
        Else
            DumpValues("SftDate1.ValueValidating", e)
        End If
    End Sub

    Private Sub SftDate1_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles SftDate1.ValueChanged
        DumpValues("SftDate1.ValueChanged (" + SftDate1.Text + ")", e)
    End Sub

    Private Sub SftDate1_DropDownOpening(ByVal sender As Object, ByVal e As CancelEventArgs) Handles SftDate1.DropDownOpening
        DumpValues("SftDate1.DropDownOpening", e)

        ' e.Cancel = True would keep the dropdown closed
    End Sub

    Private Sub SftDate1_DropDownOpened(ByVal sender As Object, ByVal e As EventArgs) Handles SftDate1.DropDownOpened
        DumpValues("SftDate1.DropDownOpened", e)
    End Sub

    Private Sub SftDate1_DropDownClosed(ByVal sender As Object, ByVal e As EventArgs) Handles SftDate1.DropDownClosed
        DumpValues("SftDate1.DropDownClosed", e)
    End Sub

    Private Sub SftDate1_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles SftDate1.Enter
        DumpValues("SftDate1.Enter", e)
    End Sub

    Private Sub SftDate1_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles SftDate1.Leave
        DumpValues("SftDate1.Leave", e)
    End Sub

    Private Sub SftDate1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles SftDate1.KeyDown
        DumpValues("SftDate1.KeyDown", e)
    End Sub

    ' SftDate2 - a free-form field; besides dates in the culture's format it accepts +n and -n days from today

    Private Sub SftDate2_TextParsing(ByVal sender As Object, ByVal e As TextParsingEventArgs) Handles SftDate2.TextParsing
        Dim m As Match = Regex.Match(e.Text.Trim(), "^([+-])\s*(\d{1,4})$")
        If m.Success Then
            Dim days As Integer = Integer.Parse(m.Groups(2).Value)
            e.Value = DateTime.Today.AddDays(If(m.Groups(1).Value = "-", -days, days))
            e.Handled = True
        End If
        DumpValues("SftDate2.TextParsing", e)
    End Sub

    Private Sub SftDate2_ValueValidating(ByVal sender As Object, ByVal e As ValueValidatingEventArgs) Handles SftDate2.ValueValidating
        DumpValues("SftDate2.ValueValidating", e)
    End Sub

    Private Sub SftDate2_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles SftDate2.ValueChanged
        DumpValues("SftDate2.ValueChanged (" + SftDate2.Text + ")", e)
    End Sub

    Private Sub SftDate2_DropDownOpened(ByVal sender As Object, ByVal e As EventArgs) Handles SftDate2.DropDownOpened
        DumpValues("SftDate2.DropDownOpened", e)
    End Sub

    Private Sub SftDate2_DropDownClosed(ByVal sender As Object, ByVal e As EventArgs) Handles SftDate2.DropDownClosed
        DumpValues("SftDate2.DropDownClosed", e)
    End Sub

    Private Sub SftDate2_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles SftDate2.Enter
        DumpValues("SftDate2.Enter", e)
    End Sub

    Private Sub SftDate2_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles SftDate2.Leave
        DumpValues("SftDate2.Leave", e)
    End Sub

    ' SftCalendar1

    Private Sub SftCalendar1_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles SftCalendar1.ValueChanged
        DumpValues("SftCalendar1.ValueChanged (" + Describe(SftCalendar1.Value) + ")", e)
    End Sub

    Private Sub SftCalendar1_RangeChanged(ByVal sender As Object, ByVal e As EventArgs) Handles SftCalendar1.RangeChanged
        DumpValues("SftCalendar1.RangeChanged", e)
    End Sub

    Private Sub SftCalendar1_DateActivated(ByVal sender As Object, ByVal e As DateEventArgs) Handles SftCalendar1.DateActivated
        DumpValues("SftCalendar1.DateActivated", e)
    End Sub

    Private Sub SftCalendar1_ViewChanged(ByVal sender As Object, ByVal e As EventArgs) Handles SftCalendar1.ViewChanged
        DumpValues("SftCalendar1.ViewChanged (" + SftCalendar1.View.ToString() + ")", e)
    End Sub

    Private Sub SftCalendar1_DisplayChanged(ByVal sender As Object, ByVal e As EventArgs) Handles SftCalendar1.DisplayChanged
        DumpValues("SftCalendar1.DisplayChanged (" + SftCalendar1.DisplayStart.ToShortDateString() + ")", e)
    End Sub

    Private Sub SftCalendar1_TodayClicked(ByVal sender As Object, ByVal e As EventArgs) Handles SftCalendar1.TodayClicked
        DumpValues("SftCalendar1.TodayClicked", e)
    End Sub

    Private Sub SftCalendar1_ClearClicked(ByVal sender As Object, ByVal e As EventArgs) Handles SftCalendar1.ClearClicked
        DumpValues("SftCalendar1.ClearClicked", e)
    End Sub

    Private Sub SftCalendar1_DayFormatting(ByVal sender As Object, ByVal e As DayFormattingEventArgs) Handles SftCalendar1.DayFormatting
        ' DumpValues("SftCalendar1.DayFormatting", e)  ' deliberately suppressed, it occurs for every day shown
    End Sub

    Private Sub SftCalendar1_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles SftCalendar1.Enter
        DumpValues("SftCalendar1.Enter", e)
    End Sub

    Private Sub SftCalendar1_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles SftCalendar1.Leave
        DumpValues("SftCalendar1.Leave", e)
    End Sub

    Private Sub SftCalendar1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles SftCalendar1.KeyDown
        DumpValues("SftCalendar1.KeyDown", e)
    End Sub

    Private Sub SftCalendar1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles SftCalendar1.MouseDown
        DumpValues("SftCalendar1.MouseDown", e)
    End Sub

End Class