0% found this document useful (0 votes)
6 views42 pages

Forms

The document provides an overview of forms in Visual Basic, detailing their role as containers for user interface controls and the different states and properties associated with them. It explains the loading, unloading, showing, and hiding of forms, as well as the events that occur during a form's lifecycle. Additionally, it covers drag-and-drop operations, including the properties and events related to this functionality.

Uploaded by

geetikaj1408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views42 pages

Forms

The document provides an overview of forms in Visual Basic, detailing their role as containers for user interface controls and the different states and properties associated with them. It explains the loading, unloading, showing, and hiding of forms, as well as the events that occur during a form's lifecycle. Additionally, it covers drag-and-drop operations, including the properties and events related to this functionality.

Uploaded by

geetikaj1408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 42

Forms

Introduction to form

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.
Visual Studio creates a default form when
create a Windows Forms Application.
The most important visible object,without it no
control can be displayed.
In the standard project the form Designer
creates and modifies visual forms.
Form

The designer
can add any
number of forms
to the project of
his application
by pressing: add
from project
menu.
Form

Two modes: Design mode and Running mode.



Appearance of Forms

Every form will have title bar on which the


form's caption is displayed
Buttons to close, maximize and minimize
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.
Appearance of Forms
Control Menu

It’s contains the following commands :


Restore : A maximized Form to the size it was
before it was maximized, available only if the
Form has been maximized.
Move : User moves the Form around with the
mouse.
Size : Lets the user resizes the control with the
mouse
Minimize: Minimizes the Form.
Maximize : Maximizes the Form.
Form properties

Properties list has a predefined value (numeric


or string)
Various important properties:
 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.
 CancelButton:The button that's automatically
activated when you hit the Esc key. Usually, the
Cancel button on a form is set as CancelButton
Form properties

 BackColor : Sets the form background color.


 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.
Start-Up Form

• A special object that can


be automatically loaded
when the program is run
• This object is referred to as
the Startup object
• When a form is specified
as the Startup object, that
form automatically loads
into memory when the
application starts
Start-Up Form

• By default, Visual basic suggests the names of


the first form is Form1
• Start-Up object control which form is initially
loaded
• The Start-up object can now vary depending on
the type of project that is creating
• Like Form1 and Sub Main procedure
States of Form

• Not Loaded : Form lives on a disk file and


doesn’t take up any resources
• Loaded but not shown : Form is loaded into
memory, takes up the required resources, and is
ready to be displayed
• Loaded and shown : Form is shown, and the
user can interact with it.
Loading and Unloading Forms

• Loading Forms :
Use the Load statement to load a form into
memory
The Load statement will take only one argument
-The name of the object to be loaded
Syntax : Load formName
Load Form1
Load Statement doesn’t show the Form
Loading and Unloading Forms

• Unloading Forms :
 To unload forms, use Unload statements
Syntax : Unload formName
Form is loaded, it takes required resources, so
should always unload a form that’s no longer
needed
Unload statement accepts a valid object name
Load and Unload are used to control the
memory status of a form
Showing and Hiding Forms

• Loading and Unloading only bring the form into


memory or remove the form from memory
• The Show and Hide methods affect the form's
visibility
• Show statement causes the form to appear
• Hide will make the form invisible, but allows it to
remain in memory
• Form is to be directly shown to the user, only
the Show method is required. The loading of the
Form object will take place automatically
Showing Forms
• To show a form, use Show Method
• If the form isn’t loaded, show method load it and
display it
• Syntax: formName.Show mode
Form1.show
• Optional argument mode : modal or modeless
• 0-Modeless(default) 1-Modal
• Modeless : allow to switch to any other form of
application
Showing Forms

• Modal : Takes total control of application and


won’t let the application proceed unless the form
is closed
• Example: InputBox() function
• Two reasons for loading forms separately:
1.Some forms don’t need to be displayed
2.Speed up the display of a form by loading it a
head of time
Hiding Forms

• Forms can be hidden instead of being unloaded


• If a form is hidden without being unloaded and
destroyed
• then the values of controls will remain as the
user entered them
• To hide a form, use the Form’s Hide method
• Syntax: Form.hide
• To hide a Form from within its own code, use
Me.Hide
Initialize, Load, and Activate
Events
• These events fire toward the beginning of the
life of a form
• Timing of these events can differ
• Generally, of the three "beginning" events:
1.The Initialize event fires least often
2.The Activate event fires most often
3.The Load event fires less often than the Activate
event and more often than the Initialize event
The Initialize Event

• The Initialize event fires when an instance of the


form is created in the application.
• Initialize fires just before Load
• The very first time the form is loaded into
memory during the application's current session
• It may or may not fire before subsequent Loads
of the form
• If the form was previously loaded and then
unloaded, , the Initialize event will not fire the
next time the form is loaded
The Initialize Event

• The form was previously unloaded and then


• Set formname = Nothing '( Terminate event)
The Initialize event will fire the next time the
form loads.
• Forms have an Initialize event because
Microsoft wants us to see forms as
programmable, customizable object classes
• VB classes always sport built-in Initialize and
Terminate events.
The Load Event

• The Load event fires when the form loads into


memory
• This event's procedure for programmers to
insert code that sets form-level Private variables
(not associated with custom properties)
• And performs other start-up processes.
The Load event procedure

• The Load event procedure fires before the form


is established visually
• And before the data connections of any data
controls that it contains have been established
• Get runtime errors if you place code in
form_Load
• That tries to use data connections belonging to
the form's own Data Controls
Load Event

• Form Load Application handling slow-loading


form
• Can’t shorten the load time, but improve the
subjective delay
Activate, Deactivate events
• The Activate event occurs when an object
becomes the active window
• The Deactivate event occurs when an object is
no longer the active window.
Syntax
Private Subobject_Activate( )
Private Subobject_Deactivate( )
• An object can become active by using the
Show method in code.
Activate, Deactivate events

• Activate event can occur only when an object is


visible
• A User Form loaded with Load isn't visible
unless you use the Show method
• Deactivate event doesn't occur when unloading
an object.
Activate events

• More than one form is displayed, user switch


from one to other with the mouse or by pressing
Alt+Tab
• Each time a Form activated
• The activate event takes place.
• Private Sub Form_Activate()
Form2.Caption = “Form2 Activated”
End Sub
Deactivate events

• When a form is activated, the previously active


form receives the Deactivate event
• Which the application uses to change the form’s
caption:
• Private Sub Form_Decativate()
Form2.Caption = “form2 Inactive”
End Sub
Form Load Event

• By default, VB loads and display the first form of


the project
• Form-Load application handling slow-loading
forms
• Example: FormLoad application has 3 forms
- First one, shown on top of the others
- loading two forms
Load Form2
Load Form3
Load Event Handler
• Private Sub Form_load()
Form1.Show
Form1.Refresh
Form1.Caption = “Loading Form2…”
load Form2
Form 1.Caption =“ Loading Form3…”
Load form3
Form1.Caption = “Form Loader”
Command1.visible = True
Command2.visible = True
Label1.Caption =“Application Loaded”
End Sub
Load Event Handler

• Without Form1.show method, The main form


won’t be displayed before the other two forms
are loaded
• Refresh method : VB to update the display
before executing the commands
• By default, VB refreshes the display only gets a
chance
• For display: Form2.Show
Form3.Show
Drag And Drop Operation
• Drag & Drop : action of clicking on an object and
dragging it to a different location (on screen) as
required.
• The basic sequence involved in drag & drop :
Press & hold down, the button on the mouse or
other pointing device.
"Drag" the object /cursor/ pointing device to the
desired location
"Drop" the object by releasing the button
Drag And Drop

• Example
Drag And Drop: Basic Concept
• A drag-and-drop operation involves a source
object and a target object.
• The source object any Visual Basic control
• And the target object can be any control or the
form itself.
• The operation has three parts:
1.The drag operation begins when the user
presses the mouse button as the pointer is over
a control that has been enabled as a drag-and-
drop source.
Cont…
2. The operation continues as the pointer is
moved, with the left mouse button still down,
over other controls or the form itself.
A control receives notification, via the Drag
Over event, that it is being "dragged over"
And can signal whether the data can be
dropped on it by changing the appearance of
the mouse pointer.
3. The operation ends when the user releases the
mouse button. A control is notified by the Drag
Drop event that it has been dropped on
Properties of Drag & Drop

• Controls have two properties :


• DragMode: Set to vbManual (value = 0, the
default) for manual drag-and-drop,
which requires use of the Drag method to
initiate a drag-and-drop operation.
• DragIcon: Specifies the mouse pointer that is
displayed while the control is being dragged.
The default setting displays an arrow with a
rectangle.
Drag & Drop : Event
• Controls have two events that are related to
drag-and-drop.
• DragOver is used to detect when an object is
dragged over a control
• And DragDrop is used to detect when an object
is dropped on a control
Target_DragOver (source As Control, x As Single,
y As Single, State As Integer)
Target_DragDrop(Source as Control,X as
Single,Y as single)
Cont..
• Target identifies the target object (the one being
dragged over or dropped on)
• Source identifies the source control (where the
drag-drop operation began)
• X and y gives the horizontal and vertical position
of the mouse pointer with respect to object
• These values are always expressed according
to the object's coordinate system
Cont..
• State specifies the relationship between the
mouse pointer and the target, as follows:
• A value of 0 indicates the pointer just entered
the target.
• A value of 1 indicates the pointer is leaving the
target.
• A value of 2 indicates that the pointer is moving
within the target
Cont..
• When a source control is dragged and dropped,
here's what happens:
When the mouse pointer leaves the source
control, the parent form receives a single Drag
Over event with the State argument equal to 0.
As the pointer moves over the form, the form
receives multiple Drag over events with the
State argument equal to 2.
Drag & Drop
When the source is dragged over another
control on the form, the form receives a Drag
Over event with the State argument equal to 1
And the control over which the source was just
dragged receives a DragOver event with the
State argument equal to 0
When the control is dropped, the object it is
currently over receives a DragDrop event.

You might also like