0% found this document useful (0 votes)
4 views

Unit 2- Visual Programming Material

Uploaded by

maheskrishna5267
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit 2- Visual Programming Material

Uploaded by

maheskrishna5267
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Unit II

Introduction to the windows forms


Windows Forms

• Windows Forms is a GUI (Graphical User Interface) framework provided by


Microsoft for creating desktop applications on the Windows operating system.
• It's part of the .NET Framework and is often used with languages like Visual
Basic (VB.NET) and C#.
• Visual Basic Form is the container that allows developers to create visually
appealing and interactive desktop applications with ease.
• Windows Forms in VB.NET is a powerful tool for creating Windows desktop
applications. It offers a user-friendly design experience, event-driven
programming, and the flexibility to build applications ranging from simple
utilities to complex business software
Key Concepts of Windows Forms

1. Graphical User Interface (GUI):


• GUI is the visual part of an application that users interact with.
• Windows Forms provides tools and controls (e.g., buttons, textboxes, labels) to
create the GUI for your application.

2. Event-Driven Programming:
• Windows Forms applications respond to events triggered by user actions (e.g.,
button click, mouse movement).
• Developers write code to handle events, defining how the application should
respond.
Key Concepts of Windows Forms

3. Visual Design:
• Windows Forms applications are designed visually using a drag-and-drop
approach.
• You place controls on a form, set their properties, and define the layout using
an Integrated Development Environment (IDE) like Visual Studio.
4. Controls
• Controls are the building blocks of a Windows Form.
• Common controls include buttons, textboxes, labels, checkboxes, and more.
• Each control has properties (e.g., color, size) and can trigger events (e.g.,
Click, TextChanged).
Key Concepts of Windows Forms

5. Form Lifecycle:
• Windows Forms follow a lifecycle: creation, loading, showing, and closing.
• You can customize behavior at each stage, e.g., initializing data when the form
loads.

6. Data Binding:
• Windows Forms supports data binding, connecting controls to data sources
like databases or collections.
• This simplifies tasks such as displaying and updating data in UI elements.
Key Concepts of Windows Forms

7. Rich Controls:
• Beyond basic controls, Windows Forms offers advanced ones like datagrids,
treeviews, and tab controls.
• These enable you to create complex and feature-rich interfaces.

8. Localization and Globalization:


• Windows Forms facilitates localization and globalization.
• You can create applications that adapt to different languages and cultures.
Key Concepts of Windows Forms

9. Deployment:
• Windows Forms apps can be easily deployed on Windows computers.
• You can create standalone executables or use technologies like ClickOnce for
deployment.

10. Extensibility:
• You can extend Windows Forms by creating custom controls or integrating
with other .NET technologies like ASP.NET, WPF, and ADO.NET.
Advantages

• Familiar and user-friendly interface for Windows users.

• Quick and efficient development with visual design.

• Excellent for creating desktop applications with rich functionality.


Getting Started:

• Let's start with creating a Window Forms Application by following the following

steps in Microsoft Visual Studio

File → New Project → Windows Forms Applications


Form Properties

S.N Properties Description

The button that's automatically activated when you press Enter, no matter which
1 AcceptButton control has the focus at the time. Usually the OK button on a form is set as
AcceptButton for a form.

The button that's automatically activated when you hit the Esc key.
2 CancelButton
Usually, the Cancel button on a form is set as CancelButton for a form.

This Boolean property determines whether the controls you place on the form are
3 AutoScale automatically scaled to the height of the current font. The default value of this property
is True. This is a property of the form, but it affects the controls on the form.

This Boolean property indicates whether scroll bars will be automatically attached to
4 AutoScroll
the form if it is resized to a point that not all its controls are visible.

This property lets you specify the minimum size of the form, before the scroll bars are
5 AutoScrollMinSize
attached.
Form Properties

S.N Properties Description

The AutoScrollPosition is the number of pixels by which the two scroll bars were displaced from their
6 AutoScrollPosition
initial locations.

7 BackColor Sets the form background color.

The BorderStyle property determines the style of the form's border and the appearance of the form −
None − Borderless window that can't be resized.
Sizable − This is default value and will be used for resizable window that's used for displaying regular
forms.
Fixed3D − Window with a visible border, "raised" relative to the main area. In this case, windows
can't be resized.
8 BorderStyle
FixedDialog − A fixed window, used to create dialog boxes.
FixedSingle − A fixed window with a single line border.
FixedToolWindow − A fixed window with a Close button only. It looks like the toolbar displayed by
the drawing and imaging applications.
SizableToolWindow − Same as the FixedToolWindow but resizable. In addition, its caption font is
smaller than the usual.
Form Properties

S.N Properties Description

By default, this property is True and you can set it to False to hide the icon and
9 ControlBox
disable the Control menu.

If True, allows the form to respond to mouse and keyboard events; if False,
10 Enabled
disables form.

11 Font This property specify font type, style, size

Determines whether a Help button should be displayed in the caption box of the
12 HelpButton
form.

13 Height This is the height of the Form in pixels.


Form Properties

S.N Properties Description

By default, this property is True and you can set it to False to hide the Minimize
14 MinimizeBox
button on the title bar.

By default, this property is True and you can set it to False to hide the Maximize
15 MaximizeBox
button on the title bar.

16 MinimumSize This specifies the minimum height and width of the window you can minimize.

17 MaximumSize This specifies the maximum height and width of the window you maximize.

18 Name This is the actual name of the form.


Form Properties

S.N Properties Description

This property determines the initial position of the form when it's first displayed.
It will have any of the following values −
•CenterParent − The form is centered in the area of its parent form.
•CenterScreen − The form is centered on the monitor.
9 StartPosition •Manual − The location and size of the form will determine its starting position.
•WindowsDefaultBounds − The form is positioned at the default location and
size determined by Windows.
•WindowsDefaultLocation − The form is positioned at the Windows default
location and has the dimensions you've set at design time.

20 Text The text, which will appear at the title bar of the form.
Form Properties

S.N Properties Description

These two properties set or return the coordinates of the form's top-left corner in
21 Top, Left
pixels.

This property is a True/False value that lets you specify whether the form will
22 TopMost
remain on top of all other forms in your application. Its default property is False.

23 Width This is the width of the form in pixel.


Form Methods
S.N Method Name & Description

Activate
1
Activates the form and gives it focus.

ActivateMdiChild
2
Activates the MDI child of a form.

AddOwnedForm
3
Adds an owned form to this form.

BringToFront
4
Brings the control to the front of the z-order.

CenterToParent
5
Centers the position of the form within the bounds of the parent form.

CenterToScreen
6
Centers the form on the current screen.
Form Methods

S.N Method Name & Description

ScaleControl
13
Scales the location, size, padding, and margin of a control.

ScaleCore
14
Performs scaling of the form.

Select
15
Activates the control.

SendToBack
16
Sends the control to the back of the z-order.

SetAutoScrollMargin
17
Sets the size of the auto-scroll margins.

SetDesktopBounds
18
Sets the bounds of the form in desktop coordinates.
Form Methods

S.N Method Name & Description

SetDesktopLocation
19
Sets the location of the form in desktop coordinates.

SetDisplayRectLocation
20
Positions the display window to the specified value.

Show
21
Displays the control to the user.

ShowDialog
22
Shows the form as a modal dialog box.
Form Events
S.N Event Description

1 Activated Occurs when the form is activated in code or by the user.

2 Click Occurs when the form is clicked.

3 Closed Occurs before the form is closed.

4 Closing Occurs when the form is closing.

5 DoubleClick Occurs when the form control is double-clicked.

6 DragDrop Occurs when a drag-and-drop operation is completed.

7 Enter Occurs when the form is entered.

8 GotFocus Occurs when the form control receives focus.

9 HelpButtonClicked Occurs when the Help button is clicked.

10 KeyDown Occurs when a key is pressed while the form has focus.
Form Events
S.N Event Description

11 KeyPress Occurs when a key is pressed while the form has focus.

12 KeyUp Occurs when a key is released while the form has focus.

13 Load Occurs before a form is displayed for the first time.

14 LostFocus Occurs when the form loses focus.

15 MouseDown Occurs when the mouse pointer is over the form and a mouse button is pressed.

16 MouseEnter Occurs when the mouse pointer enters the form.

17 MouseHover Occurs when the mouse pointer rests on the form.

18 MouseLeave Occurs when the mouse pointer leaves the form.

19 MouseMove Occurs when the mouse pointer is moved over the form.

20 MouseUp Occurs when the mouse pointer is over the form and a mouse button is released.
Form Events

S.N Event Description

21 MouseWheel Occurs when the mouse wheel moves while the control has focus.

22 Move Occurs when the form is moved.

23 Resize Occurs when the control is resized.

24 Scroll Occurs when the user or code scrolls through the client area.

25 Shown Occurs whenever the form is first displayed.

26 VisibleChanged Occurs when the Visible property value changes.


Setting the title Bar Text, Minimizing and
Maximizing a form
Setting the title Bar Text

• Setting the title bar text of a Windows Forms application is a common task when
you want to provide a meaningful title for your application's main window.
• The title bar is the topmost part of the application window and typically
displays the application name or a description.

This is title bar

• You can set the title property at design-time as well as run time in the Visual
Studio IDE
Setting the title Bar Text at the Design Time

• In Windows Forms, you can set the title bar text of a form by modifying its

“Text” property.

• This property represents the text that appears in the title bar

• Follow the following steps to set title bar at design time

• Select window forms (click on window forms) – Go to property window – Select

“text” property – Change the name of the text as you want


Setting the title Bar Text at the Design Time

Default Text name in the form Default Title bar


property
Setting the title Bar Text at the Design Time

Changing Text name at the design


Changed Title bar
time
Setting the title Bar Text at Run time

• You can also set the title bar text dynamically at run-time using VB.NET code.

• To do this, access the form's Text property and assign it a string value.
Setting the title Bar Text at Run time

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


Me.Text = "WELCOME TO III SEM BCA E "
End Sub
End Class
Minimizing and Maximizing a form

• In Visual Basic (VB) for Windows Forms applications, you can easily implement the
functionality to minimize and maximize a form using built-in methods and properties
Minimizing and Maximizing a form at design time

• WindowState property is used for Minimizing and Maximizing a form


• There are three option are ther in the WindowState property such as Normal or
Minimized or Maximized
• Follow the steps to set the Minimizing and Maximizing a form property
• Select window forms (click on window forms) – Go to property window – Select
“WindowState” property – Select the option Normal or Minimized or Maximized

• WindowState: Minimized is used to minimize a form


• WindowState: Maximized is used to maximize a form
• WindowState: Normal is used to return a form to its normal state after it has been
minimized or maximized
Minimizing and Maximizing a form at the Design Time

Selecting the option from WindowState: Normal


WindowState
Minimizing and Maximizing a form at Run time

Minimizing a Form:

• To minimize (or "iconify") a form, you can use the WindowState property of the

form. You set this property to FormWindowState.Minimized.

• An example of how to minimize a form:

Me.WindowState = FormWindowState.Minimized

• You can place this code in an event handler, such as a button click event, to

minimize the form when the button is clicked.


Minimizing and Maximizing a form at Run time

Maximizing a Form:

• To maximize a form, you can again use the WindowState property, but this time

set it to FormWindowState.Maximized.

• An example of how to maximize a form:

Me.WindowState = FormWindowState.Maximized

• You can place this code in an event handler, such as a button click event, to

maximize the form when the button is clicked.


Minimizing and Maximizing a form at Run time

Restoring a Form:

• To return a form to its normal state after it has been minimized or maximized, set

the WindowState property to FormWindowState.Normal.

• For example:

Me.WindowState = FormWindowState.Normal

• You can place this code in an event handler, such as a button click event, to To

return a form to its normal state after it has been minimized or maximized when

the button is clicked.


Minimizing and Maximizing a form at Run time

Form Design
Minimizing and Maximizing a form at Run time
Code:

Public Class Form1

Private Sub MinimizeButton_Click(sender As Object, e As EventArgs) Handles MinimizeButton.Click


Me.WindowState = FormWindowState.Minimized
End Sub

Private Sub MaximizeButton_Click(sender As Object, e As EventArgs) Handles MaximizeButton.Click


Me.WindowState = FormWindowState.Maximized
End Sub

Private Sub RestoreButton_Click(sender As Object, e As EventArgs) Handles RestoreButton.Click


Me.WindowState = FormWindowState.Normal
End Sub
End Class
Setting initial position of a form
Setting initial position of a form

• The initial position of a Windows Forms application refers to where the


application's main form (the primary window) appears on the user's screen when
the application is launched.
• By default, forms are centered on the screen, ensuring that they are visible and
easily accessible to users.
Setting initial position of a form at design time

• You can set the initial position in the Properties window while designing the form. Look for

the "StartPosition" property, which has several options:

• WindowsDefaultBounds: The form will appear at its last closed position.

• CenterScreen: The form will be centered on the screen.

• CenterParent: The form will be centered within its parent form (useful for dialog boxes).

• Manual: You can specify a custom position using the Location property.
Setting initial position of a form at design time

Selecting the option StartPosition StartPosition : WindowsDefaultLocation


Setting initial position of a form at Run time

• To set the initial position programmatically at run-time, you can use the form's

StartPosition property in your VB.NET code.

• Me.StartPosition = FormStartPosition.CenterScreen ' Center the form on the screen.

• You can also manually set the form's location using the Location property

• Me.Location = New Point(100, 100) ' Set the form's top-left corner position to (100, 100).
Setting initial position of a form at Run time

Public Class Form1


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.StartPosition = FormStartPosition.CenterParent ' Center the form on the screen.
End Sub
End Class
WORKING WITH MULTIPLE FORMS
Working with multiple forms in VB.NET is a common requirement when building Windows Forms
applications. Multiple forms allow you to create complex user interfaces and manage different aspects
of your application's functionality.

Creating Multiple Forms:


Add New Forms:

In your VB.NET project, you can add new forms by right-clicking on your project in the Solution
Explorer, selecting "Add" > "Windows Form," and giving it a name.

Design Your Forms:

Open each form and design its user interface using the Visual Studio designer. You can add controls,
set properties, and create the desired layout for each form.

Example - Multi Forms


Form 1
Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Form2.Show()
Me.Hide()

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


Form3.Show()
Me.Hide()
End Sub
End Class

Form 2
Public Class Form2

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load


End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Form1.Show()
Me.Hide()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


Form3.Show()
Me.Hide()
End Sub
End Class
Form 3
Public Class Form3

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Form2.Show()
Me.Hide()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


End
End Sub
End Class

Managing Form Lifecycles


Show and Hide Forms:

Use the Show() method to display a form, and the Hide() method to hide it. Forms that are hidden
are not closed; they can be shown again.

Close Forms:

Use the Close() method to close a form when it's no longer needed. Closed forms can be disposed of
and removed from memory.

Working with multiple forms in VB.NET allows you to create rich, multi-screen applications with
separate UI and functionality. Effective communication between forms and proper management of
form lifecycles are key aspects of building successful multi-form applications.
CREATING ADDING CONTROLS TO A FORM
Adding controls to a form in VB.NET is a fundamental step in designing the user interface of
your Windows Forms application. You can add various controls like buttons, textboxes, labels,
and more to create a visually interactive and functional form. Here's how to add controls to a
form:
Open or Create a Windows Form:
 Open your VB.NET project in Visual Studio.
 Create a new Windows Forms application or open an existing one.
 Open the form you want to add controls to by double-clicking its design file (usually
with a .vb extension).

1. Open the Toolbox:


 In Visual Studio, there's a Toolbox window that contains various controls you
can add to your form. If you don't see the Toolbox, you can open it by going to
"View" > "Toolbox" or pressing Ctrl+Alt+X.
2. Select and Drag Controls:
 In the Toolbox, locate the control you want to add to your form (e.g., a Button
or TextBox).
 Click and drag the control from the Toolbox onto your form. As you drag, you'll
see an outline of the control following your mouse cursor.
3. Position and Resize Controls:
 Drop the control on the form where you want it to appear.
 Use the mouse to adjust the size and position of the control as needed.
 To align controls precisely, you can use the gridlines and snap-to-grid feature.
4. Customize Control Properties:
 With the control selected, you can customize its properties in the Properties
window. For example, you can set the text of a Button or the name of a TextBox.
5. Repeat for Additional Controls:
 Repeat the process to add more controls to the form. You can add as many
controls as necessary to create your desired user interface.

Here's a sample code to add a Button control to a form programmatically:

public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


Dim button As New Button()
button.Text = "Click Me"
button.Location = New Point(100, 100) ' Set the position (X, Y) of the button on the form.
Me.Controls.Add(button) ' Add the button to the form's Controls collection.

End Sub
End Class
SETTING THE TAB ORDER
Setting the tab order for controls in a VB.NET Windows Forms application is essential for
defining the sequence in which users can navigate through controls on the form using the Tab
key. This ensures a logical and user-friendly flow within your application. Here's how to set
the tab order for controls:
1. Design Your Form:
 Open your VB.NET project in Visual Studio.
 Open the form you want to work on in the Visual Studio designer.

2. View Tab Order:


 In the Visual Studio designer, you can view the current tab order of controls by going
to "View" > "Tab Order" from the menu or by clicking the "Tab Order" button on the
toolbar. This mode allows you to see the numbers assigned to controls indicating
their tab order.

3. Manually Set Tab Order:


 To manually set the tab order, click on each control in the order you want users to
navigate.
 As you click on a control, you'll see a number indicating its current tab order. You
can change this number by clicking on another control. The controls will be
numbered sequentially.

4. Automatically Set Tab Order:

If the automatic order doesn't match your desired order, you can manually set it by
clicking on each control, as mentioned in step 3.
5. Disable Controls from Tab Order:
 If you have controls that you don't want to include in the tab order (e.g., labels or
non-input controls), you can disable them by setting their TabStop property to False.
Disabled controls won't participate in the tab order.

6. Verify Tab Order:


 After setting the tab order, make sure to test your form's navigation using the Tab
key to ensure that it follows the sequence you've defined. This helps you confirm
that the tab order provides a logical flow for users.

7. Changing Tab Order Programmatically:


 You can also change the tab order programmatically in your VB.NET code using the
TabIndex property of controls.
For example:
TextBox1.TabIndex = 1
TextBox2.TabIndex = 2
Button1.TabIndex = 3
NAMING CONTROLS IN VB.NET
In VB.NET, naming controls is important for identifying and accessing them in your code.
Properly naming controls makes your code more readable and maintainable. Here's how to
name controls in VB.NET:
1. Using the Properties Window:
 Open your Windows Forms project in Visual Studio.
 Open the form containing the control you want to name in the Visual Studio designer.
 Select the control you want to name by clicking on it.
 In the Properties window (usually located below the Solution Explorer), locate the "Name"
property.
 Enter the desired name for the control. Names should be meaningful and adhere to naming
conventions (e.g., TextBox1, ButtonOK, LabelName).

2. Programmatically Naming Controls:


 You can also name controls programmatically in your VB.NET code. To do this, set the
Name property of the control in your code.
For example:
vb.netCopy code

 Note that when you create controls programmatically, you should set their properties,
including the Name, before adding them to the form.

3. Naming Conventions:
 It's a good practice to follow naming conventions for controls. Some common conventions
include:
 Prefixes: You can use prefixes to indicate the control type, such as btn for buttons, txt for
textboxes, and lbl for labels.
 Camel Case: Use camelCase for multi-word names (e.g., btnSubmit, txtLastName).
 Meaningful Names: Choose names that reflect the purpose or content of the control (e.g.,
btnSave, txtEmail).

4. Avoid Reserved Keywords:


 Ensure that your control names do not conflict with VB.NET reserved keywords or
existing variable names in your code.

5. Be Consistent:
 Maintain consistency in your naming conventions throughout your project to make your
codebase more readable and predictable.

6. Use Descriptive Names:


 Choose names that provide context about the control's purpose. This makes it easier for
you and other developers to understand the code.

7. Accessibility:
 If you plan to access controls from other forms or classes, make sure the control's
accessibility is appropriate. For example, use Public if you need to access a control from
outside the form.

8. Avoid Ambiguity:
 Ensure that control names are unique within the form to avoid naming conflicts.
CREATING VISUAL STUDIO APPLICATIONS : Setting
Properties at Design Time

1. Start Visual Studio.


2. Click the new project button on the toolbar OR click File > New project. A new project
dialog box will appear.

3. Click visual basic in the Installed Templates box on the left side of the window.
4. Click Windows under the Visual Basic option. A list of templates will appear in the templates
pane in the center section of the window.
5. Select Windows forms application in the center section of the window.
6. Type form1 in the Name text box and click OK. A new project will be created and a blank
form will appear.
7. Double click button control in the toolbox. A button will appear on the top left side of the
form with button1 text on it as follows:
8. Double click label control in the toolbox. A label will appear on the form with label1 text
on it as follows:

Setting the Properties:


1. Click the title bar of the form. The form will be selected and its properties will appear in the
properties window.
2. Type first project in text property and press enter.
3. Click button1. The properties window will display its properties.
4. Type btnClick in the name property of the button and press enter. The name property is the
identification of an object.

5. Type click me in the text property of the button and press enter. The text “Click Me” will
appear on the button.
6. Type lblMsgShow in the name property of the label and press enter.

Attaching the code:


1. Double click the button. The code window will appear with the cursor blinking between
two lines. Then Type the following code between the two lines.
2. Click the start icon on the toolbar OR press F5 to run the project.
3. Click the button. The message “Welcome to our First Visual Basic Project” will appear on
the label.

4. Click the stop button on the toolbar to stop the project.


INTRODUCTION TO THE WINDOWS FORMS : Setting
Properties at run time
Let's start with creating a Window Forms Application by following the following steps in Microsoft
Visual Studio - File → New Project → Windows Forms Applications

Finally, select OK, Microsoft Visual Studio creates your project and displays following window Form
with a name Form1.

Visual Basic Form is the container for all the controls that make up the user interface. Every window
you see in a running visual basic application is a form, thus the terms form and window describe the
same entity. Visual Studio creates a default form for you when you create a Windows Forms
Application.
Every form will have title bar on which the form's caption is displayed and there will be buttons to
close, maximize and minimize the form shown below −

If you click the icon on the top left corner, it opens the control menu, which contains the various
commands to control the form like to move control from one place to another place, to maximize or
minimize the form or to close the form.

Form Properties

Following table lists down various important properties related to a form. These properties can be set
or read during application execution. You can refer to Microsoft documentation for a complete list of
properties associated with a Form control −
S.N Properties Description

1 AcceptButton The button that's automatically activated when you press Enter, no
matter which control has the focus at the time. Usually the OK button
on a form is set as AcceptButton for a form.

2 CancelButton The button that's automatically activated when you hit the Esc key.

Usually, the Cancel button on a form is set as CancelButton for a form.

3 AutoScale Automatic scaling enables a form and its controls, designed on one
machine with a certain display resolution or font, to be displayed
appropriately on another machine with a different display resolution or
font.

4 AutoScroll This Boolean property indicates whether scroll bars will be


automatically attached to the form if it is resized to a point that not all
its controls are visible.

5 BackColor Sets the form background color.

6 BorderStyle The BorderStyle property determines the style of the form's border and
the appearance of the form −

 None − Borderless window that can't be resized.

 Sizable − This is default value and will be used for resizable


window that's used for displaying regular forms.

 Fixed3D − Window with a visible border, "raised" relative to the


main area. In this case, windows can't be resized.

 FixedDialog − A fixed window, used to create dialog boxes.

 FixedSingle − A fixed window with a single line border.

 FixedToolWindow − A fixed window with a Close button only.


It looks like the toolbar displayed by the drawing and imaging
applications.

 SizableToolWindow − Same as the FixedToolWindow but


resizable. In addition, its caption font is smaller than the usual.
7 ControlBox By default, this property is True and you can set it to False to hide the
icon and disable the Control menu.

8 Enabled If True, allows the form to respond to mouse and keyboard events; if
False, disables form.

9 Font This property specify font type, style, size


10
HelpButton Determines whether a Help button should be displayed in the caption
box of the form.
11
Height This is the height of the Form in pixels.
12
MinimizeBox By default, this property is True and you can set it to False to hide the
Minimize button on the title bar.
13
MaximizeBox By default, this property is True and you can set it to False to hide the
Maximize button on the title bar.
14
MinimumSize This specifies the minimum height and width of the window you can
minimize.
15
MaximumSize This specifies the maximum height and width of the window you
maximize.
16
Name This is the actual name of the form.
17
StartPosition This property determines the initial position of the form when it's first
displayed. It will have any of the following values −
 CenterParent − The form is centered in the area of its parent
form.
 CenterScreen − The form is centered on the monitor.
 Manual − The location and size of the form will determine its
starting position.
 WindowsDefaultLocation − The form is positioned at the
Windows default location and isn't resized.
18
Text The text, which will appear at the title bar of the form.
19
TopMost This property is a True/False value that lets you specify whether the
form will remain on top of all other forms in your application. Its default
property is False.
20
Width This is the width of the form in pixel.
Form Events
Following table lists down various important events related to a form. You can refer to Microsoft
documentation for a complete list of events associated with forms control −

Sr.No. Event Description

1 Activated Occurs when the form is activated in code or by the user.

2 Click Occurs when the form is clicked.

3 Closed Occurs before the form is closed.

4 Closing Occurs when the form is closing.

5 DoubleClick Occurs when the form control is double-clicked.

6 DragDrop Occurs when a drag-and-drop operation is completed.

7 HelpButtonClicked Occurs when the Help button is clicked.

9 KeyDown Occurs when a key is pressed while the form has focus.

10 KeyUp Occurs when a key is released while the form has focus.

11 Load Occurs before a form is displayed for the first time.

12 LostFocus Occurs when the form loses focus.

MouseDown Occurs when the mouse pointer is over the form and a mouse
13 button is pressed.

14 MouseEnter Occurs when the mouse pointer enters the form.

15 MouseHover Occurs when the mouse pointer rests on the form.

16 MouseLeave Occurs when the mouse pointer leaves the form.

17 MouseMove Occurs when the mouse pointer is moved over the form.

MouseUp Occurs when the mouse pointer is over the form and a mouse
18 button is released.

MouseWheel Occurs when the mouse wheel moves while the control has
19 focus.

20 Move Occurs when the form is moved.

21 Resize Occurs when the control is resized.


Example
Following is an example, which shows how we create two buttons at the time of form load event and
different properties are being set at the same time.

Because Form1 is being referenced within its own event handler, so it will be written as Me instead of
using its name, but if we access the same form inside any other control's event handler, then it will be
accessed using its name Form1.

Let's double click on the Form and put the follow code in the opened window.

Public Class Form1


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

‘Create 2 buttons
Dim button1 As New Button()
Dim button2 As New Button()

' Set the text of button1 to "OK".


button1.Text = "OK"

' Set the position of the button on the form.


button1.Location = New Point(10, 10)

' Set the text of button2 to "Cancel".


button2.Text = "Cancel"

' Set the position of the button based on the location of button1.
button2.Location = New Point(button1.Left, button1.Height + button1.Top + 10)

' Set the caption bar text of the form.


Me.Text = "tutorialspoint.com"

' Display a help button on the form.


Me.HelpButton = True

' Define the border style of the form to a dialog box.


Me.FormBorderStyle = FormBorderStyle.FixedDialog

' Set the MaximizeBox to false to remove the maximize box.


Me.MaximizeBox = False

' Set the MinimizeBox to false to remove the minimize box.


Me.MinimizeBox = False

' Set the accept button of the form to button1.


Me.AcceptButton = button1

' Set the cancel button of the form to button2.


Me.CancelButton = button2

' Set the start position of the form to the center of the screen.
Me.StartPosition = FormStartPosition.CenterScreen

' Set window width and height


Me.Height = 300
Me.Width = 560

' Add button1 to the form.


Me.Controls.Add(button1)

' Add button2 to the form.


Me.Controls.Add(button2)

End Sub
End Class

You might also like