jump to navigation

Creating User Defined Controls in VB.NET July 18, 2008

Posted by prasu in : Technical, VB.NET , 4 comments

In many applications we need tailor-made controls to suit our programming needs. Such controls can be created in VB.NET.

  1. To create a user-defined control you have to use Windows Control Libray.
    Go to file –> New Project–> Windows Forms Contol Library –> (Give any desired name to your project) –> Ok.
  2. You will find a Canvas. Now using the controls present in the toolbox create the required component (control).
  3. Code the Control as you want it to work.
  4. You can add user defined properties by using property keyword.
  5. The size of the canvas is the size of the user defined control you are creating. So change the canvas size as required at the end.
  6. After you have written the code check the control by debugging the project.
  7. Now go to Build –> Build <component name>.
  8. To add the Component (control) in the toolbox, right click in the empty area of the Toolbox –> Choose Items –> Click on browse. (This should be in a new VB.NET Windows Application)
    Go to the Project Folder (This time, its the Control Library Project you created) –> Bin –> Debug –> Select <componentname>.dll file
    Check the Component name in .NET Framework Components Tab –> OK.
  9. The new Component (control) is added to your toolbox.

Ex: Creating a text-flipping banner control.

  1. Create a windows library form and name it Banner.
  2. In the Canvas drag two Textboxes and add a Timer.
  3. Set the Timer’s enabled property true,Set the interval to say 1000 (unit:milliseconds).
  4. Set some default Text in both the Textboxes.
  5. Double click on the timer and write the below code.
  6. Then write two properties named first and second which are used to change the text of the banner.
  7. Drag one textbox on top of other and then reduce the canvas size such that it covers only the textboxes.
  8. Debug the program, The Text of the banner oscillates between the two values of the textboxes.You can change the text of the first and second text box using property first and second respectively.
  9. If it is working properly stop debugging and click on build –> build banner.

Public Class Banner

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Static f As Boolean
        If f = True Then
            Me.TextBox1.Visible = True
            Me.TextBox2.Visible = False
        Else
            Me.TextBox1.Visible = False
            Me.TextBox2.Visible = True
        End If
        f = Not f
    End Sub

    Public Property first() As String
        Get
            Return Me.TextBox1.Text
        End Get
        Set(ByVal value As String)
            Me.TextBox1.Text = value
        End Set
    End Property

    Public Property second() As String
        Get
            Return Me.TextBox2.Text
        End Get
        Set(ByVal value As String)
            Me.TextBox2.Text = value
        End Set
    End Property

End Class

In the above code within a property we see two more keywords
get:It returns a value to the caller.
set:It sets the value to the given property.


Warning: stristr() [function.stristr]: Empty delimiter in /home/tekyt17/public_html/dotnet/wp-content/plugins/wassup/wassup.php on line 2093