Visual Basic

How To Create A Form In Visual Basic

To create a form in Visual Basic, start a Windows Forms project in Visual Studio, place controls on the designer, set their properties, and add event handlers for user actions. Run the application to check its behaviour, then add input validation and test the layout.

Which licences fit your PC setup?

Prices are an AUD snapshot, not a live quote. Confirm variant and total on the listing.

Create a Visual Basic Windows Forms project

This guide covers Visual Basic for .NET using Windows Forms on Windows. A form is an application window that contains controls such as labels, text boxes and buttons. For a first project, build a small greeting form: the user enters a name, clicks a button and sees a message.

  1. Open Visual Studio and select Create a new project.
  2. Search for Windows Forms and filter the language to Visual Basic. Choose a Windows Forms App template that matches your project's target framework.
  3. Enter a project name and choose a location, then complete the project creation prompts.
  4. Open Form1.vb in the designer. If you see source code instead, right-click the file in Solution Explorer and select View Designer.

If the template is missing, check that the .NET desktop development workload is installed through the Visual Studio Installer. Microsoft's Visual Basic Windows Forms tutorial explains the required setup.

The project already includes a starting form; you do not need to add another one before designing your interface. For related language topics, browse the Visual Basic guides.

Set the form's title and layout

Select an empty area of the form and open the Properties window. The Text property sets the caption displayed in the window's title bar. Change it to Greeting. Resize the form using its sizing handles or the Size property, leaving room for input, an action button and a result.

Keep the distinction between Name and Text clear. Name identifies an object in your code; Text supplies its displayed wording. Changing a button's caption to Greet does not automatically give it a useful code name.

Make the layout usable

  • Place labels beside their inputs and keep related controls together.
  • Use readable fonts and enough contrast between text and background colours.
  • Leave room for longer input and messages rather than designing only for a short example.
  • Use Anchor to maintain a control's distance from selected container edges, or Dock to attach it to an edge or fill its container.
  • Set TabIndex values so keyboard focus follows the order in which users complete the form.

Add controls that match the task

Open the Toolbox through View > Toolbox while the designer is active. Drag controls onto the form, then select each one to edit its properties. Choose controls according to the information users need to enter or read.

Control Use it for
Label Field captions, instructions and short results.
TextBox Free text input, such as a person's name.
Button An explicit action, such as calculating or submitting.
ComboBox A list of choices. Use DropDownList when users must select an existing item.
CheckBox An independent option that can be selected or cleared.
RadioButton One choice from a group of mutually exclusive options.

For the greeting example, add a Label with Text set to Name, a TextBox named txtName, a Button named btnGreet with Text set to Greet, and another Label named lblResult. Clear lblResult's Text property so no result appears before the user acts.

Connect the button to an event handler

Windows Forms uses events to respond to interaction. A button raises Click when activated; your event handler contains the instructions that run in response.

  1. Double-click btnGreet in the designer to generate its Click handler and open the code editor.
  2. Between the generated Private Sub and End Sub lines, enter this statement: lblResult.Text = "Hello, " & txtName.Text.Trim() & "!"
  3. Keep the generated handler declaration, including its Handles btnGreet.Click clause, so the procedure remains connected to the button.
  4. Save the file and return to the designer when you need to adjust the interface.

The statement reads the name, removes surrounding whitespace and assigns the greeting to the result label. It demonstrates interaction but still needs a rule for empty input. Microsoft explains how event connections work in its guide to adding and removing Windows Forms event handlers.

Use TextChanged when behaviour should follow changes to text, and SelectedIndexChanged when it should follow a changed list selection. Keep substantial processing in separate methods so event handlers remain easy to follow.

Validate input before processing it

A standard Windows Forms TextBox has no Required property. Enforce required fields in code. For the greeting form, check String.IsNullOrWhiteSpace(txtName.Text) before assigning the greeting. If it returns True, display “Enter your name”, return focus to txtName and exit the handler without producing a greeting.

For a larger form, use each input's Validating event and an ErrorProvider component to show field errors. Call ValidateChildren before saving and proceed only when validation succeeds. Clear an input's error once its value is valid. Microsoft's input validation guide describes these approaches.

For numeric fields, use a suitable TryParse method to check conversion and then enforce the permitted range. A TextBox accepts text; its appearance does not make its contents a valid number.

Handle failures separately from invalid input

File access and other external operations can fail even after input passes validation. Use Try...Catch around operations whose failures you can handle, catch specific exception types, and give the user an actionable message. Use Finally for necessary cleanup. Microsoft's exception handling reference explains the control flow.

Run the form and check its behaviour

  1. Select Debug > Start Debugging to build and run the application. Resolve build errors before testing the window.
  2. Enter a name and activate Greet. Check that the result contains the expected name.
  3. Try an empty field, spaces only and a longer name. Confirm that validation prevents an empty greeting.
  4. Navigate with Tab and Shift+Tab. Resize the window and check for clipped text or overlapping controls.
  5. If behaviour is wrong, set a breakpoint inside the Click handler. Run again, activate the button and inspect the input and result values while execution is paused.

Add another form when the application needs it

To add a separate window, right-click the project in Solution Explorer, choose Add > Windows Form, and give it a descriptive name. Adding a form does not automatically make it appear when the application starts.

If you need an initial loading window, follow the guide to adding a splash screen in Visual Basic. When the form needs to retain entered information, use the guide to saving data in Visual Basic.

Start by building the greeting form and checking that blank input produces a clear correction message before adding more fields.

Other editions for separate workloads

Other editions for separate workloads; prices in AUD, confirm the total on the listing.

How to check this worked

  1. Open Settings, System, Activation and confirm the edition shown.
  2. Enter the key and run activation; expect a green tick within a minute.
  3. Reboot and re-check activation survives the restart.

FAQ

What else should you know before buying?

What do I need before starting?
Read the requirements and confirm the edition first; the steps assume the machine is compatible.
How do I know the licence is genuine?
Activate on Microsoft's servers and check Settings, System, Activation. A working activation is the test.
Is this a subscription?
Keys sold here are one-time lifetime licences. Microsoft 365 plans are billed by Microsoft instead.
What if the key fails?
Email hi@ms.codes with the order number and a screenshot. Replacement or refund applies within 30 days.

Check who prepared and reviewed this guide

Written by
MS.Codes editorial
Reviewed by
Support team

Last verified: (source review; see test status above).

Recent Post