Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class that inherits from button and in it I have a method that makes the button grow in size when focused, and a property to establish whether this should be allowed or not. If it is allowed, I'd like to be able to determine the size of the button's enlargement with a sub property.

This is my code:

Imports System.Windows.Forms
Public Class ClearButton
    Inherits Windows.Forms.Button
    Public Grow As Boolean
    Public n As Integer
    Public Growth As Integer = n * 2
    Public Sub New()
        Me.FlatStyle = FlatStyle.Flat
        Me.BackgroundImage = My.Resources.bClear
        Me.BackgroundImageLayout = ImageLayout.Stretch
        Me.BackColor = Drawing.Color.Black
        Me.FlatAppearance.MouseDownBackColor = Drawing.Color.Transparent
        Me.FlatAppearance.MouseOverBackColor = Drawing.Color.Transparent
        Me.FlatAppearance.BorderSize = 0
        Me.ForeColor = Drawing.Color.White
        Me.AllowEnlargeWhenFocus = False
    End Sub

    Public Property AllowEnlargeWhenFocus() As Boolean
        Get
            Return Me.Grow
        End Get
        Set(ByVal value As Boolean)
            Me.Grow = value
        End Set
    End Property

    Private Sub ClearButton_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        If Grow = True Then
            Me.BringToFront()
            Me.SetBounds(Me.Left - n, Me.Top - n, Me.Size.Width + Growth, Me.Size.Height + Growth)
        End If
    End Sub
End Class


So I'd like to have a sub property to determine "n". And then it could be accessed like so:
VB
ClearButton1.AllowEnlargeWhenFocus.mSize = 3

Is there a way I can do that or should I just add the nSize property as a different property instead?
Posted

1 solution

A "sub-Property" is simply a Property that is also a Class or Structure.
So having a Property such as the following will allow you to call ClearButton.AllowEnlargeWhenFocus.mSize = 3
VB
Public Class Enlargement
   Public Property mSize As Integer
End Class

Public Class ClearButton
   ' Stuff...
   Private _enlargement As Enlargement
   Public Property AllowEnlargeWhenFocus As Enlargement
      Get
         Return _enlargement
      End Get
      Set(Byval value As Enlargement)
         _enlargement = value
         Me.Resize(value.mSize)
      End Set
   End Property
   ' More stuff...
End Class
However I do not quite see why this would be necessary. AllowEnlargeWhenFocus sounds as though it is a Boolean. It seems all your ClearButton would need is another Property, let's call it Enlargement, that is an Integer and will be used for resizing when AllowEnlargeWhenFocus is True.
Having a Class (or Structure)with just one Property like that makes no sense at all, neither does starting a Property name with "Allow" and not making it a Boolean. Or having a Public Property be called mSize (CamelCase[^] for Public Properties and Methods is widely accepted as the standard).
Hope that helps you solve your problem :)
 
Share this answer
 
v2
Comments
Vic91 7-Nov-11 19:51pm    
Yes that is what I though. And AllowEnlargeWhenFocus IS boolean.. I just wasn't sure what the propper way of doing this was. Thank you very much!
Sander Rossel 8-Nov-11 2:11am    
No problem :)
Vic91 7-Nov-11 20:09pm    
Just one more thing, how can I code a description for the properties? So that when I'm coding and I call the property, the tooltip will tell me what it's for?
Sander Rossel 8-Nov-11 2:14am    
You can type ''' directly above any Property, Method, Class... etc. This will create an XML comment. Like this:
''' <summary>
''' Your description goes here.
''' </summary>
''' <returns>Some value.</returns>
Public Property AllowEnlarge As Boolean

Notice that you can add more comment fields by typing < intellisense will show you your option. All comments are optional. Simply delete the opening and closing tags to get rid of it :)
Vic91 10-Nov-11 0:25am    
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900