-
Mar 19th, 2025, 07:24 AM
#1
Thread Starter
Hyperactive Member
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.
-
Mar 19th, 2025, 07:38 AM
#2
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
-
Mar 19th, 2025, 10:26 AM
#3
Thread Starter
Hyperactive Member
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
-
Mar 19th, 2025, 09:52 PM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|