How to create your first custom control in C# – Numeric Textbox

Suppose you need to add some additional functionality to your windows control in your application and you have requirement to use this control with same additional functionality many times in the application. This is same as when we use the textbox or label control classes in the Windows form.

In addition you can also create your own controls by inherit these controls classes and adding new features and overriding existing functionality. The .Net framework provides the many ways to create your own custom controls – you can create custom control by just inherit from an existing control and by inherit from the control base class using graphics capabilities etc.

Ways to create custom control in C#

In this article I will show how to start with custom control. In this article we will create a textbox control that will accept only numeric value. However this functionality is very common and you don’t need to create a custom control for this functionality (you can use maskedtextbox), but it is very simple to understand the how to create custom control. So let’s starts:

1. Add a custom control template in your project (Add–>New item–>Custom control).

You can see the code of the newly added class as:

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

 
2. And Now Add a line to inherit – we will write a new line so that it inherits from the TextBox base class( see the below).

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

 
3. Add additional functionality

Public Class CustomControl1
    Inherits TextBox
    Protected Overrides Sub OnPaint(ByVal e _
                 As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        'Add your custom paint code here
    End Sub
 
    Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
        If Not Char.IsControl(e.KeyChar) AndAlso _
                       Not Char.IsDigit(e.KeyChar) Then
            e.Handled = True
        End If
        MyBase.OnKeyPress(e)
    End Sub
End Class

 
4. Build your project. After successfully build you will be able to see your own custom control in Toolbox.

custom control in toolbox
Now you can use this control in your project. I hope now it is easier to create another custom control with some more functionality for you. Thanks.

5 thoughts on “How to create your first custom control in C# – Numeric Textbox”

  1. hey there, i want to build a textbox custom that has the property of change the border color… how can i do it? can you help me

    1. Good question..

      Actually Some Windows Forms controls, such as TextBox, are painted directly by Windows. In these instances, the OnPaint method is never called. so you don’t change the border color with this type of event.

      For it you have to create a user control that should not be inherit by the TextBox class.

      Try to create this and let me know if you face any problem.

Comments are closed.