Title: Namespaces
1Namespaces
- Namespaces are file cabinets for classes, similar
to the concept of folders. - Namespaces can contain classes, other namespaces,
etc. - Namespaces help organize the vast number of
classes into a neat structure. - To make use of a class, we need to reference or
import the namespace that contains the class.
2Namespaces
- System.Diagnostics.Process.Start("www.money.com")
Namespace
Class
If System.Diagnostics has already been referenced
in our project, we can omit it in our code, such
that Process.Start("www.money.com")
3Learning Objectives
- Form basic concept
- Structure of form
- Instantiation of forms
- Form events load and activated
- Show vs. ShowDialog method
- Messagebox an example of dialog boxes
4Structure of Form
Maximize button
Minimize button
Text property
Control box
Close button
Title bar
MinimizeButton, MaximizeButton, ControlBox can be
set to false for removal from the form.
Removal of controlbox removes 3 other boxes as
well.
5WindowState Property Normal, Maximized,
Minimized Opacity Property specify form
transparency
6Form Basic Concept
System.Windows.Forms.Form
IS-A-Kind of relationship
Form1
Instance-of relationship
Form1copy1
Form1copy2
7Form Instantiation
The system automatically creates an instance of
it at runtime.
Form1
An instance of Form1 is created by the system
8Form Show Hide
From form1
Private Sub Button1_Click() Handles
Button1.Click form2.Show()
Me.Hide() me refers to form1 End Sub
From form2
Private Sub Button1_Click() Handles
Button1.Click Me.Close()
form1.Show() End Sub
9The Show Method
- makes a form visible
- triggers two events
- - the form load event (load the form into
primary - storage), only if form is not yet in
memory - - the activated event (makes the form
active) - e.g.,
- form2.show modeless style
10The Hide method
- makes a form invisible without unloading it
- e.g.,
- summaryForm.hide
11Form Events
- the form load event
- the form activated event
- these events can be used to
- initialize variable values or control
- properties
12start-up
form1 loaded form1 activated
form1
form1.hide deactivated even without hiding
it form2.show
form1 deactivated form2 loaded form2 activated
form 2
form2.close form1.show
form1 loaded once, activated twice
form2 closed form1 activated
form 1
13Form Events
- Load
- Activated
- Paint form redrawn, resized, moved
- Deactivate form no longer the active form
- FormClosing form about to close
- FormClosed after form is closed
14Choose the Start-up Form
15The ShowDialog Method
- e.g.,
- form1.ShowDialog
- makes form1 a dialog box (modal form), which has
to be responded to by the user. Other forms
become un-accessible before form1closes.
16Dialog Box Example
Form1
Form2
A dialog box
17DialogResult enumeration
- DialogResult is a property of form, public in
scope. - Values of DialogResult
- DiaglogResult.Abort
- DiaglogResult.Cancel
- DiaglogResult.No
- DiaglogResult.Yes
- DiaglogResult.OK
- DiaglogResult.Retry
18 On Form1 Private Sub Button1_Click() Handles
Button1.Click form2.ShowDialog()
program suspended waiting for user
response to form2 If form2.DialogResult
DialogResult.OK Then Label1.Text
"OK" Else Label1.Text
"Cancel" End If End Sub
19Form2
Private Sub OK_Click() Handles Button1.Click
DialogResult DialogResult.OK End
Sub Private Sub Cancel_Click() Handles
Button2.Click DialogResult
DialogResult.Cancel End Sub
Assigning value to DialogResult closes the dialog
box.
20Program flow show vs. showdialog
form2.show Line 3 Line 4
form2.showdialog Line 3 Line 4
Line 3, Line 4 continue to execute after
form.Show
Line 3, Line 4 suspended until form2 is closed.
21Getting results from MessageBoxes
- MessageBox.Show() show is function that
returns a - value of DialogResult type, i. e., one of the
following - DialogResult.Abort
- DialogResult.Cancel
- DialogResult.No
- DialogResult.Yes
- DialogResult.OK
- DialogResult.Retry
- MessageBox.Show behaves like ShowDialog method
22Getting results from MessageBoxes
23Private Sub Button1_Click() Handles
Button1.Click Dim dResult As
DialogResult dResult
MessageBox.Show("Would you marry me?", "Please
let me know", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) If dResult
DialogResult.Yes Then Label1.Text
"She said Yes!" Else
Label1.Text "I am heart-broken. She said No!"
End If End Sub
24(No Transcript)
25Private Sub Button2_Click() Handles
Button2.Click Select Case
MessageBox.Show("Yes or NO", "Make a selection",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case DialogResult.Yes
Label1.Text "User selects yes!"
Case DialogResult.No Label1.Text
"User selects no!" End Select End
Sub
26Coordinate systems of VB
- units of measurement pixels (monitor
resolution) - left (the distance between left edge of a
control from its - container)
- top (the distance between top edge of a control
from its - container)
- Forms and groupboxes are containers.
27top
left
top
left
This is a groupbox.
28Form's Screen Location
- StartPosition Property
- CenterScreen
- WindowsDefaultLocation
-
- Write code to set position
- e.g.,
- me.left 150 unit in pixels
- me.top 200
29Screen
top
left
Form