Windows Forms | VB.Net

VB.Net programmers have adeptly employed forms as a fundamental tool for constructing compelling user interfaces. Whenever you embark on the creation of a Windows application, the Visual Studio IDE seamlessly presents you with a default blank form, acting as a blank canvas upon which you can unleash your creativity.

To expedite the development process, Visual Studio equips you with the convenience of effortlessly dragging and dropping controls from the expansive array available within the Visual Studio Toolbox window. This intuitive approach empowers you to seamlessly incorporate and arrange controls, facilitating the creation of visually appealing and interactive user interfaces.

how to vb.net form

To commence the process, the initial step involves initiating a new project and constructing a form. Launch your Visual Studio IDE and navigate to the top menu. From there, select "File" and choose "New Project" from the drop-down menu. A dialog box labeled "New project" will appear, providing a range of options. Within this dialog box, opt for "Visual Basic" as the programming language. Next, locate and select "Windows Forms Application" to indicate your intention to build a Windows-based application.

To proceed, replace the default name "WindowsApplication1" displayed at the bottom of the dialog box with your desired project name. Once you have specified your project name, click the "OK" button to proceed. A visual representation illustrating the step-by-step process of creating a new form within Visual Studio is depicted below for your reference.

vb.net new project

Select project type from New project dialog Box.

new vb.net application

Upon adding a Windows Form to your project, a multitude of default properties are automatically assigned to the form. While these default values may be convenient in certain instances, it is important to recognize that they may not always align with your specific programming requirements. As a result, customization becomes necessary.

For visual reference, the following image presents an illustration of how the default appearance of a Form looks like within Visual Studio. This depiction serves as a starting point, allowing you to envision the initial structure and layout of the form as you embark on tailoring it to suit your desired user interface design and functionality.

vb.net new form

Located at the uppermost part of the form is the title bar, a crucial component that showcases the title of the form. By default, the title bar displays the name "Form1," serving as the initial identifier for the form. However, you have the flexibility to modify this name according to your preferences, thereby ensuring a more meaningful and personalized representation.

Additionally, the title bar encompasses the control box, an integral element housing essential buttons. These buttons provide key functionality, enabling users to minimize, maximize, and close the form. The minimize button reduces the form to the taskbar, the maximize button enlarges the form to occupy the entire screen, and the close button terminates the form and returns control to the operating system.

Together, the title bar and control box form an essential visual and functional component of the form, facilitating user interaction and navigation within the application.

If you want to set any properties of the Form, you can use Visual Studio Property window to change it.

vb.net form properties

For example , to change the forms title from Form1 to MyForm, click on Form1 and move to the right side down Properties window, set Text property to MyForm. Then you can see the Title of the form is changed. Likewise you can set any properties of Form through Properties window.

You can also set the properties of the Form1 through coding. For coding, you should right-click the design surface or code window and then clicking View Code.

vb.net view source code

When you right click on Form then you will get code behind window, there you can write your code.

For example , if you want to change the back color of the form to Brown , you can code like this.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.BackColor = Color.Brown End Sub

Likwise you can change other properties of Form1 through coding.

The following VB.Net source code shows how to change the Title, BackColor, Size, Location and MaximizeBox properties of Form1. Copy and paste the following VB.Net source code to source code editor of your Visual Studio.

Full Source VB.NET
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Text = "Change Prperties Through Coding" Me.BackColor = Color.Brown Me.Size = New Size(350, 125) Me.Location = New Point(300, 300) Me.MaximizeBox = False End Sub End Class

When you execute (press F5 key) the program the form is look like the following image.

vb-form-output.jpg

The Windows based programs you create using Visual Basic .NET run in the context of a form. When you close the form, the application also ends.

Benefits of Using Windows Forms in VB.NET

  1. Rapid Prototyping: Visually design your UI early in the development process, enabling quick feedback and iteration.
  2. Rich Set of Controls: Extensive library of pre-built controls for common UI elements (buttons, text boxes, list views, menus, etc.), reducing development time.
  3. Event-Driven Programming Model: Respond to user interactions and other events, making your application dynamic and responsive.
  4. Integration with .NET Framework: Leverage the vast .NET class library for various tasks like data access, networking, file I/O, and more.
  5. Wide Deployment: Windows Forms applications can run on any Windows machine without requiring additional installations.

VB.NET Form Properties

Property Name
Description
Text
Sets the text displayed in the caption bar of the form.
BackColor
Sets the background color of the form.
Size
Gets or sets the size (width and height) of the form.
Location
Gets or sets the location (X and Y coordinates) of the form on the screen.
Enabled
Gets or sets a value indicating whether the form is enabled (interacts with user input).
ShowInTaskbar
Gets or sets a value indicating whether the form appears in the Windows taskbar.

VB.NET Form Methods

Method Name
Description
Show()
Displays the form on the screen.
Close()
Closes the form.
ShowDialog()
Displays the form modally, meaning it takes user focus and prevents interaction with other windows until it's closed.
Hide()
Hides the form from view.
BringToFront()
Brings the form to the front of all other windows in the application.
Focus()
Sets focus to the form, making it the active window.

VB.NET Form Events

Event Name
Description
Load
Occurs when the form is loaded and ready to be displayed.
FormClosing
Occurs when the user attempts to close the form (e.g., clicks the close button or presses Esc).
FormClosed
Occurs after the form has been closed.
Click
Occurs when the user clicks anywhere on the form itself (not on specific controls).
KeyDown
Occurs when a key is pressed while the form has focus.
KeyPress
Occurs when a key is pressed and released while the form has focus (useful for handling character input).

Conclusion

Windows Forms in VB.NET provides a robust framework for creating graphical user interfaces (GUIs) in desktop applications. Leveraging a drag-and-drop design approach and a rich set of controls, developers can quickly build interactive and visually appealing applications, enhancing user experience and productivity. Additionally, seamless integration with the .NET ecosystem and comprehensive event-driven programming capabilities empower developers to create dynamic and responsive desktop applications with ease.