4.3Các điều khiển M ới -
Điều khiển ADO data
-
Điều khiển Coolbar
-
Điều khiển D ata grid
-
Điều khiển Datalist, DataCombo
-
Điều khiển DataRepeater
-
Điều khiển DataTimePicker
-
Điều khiển Flat Scollbar
-
Điều khiển Hierarchical FlexGrid
-
Điều khiển ImageComBo
-
Điều khiển Month View
5Nhập môn lập trình
Các điều khiển trên biểu mẫu chỉ là một phần nhỏ của quá trình lập trình phát triển ứng dụng, nhằm tạo ra giao diện cho ứng dụng. Sau đó, bạn cần viết chương trình để ứng dụng hoạt động. Do đó, chương này sẽ đi sâu vào phần công việc chính của Visual Basic, viết chương trình.
Visual Basic là ngôn ngữ lập trình dựa trên đối tượng. Nếu bạn là người mới học, chương này sẽ giới thiệu các khối thiết kế cơ bản để xây dựng chương trình. Khi đã hiểu được các khái niệm cơ bản, bạn có thể tạo ra các ứng dụng rất mạnh bằng Visual Basic.
5.1Chuẩn lập trình (Coding convention) 5.1.1Coding conventions
Object Naming Conventions
Object name has 2 parts: prefix and description.
The prefix that makes it easy to identify the type of object, the description mentions name of objects.
- Conventions of description part are:
+ In English.
+ Can contain many words, each word is contiguous to others (No hyphen).
+ No acronym except listed in table Acronyms (see 4. Acronym).
+ Capitalize the first letter of each word.
(Note: These conventions will be applied to all of name types mentioned after in this document)
- Prefix conventions for some of the objects supported by Visual Basic are listed below (Sorted by control name):
Control type
|
Prefix
|
Example
|
3D Panel
|
Pnl
|
pnlGroup
|
ADO Data
|
Ado
|
adoBiblio
|
Animated button
|
Ani
|
aniMailBox
|
Check box
|
Chk
|
chkReadOnly
|
Combo box, drop-down list box
|
Cbo
|
cboEnglish
|
Command button
|
Cmd
|
cmdExit
|
Common dialog
|
dlg
|
dlgFileOpen
|
Communications
|
com
|
comFax
|
Control (used within procedures when the specific type is unknown)
|
ctr
|
ctrCurrent
|
Data
|
dat
|
datBiblio
|
Data-bound combo box
|
dbcbo
|
dbcboLanguage
|
Data-bound grid
|
dbgrd
|
dbgrdQueryResult
|
Data-bound list box
|
dblst
|
dblstJobType
|
Data combo
|
dbc
|
dbcAuthor
|
Data grid
|
dgd
|
dgdTitles
|
Data list
|
dbl
|
dblPublisher
|
Data repeater
|
drp
|
drpLocation
|
Date picker
|
dtp
|
dtpPublished
|
Directory list box
|
dir
|
dirSource
|
Drive list box
|
drv
|
drvTarget
|
File list box
|
fil
|
filSource
|
Flat scroll bar
|
fsb
|
fsbMove
|
Form
|
frm
|
frmEntry
|
Frame
|
fra
|
fraLanguage
|
Gauge
|
gau
|
gauStatus
|
Graph
|
gra
|
graRevenue
|
Grid
|
grd
|
grdPrices
|
Hierarchical flexgrid
|
flex
|
flexOrders
|
Horizontal scroll bar
|
hsb
|
hsbVolume
|
Image
|
img
|
imgIcon
|
Image combo
|
imgcbo
|
imgcboProduct
|
ImageList
|
ils
|
ilsAllIcons
|
Label
|
lbl
|
lblHelpMessage
|
Lightweight check box
|
lwchk
|
lwchkArchive
|
Lightweight combo box
|
lwcbo
|
lwcboGerman
|
Lightweight command button
|
lwcmd
|
lwcmdRemove
|
Lightweight frame
|
lwfra
|
lwfraSaveOptions
|
Lightweight horizontal scroll bar
|
lwhsb
|
lwhsbVolume
|
Lightweight list box
|
lwlst
|
lwlstCostCenters
|
Lightweight option button
|
lwopt
|
lwoptIncomeLevel
|
Lightweight text box
|
lwtxt
|
lwoptStreet
|
Lightweight vertical scroll bar
|
lwvsb
|
lwvsbYear
|
Line
|
lin
|
linVertical
|
List box
|
lst
|
lstPolicyCodes
|
ListView
|
lvw
|
lvwHeadings
|
MAPI message
|
mpm
|
mpmSentMessage
|
MAPI session
|
mps
|
mpsSession
|
MCI
|
mci
|
mciVideo
|
Menu
|
mnu
|
mnuFileOpen
|
Month view
|
mvw
|
mvwPeriod
|
MS Chart
|
ch
|
chSalesbyRegion
|
MS Flex grid
|
mfg
|
mfgClients
|
MS Tab
|
mst
|
mstFirst
|
OLE container
|
ole
|
oleWorksheet
|
Option button
|
opt
|
optGender
|
Picture box
|
pic
|
picVGA
|
Picture clip
|
clp
|
clpToolbar
|
ProgressBar
|
prg
|
prgLoadFile
|
Remote Data
|
rd
|
rdTitles
|
RichTextBox
|
rtf
|
rtfReport
|
Shape
|
shp
|
shpCircle
|
Slider
|
sld
|
sldScale
|
Spin
|
spn
|
spnPages
|
StatusBar
|
sta
|
staDateTime
|
SysInfo
|
sys
|
sysMonitor
|
TabStrip
|
tab
|
tabOptions
|
Text box
|
txt
|
txtLastName
|
Timer
|
tmr
|
tmrAlarm
|
Toolbar
|
tlb
|
tlbActions
|
TreeView
|
tre
|
treOrganization
|
UpDown
|
upd
|
updDirection
|
Vertical scroll bar
|
vsb
|
vsbRate
|
Prefix conventions for menus
Menu control prefixes will be extended beyond the initial "mnu" label by adding an additional prefix for each level of nesting, with the final menu caption at the end of the name string. The following table lists some examples.
Menu caption sequence
|
Menu handler name
|
File Open
|
mnuFileOpen
|
File Send Email
|
mnuFileSendEmail
|
File Send Fax
|
mnuFileSendFax
|
Format Character
|
mnuFormatCharacter
|
Help Contents
|
mnuHelpContents
|
Variable naming conventions
Variable name must describe data type, scope and identifier of a variable.
Variable data types
Data type
|
Prefix
|
Example
|
Boolean
|
bln
|
|
Byte
|
byt
|
|
Currency
|
cur
|
|
Date (Time)
|
dtm
|
|
Double
|
dbl
|
|
Error
|
err
|
|
Integer
|
int
|
|
Long
|
lng
|
|
Object
|
obj
|
|
Single
|
sng
|
|
String
|
str
|
|
User-defined type
|
udt
|
|
Variant
|
vnt
|
|
Variable scope prefixes
Scope
|
Prefix
|
Example
|
Note
|
Global
|
G
|
gstrUserName
|
This variable is global and string type
|
Module-level
|
M
|
mblnCalcInProgress
|
This variable is module and boolean type
|
Local to procedure
|
None
|
dblVelocity
|
This variable is local and double type
|
Constants
The constant names will be UPPER_CASE with underscores (_) between words. For example:
Example
|
Note
|
USER_LIST_MAX
|
|
NEW_LINE
|
|
Prefixes for ActiveX Data Objects (ADO)
Use the following prefixes to indicate ActiveX Data Objects.
ADO object
|
Prefix
|
Example
|
Command
|
Cm
|
cmTitles
|
Connection
|
Cn
|
cnTitles
|
Field
|
Fld
|
fldName
|
Field Collection
|
flds
|
fldsTitles
|
Parameter
|
prm
|
prmTitleName
|
Parameter Collection
|
prms
|
prmsNames
|
Recordset
|
Rs
|
rsTitles
|
Structured Coding Conventions
In addition to naming conventions, structured coding conventions, such as code commenting and consistent indenting, can greatly improve code readability.
Code Commenting Conventions
All procedures and functions should begin with a brief comment describing the functional characteristics of the procedure (what it does). Input, output patameters passed to a procedure should be described. Function return values and global variables that are changed by the procedure must also be described at the beginning of each procedure.
Section heading
|
Comment description
|
Purpose
|
What the procedure does (not how).
|
Inputs
|
Describe roles of input parameters
|
Outputs
|
Describe roles of output parameters
|
Returns
|
Explanation of the values returned by functions.
|
Author
|
Author of module
|
Remember the following points:
-
Every important variable declaration should include an inline comment describing the use of the variable being declared.
-
Variables, controls, and procedures should be named clearly enough that inline commenting is only needed for complex implementation details.
-
At the start of the.bas module that contains the project's Visual Basic generic constant declarations, we should include an overview that describes the application, enumerating primary data objects, procedures, algorithms, dialogs, databases, and system dependencies. Sometimes a piece of pseudocode describing the algorithm can be helpful.
Formatting Your Code
Here are a few pointers:
-
Standard, tab-based, nested blocks should be indented four spaces (as the Visual Basic default).
-
The functional overview comment of a procedure should be indented one space. The highest level statements that follow the overview comment should be indented one tab, with each nested block indented an additional tab. For example:
'*****************************************************
' Purpose: Locates the first occurrence of a
' specified user in the UserList array.
' Inputs:
' strUserList(): the list of users to be searched.
' strTargetUser: the name of the user to search for.
' Returns: The index of the first occurrence of the
' rsTargetUser in the rasUserList array.
' If target user is not found, return -1.
'*****************************************************
Function FindUser (strUserList() As String, strTargetUser As _
String)As Integer
Dim inti As Integer ' Loop counter.
Dim blnFound As Integer ' Target found flag.
intFindUser = -1
inti = 0
While inti <= Ubound(strUserList) and Not blnFound
If strUserList(inti) = strTargetUser Then
blnFound = True
intFindUser = inti
End If
Wend
End Function
Grouping Constants
Visual Basic generic constants will be grouped in a single module to separate them from application-specific declarations.
& and + Operators
Always use the & operator when linking strings and the + operator when working with numerical values. Using the + operator to concatenate may cause problems when operating on two variants. For example:
vntVar1 = "10.01"
vntVar2 = 11
vntResult = vntVar1 + vntVar2 'vntResult = 21.01
vntResult = vntVar1 & vntVar2 'vntResult = 10.0111
Creating Strings for MsgBox, InputBox, and SQL Queries
When creating a long string, use the underscore line-continuation character to create multiple lines of code so that you can read or debug the string easily. This technique is particularly useful when displaying a message box (MsgBox) or input box (InputBox) or when creating an SQL string. For example:
Dim Msg As String
Msg = "This is a paragraph that will be " _
& "in a message box. The text is" _
& " broken into several lines of code" _
& " in the source code, making it easier" _
& " for the programmer to read and debug."
MsgBox Msg
Dim QRY As String
QRY = "SELECT *" _
& " FROM Titles" _
& " WHERE [Year Published] > 1988"
TitlesQry.SQL = QRY
Other conventions
Error trapping in development progress must follow these rules:
- Cascading error trapping. That mean all called functions will return system error codes, and showing message box will be implemented at the most exterior function/procedure.
- All system message will be located in resource file.
- Showing message boxes are implemented by pass parameters to a showing message global function.
- Error code contains 3 number.
- Versioning all modules: form module, code module...
- Display solution: 800x600 pixels
- Font size setting: Large font
- All of file name (*.vbp, *.frm, *.bas...) must less than 3 characters.
- Error code = 0 is OK.
- Error code < 0 is error.
- Error code >0 is warning.
- Function names should begin with a verb, such as InitNameArray or CloseDialog.
5.1.2Form design standard
Common conventions in form design
Items
|
Conventions
|
Note
|
Interface
|
In Vietnamese
|
|
Font name
|
MS Sans serif
|
Default
|
Font size
|
Default
|
|
Font color
|
Black
|
Default
|
Font style
|
Normal
|
Default
|
Distance between command buttons
|
100 Points
|
|
Command buttons alignment
|
Right
|
|
Label alignment
|
Left
|
|
All labels in a form must be collected in an array
|
|
|
All command buttons in a form must have same width
|
|
|
Caption of OK button
|
Chấp nhận
|
|
Caption of Cancel button
|
Thoát
|
|
Caption of Help button
|
Hướng dẫn
|
|
Caption of Add button
|
Thêm mới
|
|
Caption of Delete button
|
Xoá
|
|
Caption of Edit button
|
Sửa
|
|
Caption of Close button
|
Đóng
|
|
Caption of Save button
|
Ghi
|
|
Order of buttons in from (Left to right):
Thªm míi-Ghi-Söa-Xo¸-ChÊp nhËn-Tho¸t-Híng dÉn
|
|
|
Default button
|
Chấp nhận
|
|
Cancel button
|
Đóng
|
|
Sample:
Form controls appearance conventions
Control
|
Property
|
Value
|
Note
|
Check box
|
|
|
|
|
All colors
|
Default
|
|
Combo box, drop-down list box
|
|
|
|
|
Height
|
315
|
|
|
All colors
|
Default
|
|
|
|
|
|
Command button
|
|
|
|
|
Height
|
375 Points
|
|
|
Back color
|
Button face
|
VB Default
|
Form
|
|
|
|
|
Border style
|
Fixed length
|
|
|
Back color
|
Button face
|
VB Default
|
|
Startup position
|
CenterScreen
|
|
Label
|
|
|
|
|
Back color
|
Button face
|
VB Default
|
Vervical scroll bar
|
|
|
|
|
Width
|
260 Points
|
|
Horizontal scroll bar
|
|
|
|
|
Height
|
260 Points
|
|
Option button
|
|
|
|
|
Height
|
255 Points
|
|
Text box
|
|
|
|
|
Height
|
285 Points
|
|
|
All color
|
Default
|
| 5.1.3Report design standard (for Crystal Report)
Common conventions in report design
Items
|
Conventions
|
Note
|
Above of each total row must be a line, called Grouping line
|
|
|
Report boder
|
Only title row is bordered
|
|
Colour of all objects in report (line, character...)
|
Black
|
|
Report objects appearance conventions
Object
|
Property
|
Setting
|
Note
|
Report title
|
|
|
|
|
Font name
|
.VnArialH
|
|
|
Font style
|
Bold
|
|
|
Justify
|
Paper center
|
|
|
Space between report title and collumn heading
|
50 Points
|
Should be reviewed
|
Colunm heading
|
|
|
|
|
Font name
|
.VnArialNarrow
|
|
|
Font style
|
Bold
|
|
|
Font size
|
10
|
|
|
Justify
|
Column left
|
|
|
Before row spacing
|
6 Points
|
|
|
After row spacing
|
6 Points
|
|
|
|
|
|
Report body (data)
|
|
|
|
|
Font name
|
.VnArialNarrow
|
|
|
Font style
|
Bold
|
|
|
Font size
|
9
|
|
|
Justify
|
Depend on column data type (Number: right, string: right, date: center)
|
|
|
Row spacing
|
0
|
|
Total row
|
|
|
|
|
Font name
|
.VnArialNarrow
|
|
|
Font style
|
Bold
|
|
|
Font size
|
9
|
|
|
Justify
|
Right
|
|
|
Position
|
Under data block that summarized
|
|
Grouping line
|
|
|
|
|
Width
|
1 Point
|
|
Report boder
|
|
|
|
|
Width
|
1 Point
|
|
|
Boder column title only
|
|
|
Page number
|
|
|
|
|
Font name
|
.VnArial
|
|
|
Font size
|
9
|
|
|
Font style
|
Normal
|
|
|
Position
|
Right, bottom of page (Report footer)
|
|
|
Style
|
Page/Total page
|
|
Left sub title
|
|
|
|
|
Font name
|
.VnArial
|
|
|
Font size
|
10
|
|
|
Font style
|
Normal
|
|
|
Justify
|
Margin left
|
|
Right sub title
|
|
|
|
|
Font name
|
.VnArial
|
|
|
Font size
|
10
|
|
|
Font style
|
Normal
|
|
Sample:
Left subtitle
|
Report title
|
Right subtitle
|
Column heading 1
|
Column heading 2
|
Column heading 3
|
Column heading 4
|
Report body 1
|
Report body 2
|
Report body 3
|
Report body 4
|
Report body 1
|
Report body 2
|
Report body 3
|
Report body 4
|
|
|
|
Total row 4
|
Report body 1
|
Report body 2
|
Report body 3
|
Report body 4
|
Report body 1
|
Report body 2
|
Report body 3
|
Report body 4
|
|
|
|
Total row 4
|
(Page number) 1/5
5.1.4Database design standards
All of object names (include: table names, view names, field names...) in database must follow these conventions:
- In English.
- Can containts one or more words and no underscore between these words.
- No acronym except listed in table Acronyms bellow.
- The first letter of each word must be capitalized.
Chia sẻ với bạn bè của bạn: |