none
How do I create my own events? RRS feed

  • Question

  •  

    for example:

     

     

    Code Block

    Public Class Form1

     

        Private mIsChecked As Boolean
        Public Property
    IsChecked() As Boolean
            Get
                Return
    mIsChecked
            End Get
            Set
    (ByVal value As Boolean)
                mIsChecked = value
            End Set
        End Property

     

        Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, _

     ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
            If sender.Checked Then
                IsChecked = True
            Else
                IsChecked = False
            End If
            'Some other code and operations
        End Sub

     

        Private Sub IsChecked_Changed(ByVal sender As Object, _

     ByVal e As IsCheckedEventArgs) Handles IsChecked.ValueToggle
            'things I want to be done only when the property IsChecked has been switched between True and False,
            'I don't want to use Exit Sub etc.,
            'just want to learn how to create events...

    'I want also to create IsCheckedEventArgs let's say an argument that specifies the current boolean value, an

    'argument specifies the time the event has been raised at
        End Sub
    End Class

     

     

    thanks.
    Tuesday, November 27, 2007 2:49 PM

Answers

  • The code below should do what your asking for.  You need to create the custom event args class -- I inherit mine from the System.EventArgs class so it can be more dynamically used.  I made teh values readonly so they can;t be manipulated because they reflect the conditions of an event being recorded.  If you made two events -- CheckChanging and CheckChanged events then you could add a Cancel property to the Changing event that would be read by the code that raised it.  If cancelled then the Changed event would not be raised and the conditions surrounding the event could be rolled back.

     

    I used a checkbox control to set off the event as well since I wasn't sure how the DateTimePicker's date value is related to the event timestamp.

     

    Code Block

    Public Class Form1

    Private Event CheckedEvent(ByVal sender As Object, ByVal e As IsCheckedEventArgs)

     

    Private mIsChecked As Boolean

    Public Property IsChecked() As Boolean

    Get

    Return mIsChecked

    End Get

    Set(ByVal value As Boolean)

    If value <> mIsChecked Then

    'The boolean value will derive to Checked (true) or Unchecked (False) but is

    ' defaulted to Indeterminate (non-boolean).

    RaiseEvent CheckedEvent(Me, New IsCheckedEventArgs(value, Now))

    End If

    mIsChecked = value

    End Set

    End Property

     

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

    If sender.Checked Then

    IsChecked = True

    Else

    IsChecked = False

    End If

    'Some other code and operations

    End Sub

    Private Sub IsChecked_Changed(ByVal sender As Object, ByVal e As IsCheckedEventArgs) Handles Me.CheckedEvent

    MessageBox.Show("The checked state changed to " & [Enum].GetName(GetType(CheckState), e.CheckedState) & " at " & e.DateStamp.ToShortDateString & " " & e.DateStamp.ToLongTimeString)

    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    IsChecked = CheckBox1.Checked

    End Sub

    End Class

     

     

    Code Block

    Public Class IsCheckedEventArgs

    Inherits EventArgs

    Private m_CheckedState As CheckState = CheckState.Indeterminate

    Private m_DateStamp As Date = Date.MinValue

    Public ReadOnly Property CheckedState() As CheckState

    Get

    Return m_CheckedState

    End Get

    End Property

    Public ReadOnly Property DateStamp() As Date

    Get

    Return m_DateStamp

    End Get

    End Property

    Public Sub New()

    End Sub

    Public Sub New(ByVal state As CheckState, ByVal theDate As Date)

    m_CheckedState = state

    m_DateStamp = theDate

    End Sub

    End Class

     

     

     

    Tuesday, November 27, 2007 3:58 PM

All replies

  • The code below should do what your asking for.  You need to create the custom event args class -- I inherit mine from the System.EventArgs class so it can be more dynamically used.  I made teh values readonly so they can;t be manipulated because they reflect the conditions of an event being recorded.  If you made two events -- CheckChanging and CheckChanged events then you could add a Cancel property to the Changing event that would be read by the code that raised it.  If cancelled then the Changed event would not be raised and the conditions surrounding the event could be rolled back.

     

    I used a checkbox control to set off the event as well since I wasn't sure how the DateTimePicker's date value is related to the event timestamp.

     

    Code Block

    Public Class Form1

    Private Event CheckedEvent(ByVal sender As Object, ByVal e As IsCheckedEventArgs)

     

    Private mIsChecked As Boolean

    Public Property IsChecked() As Boolean

    Get

    Return mIsChecked

    End Get

    Set(ByVal value As Boolean)

    If value <> mIsChecked Then

    'The boolean value will derive to Checked (true) or Unchecked (False) but is

    ' defaulted to Indeterminate (non-boolean).

    RaiseEvent CheckedEvent(Me, New IsCheckedEventArgs(value, Now))

    End If

    mIsChecked = value

    End Set

    End Property

     

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

    If sender.Checked Then

    IsChecked = True

    Else

    IsChecked = False

    End If

    'Some other code and operations

    End Sub

    Private Sub IsChecked_Changed(ByVal sender As Object, ByVal e As IsCheckedEventArgs) Handles Me.CheckedEvent

    MessageBox.Show("The checked state changed to " & [Enum].GetName(GetType(CheckState), e.CheckedState) & " at " & e.DateStamp.ToShortDateString & " " & e.DateStamp.ToLongTimeString)

    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    IsChecked = CheckBox1.Checked

    End Sub

    End Class

     

     

    Code Block

    Public Class IsCheckedEventArgs

    Inherits EventArgs

    Private m_CheckedState As CheckState = CheckState.Indeterminate

    Private m_DateStamp As Date = Date.MinValue

    Public ReadOnly Property CheckedState() As CheckState

    Get

    Return m_CheckedState

    End Get

    End Property

    Public ReadOnly Property DateStamp() As Date

    Get

    Return m_DateStamp

    End Get

    End Property

    Public Sub New()

    End Sub

    Public Sub New(ByVal state As CheckState, ByVal theDate As Date)

    m_CheckedState = state

    m_DateStamp = theDate

    End Sub

    End Class

     

     

     

    Tuesday, November 27, 2007 3:58 PM
  • thank you bigboy!!!

     

    will you also teach me how to create a CancelEventArgs class, and merge it within the property code?

     

    as well, I would ask you about the DateTimePicker class, the property Checked is not Overridable, so I can't create the events I was talking about, is there any way you do it?

     

    I tried to test it via a MessageBox but it appears only when the control is drawn:

     

    Code Block

    Public Class CheckBoxDateTimePicker : Inherits System.Windows.Forms.DateTimePicker

    Private mChecked As Boolean

    Public Overloads Property Checked() As Boolean

    Get

    Return mChecked

    End Get

    Set(ByVal value As Boolean)

    System.Windows.Forms.MessageBox.Show("I'am within the property!")

    mChecked = value

    End Set

    End Property

    End Class

     

     

     

    as you can see, I used Overloads in the property bt it doesn't work, I think I should use Shadows but I still need some help in here:

     

    Code Block

    Public Shadows Property Checked() As Boolean

    Get

    Return MyBase.Checked()

    End Get

    Set(ByVal value As Boolean)

    If value <> MyBase.Checked() Then

    Dim cancel As Boolean = False

    RaiseEvent CheckedChanging(Me, New CheckedCancelEventArgs(Me.Checked, Now, cancel))

    If Not cancel Then

    MyBase.Checked() = value

    RaiseEvent CheckedChanged(Me, New CheckedEventArgs(Me.Checked, Now))

    End If

    End If

    End Set

    End Property

     

     

    Saturday, December 1, 2007 7:00 PM
  • I tryed to write it like this and it works, I just need to what's your opinion as an expert, is this the way it should be done?

     

     

    Code Block

    Imports System.Windows.Forms

    Public Class CheckBoxDateTimePicker : Inherits DateTimePicker

    Private mIsChecked As Boolean

    Private WriteOnly Property IsChecked() As Boolean

    Set(ByVal value As Boolean)

    Dim CCEA As New CheckedCancelEventArgs(mIsChecked, Now, _

    New Boolean)

    RaiseEvent CheckedChanging(Me, CCEA)

    If CCEA.Cancel Then

    MyBase.Checked = mIsChecked

    Else

    mIsChecked = MyBase.Checked

    RaiseEvent CheckedChanged(Me, _

    New CheckedEventArgs(mIsChecked, Now))

    End If

    End Set

    End Property

     

    Public Sub New()

    mIsChecked = Not MyBase.Checked

    End Sub

     

    Private Sub ValueCheckedChanged() Handles MyBase.ValueChanged

    If MyBase.Checked <> mIsChecked Then

    IsChecked = MyBase.Checked

    End If

    End Sub

     

    Public Event CheckedChanging(ByVal sender As DateTimePicker, _

    ByVal e As CheckedCancelEventArgs)

     

    Public Event CheckedChanged(ByVal sender As DateTimePicker, _

    ByVal e As CheckedEventArgs)

    End Class

     

    Public Class CheckedEventArgs : Inherits EventArgs

    Private mCheckState As Boolean

    Public ReadOnly Property CheckState() As Boolean

    Get

    Return mCheckState

    End Get

    End Property

     

    Private mDateStamp As Date

    Public ReadOnly Property DateStamp() As Date

    Get

    Return mDateStamp

    End Get

    End Property

     

    Friend Sub New(ByVal state As Boolean, ByVal time As DateTime)

    mCheckState = state

    mDateStamp = time

    End Sub

    End Class

     

    Public Class CheckedCancelEventArgs : Inherits CheckedEventArgs

    Private mCancel As Boolean

    Public Property Cancel() As Boolean

    Get

    Return mCancel

    End Get

    Set(ByVal value As Boolean)

    mCancel = value

    End Set

    End Property

     

    Friend Sub New(ByVal state As Boolean, _

    ByVal time As DateTime, ByVal cancel As Boolean)

    MyBase.New(state, time)

    Me.Cancel = cancel

    End Sub

    End Class

     

     

    Saturday, December 1, 2007 9:09 PM
  • Sorry for the delay...  I've had some difficulty logging in.  I'm still a bit unclear as to why you are trying to link events to the datetimepicker checked property so I'll assume its because you need to inspect the date/time it is occurring and possibly cancel if certain conditions are not met.  I readjusted the classes for this purpose using your inherited control.  I added another event to know when the cancel is occurring so you can display a message about it (or do anything else) without interfering with the blocing of the Checked property setting (if you do a message in the CheckChanging event handler then the appearance of the checkbox is not reset until after the message has cleared).

     

    If there is anything I'm missing about your point try to be more specific about the need/concern.

     

     

    Public Class Form1

        'Private Event CheckedEvent(ByVal sender As Object, ByVal e As CheckedEventArgs)
        Friend WithEvents dtp As New CheckBoxDateTimePicker

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            dtp.Location = New Point(20, 20)
            dtp.ShowCheckBox = True
            Me.Controls.Add(dtp)
        End Sub

        Private Sub Check_Changing(ByVal sender As DateTimePicker, ByVal e As CheckedCancelEventArgs) Handles dtp.CheckedChanging
            'I assume you are trying to apply logic whenever the user changes the checked (read: user-controlled-enabled property) state of the custom DateTimePicker.  Based on the date/time of the event you can choose to cancel the user's attempt.  Is this right??
            If e.DateStamp.Hour >= 8 And e.DateStamp.Hour <= 17 Then
                'Allow the user access during work hours
            Else
                e.Cancel = True
            End If
        End Sub

        Private Sub UserAttemptRejected(ByVal sender As DateTimePicker, ByVal e As System.EventArgs) Handles dtp.CheckCancelled
            MessageBox.Show("This capacity is restricted to business hours.")
        End Sub

    End Class

    Public Class CheckBoxDateTimePicker : Inherits DateTimePicker

        Private mIsChecked As Boolean

        Public Sub New()
            MyBase.Checked = False
            mIsChecked = MyBase.Checked
        End Sub

        Private Sub HasCheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ValueChanged
            If MyBase.Checked <> mIsChecked Then
                Dim CCEA As New CheckedCancelEventArgs(mIsChecked, Now)
                RaiseEvent CheckedChanging(Me, CCEA)
                If Not CCEA.Cancel Then
                    mIsChecked = MyBase.Checked
                    RaiseEvent CheckedChanged(Me, New CheckedEventArgs(MyBase.Checked, CCEA.DateStamp))
                Else
                    MyBase.Checked = mIsChecked
                    RaiseEvent CheckCancelled(Me, New System.EventArgs)
                End If
            End If
        End Sub

        Public Event CheckedChanging(ByVal sender As DateTimePicker, ByVal e As CheckedCancelEventArgs)
        Public Event CheckedChanged(ByVal sender As DateTimePicker, ByVal e As CheckedEventArgs)
        Public Event CheckCancelled(ByVal sender As DateTimePicker, ByVal e As System.EventArgs)

    End Class


    Public Class CheckedEventArgs : Inherits EventArgs

        Private mCheckState As Boolean

        Public ReadOnly Property CheckState() As Boolean
            Get
                Return mCheckState
            End Get
        End Property

        Private mDateStamp As Date

        Public ReadOnly Property DateStamp() As Date
            Get
                Return mDateStamp
            End Get
        End Property

        Friend Sub New(ByVal state As Boolean, ByVal time As DateTime)
            mCheckState = state
            mDateStamp = time
        End Sub

    End Class


    Public Class CheckedCancelEventArgs : Inherits CheckedEventArgs

        Private mCancel As Boolean = False

        Public Property Cancel() As Boolean
            Get
                Return mCancel
            End Get
            Set(ByVal value As Boolean)
                mCancel = value
            End Set
        End Property

        Friend Sub New(ByVal state As Boolean, ByVal time As DateTime)
            MyBase.New(state, time)
        End Sub

    End Class

    Monday, December 3, 2007 3:47 PM