Results 1 to 4 of 4

Thread: Custom Controls with Default Behaviours

  1. #1

    Thread Starter
    Hyperactive Member squatman's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    331

    Custom Controls with Default Behaviours

    Hi,

    For various parts of my UI I use Labels and even Panels as a clickable object to trigger something.

    For those Labels and Panels I have the Cursor and Backcolor change on MouseEnter/Leave.

    Rather than having to add these Events every time I add a Label/Panel to be used in this way, is it possible to create a CustomLabel and Panel that has this functionality already applied?

    Thank you.

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

    Re: Custom Controls with Default Behaviours

    The convention for events in .NET is to have a method named OnSomeEvent raise the SomeEvent event. See this blog post for more information about how that's implemented and how you can do it yourself for new events. In your case though, you want to add behaviour on an existing event, rather than add a new event. To do that, you simply override the method that raises that event. In the overridden method, you invoke the base method, which ensures any standard behaviour for that event is performed and the event is raised. Any code you write before that invocation will be executed before any event handlers and any code you write after will be executed after, e.g.
    Code:
    Public Class CustomLabel
        Inherits Label
    
        Protected Overrides Sub OnMouseEnter(e As EventArgs)
            'Code here executes before event handlers.
    
            MyBase.OnMouseEnter(e)
    
            'Code here executes after event handlers.
        End Sub
    
        Protected Overrides Sub OnMouseLeave(e As EventArgs)
            'Code here executes before event handlers.
    
            MyBase.OnMouseLeave(e)
    
            'Code here executes after event handlers.
        End Sub
    
    End Class
    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

  3. #3

    Thread Starter
    Hyperactive Member squatman's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    331

    Re: Custom Controls with Default Behaviours

    Legend, thanks jmc.

    Is there a neat way of setting some default properties e.g. border, padding or should this be done in Protected Overrides Sub OnPaint? If so is likely to cause much overhead setting properties at runtime?

    Thank you

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

    Re: Custom Controls with Default Behaviours

    You can add a parameterless constructor to set initial values for properties. That is the constructor that the designer will invoke so those property values will be honoured when you add an instance in the designer. Be sure to call the base constructor:
    Code:
    Public Class CustomLabel
        Inherits Label
    
        Public Sub New()
            MyBase.New()
    
            'Set your properties here.
        End Sub
    
        Protected Overrides Sub OnMouseEnter(e As EventArgs)
            'Code here executes before event handlers.
    
            MyBase.OnMouseEnter(e)
    
            'Code here executes after event handlers.
        End Sub
    
        Protected Overrides Sub OnMouseLeave(e As EventArgs)
            'Code here executes before event handlers.
    
            MyBase.OnMouseLeave(e)
    
            'Code here executes after event handlers.
        End Sub
    
    End Class
    If you add any other constructors, they should call that constructor so they set those properties too.
    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