Unit 2- Visual Programming Material
Unit 2- Visual Programming Material
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.
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
• Let's start with creating a Window Forms Application by following the following
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
The AutoScrollPosition is the number of pixels by which the two scroll bars were displaced from their
6 AutoScrollPosition
initial locations.
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
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.
Determines whether a Help button should be displayed in the caption box of the
12 HelpButton
form.
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.
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
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.
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
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
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
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.
15 MouseDown Occurs when the mouse pointer is over the form and a mouse button is pressed.
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
21 MouseWheel Occurs when the mouse wheel moves while the control has focus.
24 Scroll Occurs when the user or code scrolls through the client area.
• 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.
• 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
• 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
• 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
Minimizing a Form:
• To minimize (or "iconify") a form, you can use the WindowState property of the
Me.WindowState = FormWindowState.Minimized
• You can place this code in an event handler, such as a button click event, to
Maximizing a Form:
• To maximize a form, you can again use the WindowState property, but this time
set it to FormWindowState.Maximized.
Me.WindowState = FormWindowState.Maximized
• You can place this code in an event handler, such as a button click event, to
Restoring a Form:
• To return a form to its normal state after it has been minimized or maximized, set
• 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
Form Design
Minimizing and Maximizing a form at Run time
Code:
• You can set the initial position in the Properties window while designing the form. Look for
• 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
• To set the initial position programmatically at run-time, you can use the form's
• 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
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.
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.
End Sub
Form 2
Public Class Form2
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).
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.
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.
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).
5. Be Consistent:
Maintain consistency in your naming conventions throughout your project to make your
codebase more readable and predictable.
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
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:
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.
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.
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.
6 BorderStyle The BorderStyle property determines the style of the form's border and
the appearance of the form −
8 Enabled If True, allows the form to respond to mouse and keyboard events; if
False, disables form.
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.
MouseDown Occurs when the mouse pointer is over the form and a mouse
13 button is pressed.
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.
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.
‘Create 2 buttons
Dim button1 As New Button()
Dim button2 As New Button()
' 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 start position of the form to the center of the screen.
Me.StartPosition = FormStartPosition.CenterScreen
End Sub
End Class