Hello buddies, today I’m here with an interesting post in VB.Net series. Yet we have used only drag and drop controls in the VB.Net to create the Windows Form Applications. But today, I will show you how you can create the dynamic controls to create the windows form application. The dynamic controls will appear only in the runtime of the application. That means the visual representation of the controls will not appear till you run the project. On the other hand, when you design your windows form application by simply drag and drop, the controls will appear you to show the actual location as well as the form where you have placed it. Sometimes, it can make little cumbersome to the windows form controls at the runtime. It can take a bit time to load the form. But before moving forward to this tutorial, let’s take a glance at the previous tutorial in which we have created the splash screen with the help of the progress bar.
Dynamic Controls in Vb.Net
VB.Net allows us to create the dynamic controls just like the Java programming language. It is much easy to create the dynamic controls such as TextBox, Label, ComboBox, ListBox, PictureBox etc. These controls can simply drag on the windows form for use. But we can create it using the code. In most cases, we need to create the controls at the runtime. So, let’s start creating it with a new project.
Create a New Project
- Create a new project and name it.
Configure the Windows Form for Dynamic Controls
- Once your project has loaded, just configure the windows form as we have seen previously.
- Here, in the form you can see there is no any controls has dragged. It is a blank windows form.
- Now double click on the form and go the code window.
Code Window of Dynamic Controls Project
- Here, is the code section of the windows form.
- In the form load event paste a single line code to change the background color of the windows form.
Me.BackColor = Color.GreenYellow 'background color of the windows form
- Now, debug the project to see the effect of the code.
- Here, me is a keyword that refers to the current form.
Let’s create a Registration Form just for demonstration purpose using Dynamic controls.
Source code for Dynamic Controls
Public Class RegistrationForm
'declaring variable for controls
Dim lblheading As Label 'Label for heading text
Dim lblname As Label
Dim lbldob As Label
Dim lblfname As Label
Dim lblgender As Label
Dim lbladdress As Label
Dim lblphone As Label
Dim txtname As TextBox
Dim dob As DateTimePicker
Dim txtfname As TextBox
Dim cmbgender As ComboBox
Dim txtaddress As TextBox
Dim txtphone As TextBox
Dim btnsubmit As Button
Private Sub RegistrationForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BackColor = Color.GreenYellow 'background color of the windows form
Me.Size = New Size(700, 500) 'size of the form (width, height)
'Personalizing the appearance of the controls
'Creating a label for heading text
lblheading = New Label()
lblheading.Text = "Registration Form"
lblheading.Location = New Point(250, 10) ' location (x axis, y axis)
lblheading.Font = New Font("Arial", 16, FontStyle.Underline) ' font (font-family, size, variant)
lblheading.Size = New Size(400, 40) ' size (width, height)
'creating label for name
lblname = New Label()
lblname.Text = "Name"
lblname.Location = New Point(100, 60)
lblname.Font = New Font("Arial", 12, FontStyle.Regular)
lblname.Size = New Size(60, 40)
'creating label for date of birth
lbldob = New Label()
lbldob.Text = "Date of Birth"
lbldob.Location = New Point(100, 100)
lbldob.Font = New Font("Arial", 12, FontStyle.Regular)
lbldob.Size = New Size(100, 40)
'creating label for father's name
lblfname = New Label()
lblfname.Text = "Father's Name"
lblfname.Location = New Point(100, 140)
lblfname.Font = New Font("Arial", 12, FontStyle.Regular)
lblfname.Size = New Size(120, 40)
'creating label for gender
lblgender = New Label()
lblgender.Text = "Gender"
lblgender.Location = New Point(100, 180)
lblgender.Font = New Font("Arial", 12, FontStyle.Regular)
lblgender.Size = New Size(120, 40)
'creating label for address
lbladdress = New Label()
lbladdress.Text = "Address"
lbladdress.Location = New Point(100, 220)
lbladdress.Font = New Font("Arial", 12, FontStyle.Regular)
lbladdress.Size = New Size(120, 40)
'creating label for phone no.
lblphone = New Label()
lblphone.Text = "Phone no."
lblphone.Location = New Point(100, 300)
lblphone.Font = New Font("Arial", 12, FontStyle.Regular)
lblphone.Size = New Size(120, 40)
'creating textbox for reading name
txtname = New TextBox()
txtname.Location = New Point(300, 55)
txtname.Size = New Size(300, 40)
txtname.Font = New Font("Arial", 12, FontStyle.Regular)
'creating DateTimePicker for date of birth
dob = New DateTimePicker()
dob.Location = New Point(300, 95)
dob.Format = DateTimePickerFormat.Short
dob.Font = New Font("Arial", 12, FontStyle.Regular)
'creating textbox for father's name
txtfname = New TextBox()
txtfname.Location = New Point(300, 135)
txtfname.Font = New Font("Arial", 12, FontStyle.Regular)
txtfname.Size = New Size(300, 40)
'creating combobox for gender
cmbgender = New ComboBox()
cmbgender.Location = New Point(300, 175)
cmbgender.Size = New Size(200, 40)
cmbgender.Font = New Font("Arial", 12, FontStyle.Regular)
cmbgender.DropDownStyle = ComboBoxStyle.DropDownList
'adding item in the combobox
cmbgender.Text = "-- Select Gender --"
'creating textbox for address
txtaddress = New TextBox()
txtaddress.Multiline = True
txtaddress.Location = New Point(300, 220)
txtaddress.Size = New Size(300, 60)
txtaddress.Font = New Font("Arial", 12, FontStyle.Regular)
txtaddress.ScrollBars = ScrollBars.Vertical
'creating textbox for phone no.
txtphone = New TextBox()
txtphone.Location = New Point(300, 300)
txtphone.Size = New Size(300, 40)
txtphone.Font = New Font("Arial", 12, FontStyle.Regular)
txtphone.MaxLength = "10"
' creating button for submit
btnsubmit = New Button()
btnsubmit.Text = "Submit"
btnsubmit.Size = New Size(200, 40)
btnsubmit.Location = New Point(250, 350)
btnsubmit.BackColor = Color.Honeydew
btnsubmit.Cursor = Cursors.Hand
btnsubmit.ForeColor = Color.Brown
btnsubmit.Font = New Font("Rockwell", 14, FontStyle.Bold)
'Adding controls in the form
- In the above source code, I have declared the variables inside the form class section. The variables are declared as a type of object as the controls.
- In the form load event, we need to create the object of the declared controls in the form class.
- Then one by one, set the location and size of the controls where you want to place it on the form.
- I have defined the Label, TextBox, ComboBox, DateTimePicker and the Button for creating a simple form. Similarly, you can create more controls as per your need to design the form.
- At last, add all controls to the windows form so that the controls will appear when the form will load.
Note: These controls are defined inside the form load event because when the form will load, the controls will appear on to the form. Otherwise, if you place these codes inside any other event, then you will have to perform that event for loading these controls.
An output of the Dynamic Controls
- Before debugging the program you can see the design part of the window form, it is fully blank and there have no any controls on the form.
- Actually, we have placed the overall code inside the form load event so in the design time nothing will show up.
- Save and run to see the result.
- Here you can see when the form has loaded the Dynamic controls have appeared.
- You can design the form according to your requirement it was just for demonstration.
The dynamic controls appear during the runtime of the project. The defined controls will not appear till the form will not load. The overall controls are defined inside the form load event. So the code inside the form load event will work only when the form will load. By following the above code you can create more dynamic controls. So that’s all for this post, in the next post I’ll come with another interesting post. If you need any help regarding this post or any previously posted tutorial, then you can leave your comment in the comment section. Thank you 🙂