Quantcast
Channel: AuthorCode » Windows Controls
Viewing all articles
Browse latest Browse all 10

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

$
0
0

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:

  1. Public Class CustomControl1
  2.  
  3.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  4.         MyBase.OnPaint(e)
  5.  
  6.         'Add your custom paint code here
  7.     End Sub
  8.  
  9. 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‘.

  1. Public Class myButton
  2.     Inherits Button
  3.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  4.         MyBase.OnPaint(e)
  5.  
  6.         'Add your custom paint code here
  7.     End Sub
  8.  
  9. 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. 

  1. Private _TextUpperCase As Boolean = False
  2.  
  3.     ' The Category attribute tells the designer to set the the lower case and upper case of the text
  4.    
  5.     <Category("Text"), _
  6.     Description("Case of the Text.")> _
  7.     Public Property TextUpperCase() As Boolean
  8.         Get
  9.             Return _TextUpperCase
  10.         End Get
  11.         Set(ByVal value As Boolean)
  12.             _TextUpperCase = value
  13.             If _TextUpperCase Then
  14.                 Me.Text = Me.Text.ToUpper()
  15.             Else
  16.                 Me.Text = Me.Text.ToLower()
  17.             End If
  18.         End Set
  19.     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


Viewing all articles
Browse latest Browse all 10

Trending Articles