Results 1 to 2 of 2

Thread: [2005] Help need in creating custom control?

  1. #1

    Thread Starter
    Addicted Member senthilkumartd's Avatar
    Join Date
    Feb 2005
    Posts
    206

    [2005] Help need in creating custom control?

    Hi,
    I wish to create custom control. I need to add events for the control that I am creating. But I don't know how to do. So please provide links for creating events and raising it.

    thanks
    God has been pleased to place as a king or cobbler do the work sincerely

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Help need in creating custom control?

    The mechanism for raising events in VB.NET is child's play. Here's a generic example:
    VB Code:
    1. Public Class Class1
    2.  
    3.     'A private field to store the value.
    4.     Private myVar As Object
    5.  
    6.     'A public property to expose the value.
    7.     Public Property MyProperty() As Object
    8.         Get
    9.             Return Me.myVar
    10.         End Get
    11.         Set(ByVal value As Object)
    12.             If value IsNot Me.myVar Then
    13.                 'The new value is different so set the field and raise the event.
    14.                 Me.myVar = value
    15.                 Me.OnMyPropertyChanged(EventArgs.Empty)
    16.             End If
    17.         End Set
    18.     End Property
    19.  
    20.     'A public event to indicate that the property value has changed.
    21.     'Note that the event name is the property name with a "Changed" suffix.
    22.     Public Event MyPropertyChanged(ByVal sender As Object, ByVal e As EventArgs)
    23.  
    24.     'A protected method to raise the event.
    25.     'Note that the method name is the event name with an "On" prefix.
    26.     Protected Sub OnMyPropertyChanged(ByVal e As EventArgs)
    27.         RaiseEvent MyPropertyChanged(Me, e)
    28.     End Sub
    29.  
    30. End Class
    Events don't have to be linked to a specific property value changing but they commonly are.

    If the methods that will handle your event need to receive data about it then you would use a type derived from EventArgs instead of an EventArgs object. You can use an existing type if it suits or declare your own. In that case you would create a new instance in the property setter and pass it to the method that raises the event.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width