Custom Button Attributes

  • 13 years ago
    Hey. I'm a bit of a newbie to vb .net.
    I'm curious if it's possible some one could point me in the direction of some pre-written custom button (particularly with more attribute fields for info), or let me know step by step how to add some custom attributes for the default button?

    I've read a few items on adding custom attributes and writing custom controls, but I'm afraid they're slightly above my current understanding. I'd apprechiate it if someone could assist. Thanks.


  • 13 years ago

    What you'd like to do is extend the button class.  This is pretty easy and I'll walk you through a simple example.  Once you understand it maybe the more complicated examples you've seen will make a little more sense.  At the very least you'll have a starting point.

    1) Start a new project (windows application will be fine)
    2) Click Properties > Add class
    3) Give the class a usefull name like ExtendedButton and then click Add
    4) Double click on the class in the solution explorer to bring up the code window
    5) Under the class declaration type 'Inherits Button' and hit Enter



    If you don't know about inheritance you should do some research on class inheritance so your more familiar with it.  For now just know that by inheriting the Button class we've now created a Button with all the properties and methods of the standard Button.  As a rule of thumb if a control does mostly what you want you should try to inherit from it and then modify it to your liking. All you want are a few extra fields so this is definitly the right approach.

    6) Add a private string variable 'm_Field1'

    This variable will contain the properties value that we'll create next.  When creating properties I like to name the local variable using the convention m_PropertyName.  I've seen other nameing conventions like _PropertyName or mPropertyName.  Pick the one you like or use something else it's up to you, but be consistent.

    7) Type 'Public Property Field1 as String' and hit Enter

    The rest of the code for the property will be created for you when you hit enter.  Again if your not familiar with creating properties do a little research to familirze yourself with them.  In general it's good programming practice to use properties instead of public variables.  For instance declaring m_Field1 as Public and nameing it Field1 would have acomplished the same final result we'll have after we've finished the property.  However, I like to stick with properties for everything even if they are very simple like this example.

    8) In the Get portion of the property add the code 'Return m_Field1'
    9) In the Set portion of the property add the code 'm_Field1 = value'
    10) Run your program and then stop it. This should cause your control toolbox to refresh and your new control will be added to the list with the name 'ExtendedButton' or whatever you chose as a name.  As an alternative I believe building the solution will also refresh the list, but running it instead will save you the hassle of needing to save your project to build it.

    At this point you should have an extended button with an additional property 'Field1'.  This Button should also appear in your toolbox.  You can now use this Button just like a normal Button.  Hope that helped.  Here is the code you should have if you've followed everything I said.

    Public Class ExtendedButton
        Inherits Button
    
        Private m_Field1 As String = ""
    
        Public Property Field1() As String
            Get
                Return m_Field1
            End Get
            Set(ByVal value As String)
                m_Field1 = value
            End Set
        End Property
    End Class

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila” - Mitch Ratcliffe