How to add misc propety in Button custom control in vb.net

you can use custom control to add additional property in the button through making custom control.
in this article i will add some misc properties in the button custom control and also will show how to use this control in your project.  
First add a custom control from Right click on your project root–>Add–>new item–>custom control
After adding new custom control in your project you will see this code in custom control code file:

Public Class CustomControl1
 
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
 
        'Add your custom paint code here
    End Sub
 
End Class

Rename the class name as you wish. On here i am replacing customcontrol1 to myButton. And change the base class of the control to Button by adding a new line ‘Inherits Button‘.

Public Class myButton
    Inherits Button
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
 
        'Add your custom paint code here
    End Sub
 
End Class

Save your control or project and than Build your project, now you can able to see your control in the toolbox.You can use this control on your project and it would look and act exactly like a standard Button control.    
Next step is to add some additional functionality and visual properties of that control . The following code fragment defines a custom property named TextUpperCase for the your custom control. 

Private _TextUpperCase As Boolean = False
 
    ' The Category attribute tells the designer to set the the lower case and upper case of the text
 
    <Category("Text"), _
    Description("Case of the Text.")> _
    Public Property TextUpperCase() As Boolean
        Get
            Return _TextUpperCase
        End Get
        Set(ByVal value As Boolean)
            _TextUpperCase = value
            If _TextUpperCase Then
                Me.Text = Me.Text.ToUpper()
            Else
                Me.Text = Me.Text.ToLower()
            End If
        End Set
    End Property

From the above we are using category and description attributes for describing the custom control, The Category attribute tells the developer to display it in the Text grouping.and Description attribute provides a description of the property.
We create the ‘TextUpperCase’ custom property as Boolean type. if designer will set this property to true than property set the text case of the button caption to Upper case and if set the False the caption of the text will be change in to lower case.

Now you can build the control and you can use this button control in toolbox with named myButton
you can drag and drop in to your windows form. The TextUpperCase property will be display in the property windows of the control as :

See more on Custom Control

One thought on “How to add misc propety in Button custom control in vb.net”

Comments are closed.