Creating User Defined Controls in VB.NET July 18, 2008
Posted by prasu in : Technical, VB.NET , 4 commentsIn many applications we need tailor-made controls to suit our programming needs. Such controls can be created in VB.NET.
- 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. - You will find a Canvas. Now using the controls present in the toolbox create the required component (control).
- Code the Control as you want it to work.
- You can add user defined properties by using property keyword.
- 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.
- After you have written the code check the control by debugging the project.
- Now go to Build –> Build <component name>.
- 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. - The new Component (control) is added to your toolbox.
Ex: Creating a text-flipping banner control.
- Create a windows library form and name it Banner.
- In the Canvas drag two Textboxes and add a Timer.
- Set the Timer’s enabled property true,Set the interval to say 1000 (unit:milliseconds).
- Set some default Text in both the Textboxes.
- Double click on the timer and write the below code.
- Then write two properties named first and second which are used to change the text of the banner.
- Drag one textbox on top of other and then reduce the canvas size such that it covers only the textboxes.
- 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.
- 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.
Binding Data To Controls July 1, 2008
Posted by prasu in : Databases, Technical, VB.NET , add a commentIn practical applications it is not always convenient to retrieve data in DataGridView (as done in a previous example). Data can also be retrieved in a more elegant manner and bound to Controls like TextBoxes or Labels. For this purpose, we come across a new object type known as BindingSource.
Binding Source binds the data from a column in a table to a control.
For this example, we will be using the same table as we did for the DataGridView Example (my previous post)
Example:
- Drag three Labels, three Textboxes and two buttons to a new Windows form.
- Change the text property of the three Labels to Name, City and Date of Birth.
- Change the text property of the buttons to Next and Previous.
- Double click on the form and within the form_load method type the code as shown below.
- Double click on the Next button and within the Click type the code-
bs.MoveNext() - Double click on the previous button and within the Click event type the code-
bs.MovePrevious()
Public Class Form3
Dim cn As New Odbc.OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=mydb; User=root;Password=;")
Dim cmd As New Odbc.OdbcCommand
Dim adp As Odbc.OdbcDataAdapter
Dim ds As New DataSet
Dim bs As BindingSource
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn.Open()
cmd.Connection = cn
cmd.CommandText = "Select * from mytable"
adp = New Odbc.OdbcDataAdapter(cmd)
adp.Fill(ds, "mytable")
'Binding Statements Follow
bs = New BindingSource(ds, "mytable")
'Bind the name field to first textbox, type of data as text
Me.TextBox1.DataBindings.Add("text", bs, "name")
Me.TextBox2.DataBindings.Add("text", bs, "city")
Me.TextBox3.DataBindings.Add("text", bs, "dob")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
bs.MoveNext()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
bs.MovePrevious()
End Sub
End Class
In the above code:
- The name,city and the dob (Date of Birth) columns of the table named ‘mytable’ (this is the same table I used in an earlier post) is bound to three textboxes namely textbox1,textbox2 and textbox3 respectively using statement- Me.TextBox1.DataBindings.Add(”text”, bs, “name”) and so on.
- bs.MoveNext() binds the next record in the table to the controls (as long as a ‘next’ exists).
- bs.MovePrevious() binds the previous record in the table to the controls (as long as a ‘previous’ exists).
