- A Form in Windows Forms applications is a window or dialog box that acts as the main user interface.
- Forms contain controls like buttons, textboxes, labels, etc., to interact with the user.
- To organize different functionalities or screens of an application.
- Each form can represent a different window, such as a login form, settings form, or report display.
- Allows modular design and improves code manageability.
1. Open Your Project in Visual Studio
- Ensure your Windows Forms project is open.
- Right-click on the project in the Solution Explorer.
- Choose Add → Windows Form...
- In the dialog box, enter a name for the new form (e.g.,
Form2.vb).
- Click Add.
- Visual Studio opens the new form in the Designer view.
- You can drag and drop controls (buttons, labels, textboxes, etc.) from the Toolbox.
- Set properties like
Text, Size, BackColor, etc.
- Switch to the Code view to write event handlers and other logic.
- For example, add code to respond to button clicks or form load events.
' Inside Form1 code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form2()
frm.Show() ' Shows Form2 as a non-modal window
' Or use frm.ShowDialog() to open as a modal dialog
End Sub
| Form Type |
Description |
Method to Show |
| Modal |
User must interact and close this form before returning to other forms |
ShowDialog() |
| Non-Modal |
User can switch between multiple open forms freely |
Show() |
✅ Important Points
- Each form is a separate class derived from
System.Windows.Forms.Form.
- You can pass data between forms by defining constructors or properties.
- Properly dispose forms to free resources when they are closed.
📝 Summary
| Step |
Description |
| Add New Form |
Use Add → Windows Form in Solution Explorer |
| Design Form |
Use Designer to add controls and set properties |
| Write Code |
Add event handlers and logic in Code view |
| Show Form |
Use Show() or ShowDialog() from existing form |
✅ What Does Adding Controls at Runtime Mean?
- Normally, controls (like buttons, textboxes) are added to a form at design time using the Visual Studio Designer.
- Adding controls at runtime means creating and adding controls programmatically while the application is running.
- Useful for dynamic user interfaces where controls are created based on conditions or user inputs.
✅ How to Add Controls at Runtime
- Create the control object using the appropriate class.
- Set properties of the control (like size, position, text).
- Add the control to the form’s
Controls collection.
- Optionally, attach event handlers to respond to user actions.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Create a new Button
Dim btn As New Button()
' Set properties
btn.Name = "btnClickMe"
btn.Text = "Click Me"
btn.Size = New Size(100, 30)
btn.Location = New Point(50, 50)
' Add an event handler for the Click event
AddHandler btn.Click, AddressOf btn_Click
' Add button to the form
Me.Controls.Add(btn)
End Sub
' Event handler method
Private Sub btn_Click(sender As Object, e As EventArgs)
MessageBox.Show("Button clicked!")
End Sub
✅ Key Points
- Use the control’s constructor to create an instance (
New Button(), New TextBox(), etc.).
- Properties like
Name, Text, Size, and Location define how the control appears and where.
- Use
Me.Controls.Add(control) to add the control to the form.
- Use
AddHandler to attach event handlers for control events.
- Controls added at runtime behave just like design-time controls.
✅ Advantages of Adding Controls at Runtime
- Flexibility to create UI based on user input or program logic.
- Can add varying numbers of controls dynamically.
- Useful in scenarios like dynamic forms, wizards, or generated reports.
📝 Summary Table
| Step |
Description |
Code Example |
| Create Control |
Instantiate the control class |
Dim btn As New Button() |
| Set Properties |
Set text, size, location, name, etc. |
btn.Text = "Click Me" |
| Attach Event Handlers |
Link event to a handler method |
AddHandler btn.Click, AddressOf btn_Click |
| Add to Form Controls |
Add control to form’s Controls collection |
Me.Controls.Add(btn) |
- Form inheritance allows you to create a new form that inherits the properties, methods, and controls of an existing form.
- The new form (derived form) extends or customizes the base form without rewriting common code.
- Helps promote code reuse and consistent UI design across multiple forms.
- A base form class is a form created to contain common functionality, controls, or layout shared by multiple derived forms.
- Derived forms inherit from this base form class and add or override features as needed.
- To avoid duplication of code and controls on multiple forms.
- To maintain a consistent look and feel.
- To centralize common logic (e.g., logging, authentication, styling).
- Add a new Windows Form to the project (e.g.,
BaseForm.vb).
- Design the base form by adding common controls (like headers, menus).
- Write shared methods or event handlers here.
- Add a new form.
- Instead of inheriting from
System.Windows.Forms.Form, inherit from your base form.
In the derived form code, change inheritance as:
Public Class DerivedForm
Inherits BaseForm
End Class
✅ Example:
Public Class BaseForm
Inherits System.Windows.Forms.Form
Public Sub New()
InitializeComponent()
' Add common initialization here
End Sub
Protected Sub ShowWelcomeMessage()
MessageBox.Show("Welcome to the Application!")
End Sub
End Class
Public Class DerivedForm
Inherits BaseForm
Private Sub DerivedForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Call base class method
ShowWelcomeMessage()
End Sub
End Class
✅ Important Notes
- The base form can have controls added at design time or runtime.
- Derived forms inherit all controls and code from the base form.
- You can override base class methods using
Overrides keyword.
- You can add new controls or methods specific to derived forms.
- Changes to the base form affect all derived forms.
| Benefit |
Description |
| Code Reuse |
Write common code once, use many times |
| Consistency |
Uniform UI and behavior across forms |
| Easier Maintenance |
Fix or update base form to affect all |
| Extensibility |
Customize specific forms without affecting others |
Creating Your Own Base Classes
✅ What is a Base Class?
- A base class is a class designed to be a parent class for other classes.
- It contains common properties, methods, and functionality that multiple derived classes can inherit.
- Base classes help in code reuse, organization, and enforcing consistent behavior.
✅ Why Create Your Own Base Class?
- To avoid duplicating common code across multiple classes.
- To provide a standard structure or behavior that all derived classes share.
- To simplify maintenance by updating shared functionality in one place.
- To create a customized foundation suited to your application’s needs.
✅ Creating a Base Class in VB.NET
- Define a class with
Public Class.
- Include properties, methods, and events common to all derived classes.
- Use
MustInherit keyword if you want to make the base class abstract (cannot be instantiated directly).
- Derived classes inherit using the
Inherits keyword.
Public Class MyBaseForm
Inherits System.Windows.Forms.Form
' Common property
Public Property UserName As String
' Common method
Public Sub ShowWelcome()
MessageBox.Show("Welcome, " & UserName & "!")
End Sub
' Common event handler
Protected Sub LogEvent(ByVal message As String)
' Code to log event, e.g. write to file or database
End Sub
End Class
Public Class CustomerForm
Inherits MyBaseForm
Private Sub CustomerForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
UserName = "CustomerUser"
ShowWelcome()
LogEvent("CustomerForm loaded")
End Sub
End Class
✅ Key Concepts
| Concept |
Explanation |
Inherits |
Used by derived class to inherit base class code |
MustInherit |
Makes a class abstract, cannot create instance |
| Overriding |
Use Overrides keyword to change base method behavior |
| Access Modifiers |
Use Public, Protected, Private for control visibility |
✅ Advantages of Creating Your Own Base Classes
- Promotes code reusability.
- Ensures consistent behavior across derived classes.
- Makes application easier to maintain and extend.
- Encourages clean and organized code design.
✅ Summary
| Step |
Description |
| Define Base Class |
Create a class with common functionality |
| Add Properties/Methods |
Include shared properties and methods |
| Inherit Base Class |
Use Inherits in derived classes |
| Extend or Override |
Add or change functionality in derived classes |