Você está na página 1de 20

PRACTICE 08

VB.NET
VB.NET’s’s Form
Visual Basic.NET
by Has Bunton

1
Form and Window
ƒ The term form and window describe
the same entity.
ƒ The window is what the user sees when
the application is running.
ƒ A form or Window form is the same
entity at design time

by Has Bunton 2
Properties of the Form Control
ƒ Accept Button/Cancel Button: These
properties specify button to be automatically
activated when user presses Enter or Esc key
ƒ AutoScale: Automatically scale the height of
the control to be placed on the form to fit
the height of text.
ƒ AutoScroll*: Automatically
attaches scroll bars to the
form when it’s resized to
the point that not all its
controls are visible.
ƒ Etc.

by Has Bunton 3
Designing Functional Form
ƒ Data entry form is one of the interfaces
users work with most of the time and they
work mostly with keyboard
ƒ In developing data entry form you must
consider the following:
1. User needs
2. Make prototype and ask users to test-drive it
3. Listen to user’s objections and use them to
refine your application’s user interface
4. Don’t defense your design

by Has Bunton 4
The Contacts Project
ƒ Objective: Demonstrate a few
techniques for designing easy to use
forms
ƒ Design:

by Has Bunton 5
Coding - The Contacts Project (1)
1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
' F10 displays the number of contacts
2. If e.KeyCode = Keys.F10 Then
3. MsgBox("There are a number of contacts“)
e.Handled = True
4. End If
' ALT+ takes you to next contact, ALT- takes you to previous contact
5. If e.KeyCode = Keys.Subtract And e.Modifiers = Keys.Alt Then
6. MsgBox(“Move previous“)
7. End If
8. If e.KeyCode = Keys.Add And e.Modifiers = Keys.Alt Then
9. MsgBox(“Move next“)
10. End If
11. End Sub

by Has Bunton 6
The Form’s Events
ƒ Activate and Deactivate: Happen when
you click on another form or when you
press Alt + Tab
ƒ Resize: Fired every time the user
resizes the form using mouse
ƒ Etc.

by Has Bunton 7
Designing Menus
ƒ Start by double clicking the MainMenu icon
on the toolbox
ƒ The MenuItem represents a menu command,
at any level, with its own
property.
ƒ To create a separator bar,
use a dash (-)
ƒ To Insert an item, right-
click, select Insert New

by Has Bunton 8
MenuItem Object’s Properties
ƒ Check
ƒ DefaultItem
ƒ Enabled
ƒ Visible
ƒ Etc.

ƒ Note: Menu commands can be programmed


just as with controls.

by Has Bunton 9
Manipulating Menus at Runtime
ƒ Dynamic menus change at runtime to display
more or fewer commands.

by Has Bunton 10
Creating Short and Long Menus
ƒ Start a new project and create menu with
the structure as above.
ƒ Write the following code
1. Protected Sub MenuSize_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuSize.Click
2. If MenuSize.Text = ">>" Then
3. MenuSize.Text = "<<"
4. Else
5. MenuSize.Text = ">>"
6. End If

7. mFontAllCaps.Visible = Not mFontAllCaps.Visible


8. mFontBold.Visible = Not mFontBold.Visible
9. mFontItalic.Visible = Not mFontItalic.Visible
10. End Sub

by Has Bunton 11
Creating Context Menu
ƒ To create a context menu, place a
CentextMenu Control on your form. Except
the first command, which is the context
menu’s name, it is identical to the
MainMenu, but it won’t be displayed there
at runtime
ƒ To associate a context menu with a
control, set the control context menu
property to the name of the corresponding
context menu.
by Has Bunton 12
Adding Controls at Runtime*
ƒ To add controls at runtime first
initialize the control, set it
properties, and finally call Add
method od the control collection.
ƒ Example:
1. Dim Bttn as New System.Winforms.Button
2. Bttn.text = “New Button”
3. Bttn.Left = 100
4. Bttn.Top = 60
5. Bttn.Width = 80
6. Me.Controls.add (bttn)

by Has Bunton 13
The Dynamic Form Project
ƒ This project allows user to specify at
runtime the number of data points
they wish to enter, and the number of
textboxes on the form changes.
ƒ The control at the top
of the form is the
NumericUpDown
control

by Has Bunton 14
Coding The Dynamic Form Project (1)

by Has Bunton 15
Coding The Dynamic Form Project (2)

by Has Bunton 16
Coding The Dynamic Form Project (3)

by Has Bunton 17
Reading the Control on the Form (4)

by Has Bunton 18
Creating Event Handlers at Runtime
ƒ Create a subroutine that accepts two
arguments, the sender and e. The
name of argument can be anything
ƒ Enter the code for the event
ƒ Connect it to an event of a specific
control using the following syntax
1. AddHandler Control.Event, New System.EventHandler(AddressOf SubName)

by Has Bunton 19
Event Handlers Added at Runtime

End! by Has Bunton 20

Você também pode gostar