home *** CD-ROM | disk | FTP | other *** search
/ Using Visual Basic 5 (Platinum Edition) / vb5.iso / Code / ch12 / Invoices.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-06-15  |  19.3 KB  |  568 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Invoices"
  4.    ClientHeight    =   6285
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   8475
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   6285
  10.    ScaleWidth      =   8475
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.CommandButton cmdDeleteItem 
  13.       Caption         =   "Delete Item"
  14.       Height          =   315
  15.       Left            =   1680
  16.       TabIndex        =   21
  17.       Top             =   5760
  18.       Width           =   1455
  19.    End
  20.    Begin VB.CommandButton cmdAddItem 
  21.       Caption         =   "Add Item"
  22.       Default         =   -1  'True
  23.       Height          =   315
  24.       Left            =   120
  25.       TabIndex        =   20
  26.       Top             =   5760
  27.       Width           =   1455
  28.    End
  29.    Begin VB.Frame Frame1 
  30.       Caption         =   "Invoice Details"
  31.       Height          =   3615
  32.       Left            =   120
  33.       TabIndex        =   22
  34.       Top             =   2040
  35.       Width           =   8175
  36.       Begin VB.CheckBox chkBackorder 
  37.          Height          =   315
  38.          Index           =   1
  39.          Left            =   7560
  40.          TabIndex        =   19
  41.          Top             =   720
  42.          Width           =   255
  43.       End
  44.       Begin VB.TextBox txtUnitPrice 
  45.          Height          =   315
  46.          Index           =   1
  47.          Left            =   4920
  48.          TabIndex        =   17
  49.          Top             =   720
  50.          Width           =   1095
  51.       End
  52.       Begin VB.TextBox txtDescription 
  53.          Height          =   315
  54.          Index           =   1
  55.          Left            =   2040
  56.          TabIndex        =   16
  57.          Top             =   720
  58.          Width           =   2895
  59.       End
  60.       Begin VB.ComboBox cboProductID 
  61.          Height          =   315
  62.          Index           =   1
  63.          Left            =   720
  64.          TabIndex        =   15
  65.          Top             =   720
  66.          Width           =   1335
  67.       End
  68.       Begin VB.TextBox txtQty 
  69.          Height          =   315
  70.          Index           =   1
  71.          Left            =   120
  72.          TabIndex        =   14
  73.          Top             =   720
  74.          Width           =   615
  75.       End
  76.       Begin VB.Label lblTotal 
  77.          Alignment       =   1  'Right Justify
  78.          BackColor       =   &H80000005&
  79.          BorderStyle     =   1  'Fixed Single
  80.          Height          =   315
  81.          Index           =   1
  82.          Left            =   6000
  83.          TabIndex        =   18
  84.          Top             =   720
  85.          Width           =   1215
  86.       End
  87.       Begin VB.Label lblColumn 
  88.          Alignment       =   2  'Center
  89.          BorderStyle     =   1  'Fixed Single
  90.          Caption         =   "Backorder"
  91.          Height          =   300
  92.          Index           =   6
  93.          Left            =   7200
  94.          TabIndex        =   29
  95.          Top             =   360
  96.          Width           =   855
  97.       End
  98.       Begin VB.Label lblColumn 
  99.          Alignment       =   2  'Center
  100.          BorderStyle     =   1  'Fixed Single
  101.          Caption         =   "Total "
  102.          Height          =   300
  103.          Index           =   5
  104.          Left            =   6000
  105.          TabIndex        =   27
  106.          Top             =   360
  107.          Width           =   1215
  108.       End
  109.       Begin VB.Label lblColumn 
  110.          Alignment       =   2  'Center
  111.          BorderStyle     =   1  'Fixed Single
  112.          Caption         =   "Unit Price "
  113.          Height          =   300
  114.          Index           =   4
  115.          Left            =   4920
  116.          TabIndex        =   26
  117.          Top             =   360
  118.          Width           =   1095
  119.       End
  120.       Begin VB.Label lblColumn 
  121.          Alignment       =   2  'Center
  122.          BorderStyle     =   1  'Fixed Single
  123.          Caption         =   " Description"
  124.          Height          =   300
  125.          Index           =   3
  126.          Left            =   2040
  127.          TabIndex        =   25
  128.          Top             =   360
  129.          Width           =   2895
  130.       End
  131.       Begin VB.Label lblColumn 
  132.          Alignment       =   2  'Center
  133.          BorderStyle     =   1  'Fixed Single
  134.          Caption         =   " Product ID"
  135.          Height          =   300
  136.          Index           =   2
  137.          Left            =   720
  138.          TabIndex        =   24
  139.          Top             =   360
  140.          Width           =   1335
  141.       End
  142.       Begin VB.Label lblColumn 
  143.          Alignment       =   2  'Center
  144.          BorderStyle     =   1  'Fixed Single
  145.          Caption         =   " Q.ty"
  146.          Height          =   300
  147.          Index           =   1
  148.          Left            =   120
  149.          TabIndex        =   23
  150.          Top             =   360
  151.          Width           =   615
  152.       End
  153.    End
  154.    Begin VB.TextBox txtHeader 
  155.       Height          =   315
  156.       Index           =   1
  157.       Left            =   5520
  158.       TabIndex        =   3
  159.       Top             =   120
  160.       Width           =   1095
  161.    End
  162.    Begin VB.TextBox txtHeader 
  163.       Height          =   315
  164.       Index           =   0
  165.       Left            =   1920
  166.       TabIndex        =   1
  167.       Top             =   120
  168.       Width           =   1215
  169.    End
  170.    Begin VB.TextBox Text5 
  171.       Height          =   315
  172.       Left            =   7680
  173.       TabIndex        =   13
  174.       Top             =   1560
  175.       Width           =   615
  176.    End
  177.    Begin VB.TextBox txtHeader 
  178.       Height          =   315
  179.       Index           =   5
  180.       Left            =   5520
  181.       TabIndex        =   11
  182.       Top             =   1560
  183.       Width           =   1095
  184.    End
  185.    Begin VB.TextBox txtHeader 
  186.       Height          =   315
  187.       Index           =   4
  188.       Left            =   1920
  189.       TabIndex        =   9
  190.       Top             =   1560
  191.       Width           =   2535
  192.    End
  193.    Begin VB.TextBox txtHeader 
  194.       Height          =   315
  195.       Index           =   3
  196.       Left            =   1920
  197.       TabIndex        =   7
  198.       Top             =   1080
  199.       Width           =   6375
  200.    End
  201.    Begin VB.TextBox txtHeader 
  202.       Height          =   315
  203.       Index           =   2
  204.       Left            =   1920
  205.       TabIndex        =   5
  206.       Top             =   600
  207.       Width           =   6375
  208.    End
  209.    Begin VB.Label lblGrandTotal 
  210.       Alignment       =   1  'Right Justify
  211.       BackColor       =   &H80000005&
  212.       BorderStyle     =   1  'Fixed Single
  213.       Height          =   315
  214.       Left            =   6120
  215.       TabIndex        =   30
  216.       Top             =   5760
  217.       Width           =   1335
  218.    End
  219.    Begin VB.Label Label1 
  220.       Alignment       =   1  'Right Justify
  221.       BorderStyle     =   1  'Fixed Single
  222.       Caption         =   "Grand Total "
  223.       Height          =   315
  224.       Index           =   13
  225.       Left            =   4800
  226.       TabIndex        =   28
  227.       Top             =   5760
  228.       Width           =   1335
  229.    End
  230.    Begin VB.Label Label1 
  231.       Alignment       =   1  'Right Justify
  232.       BorderStyle     =   1  'Fixed Single
  233.       Caption         =   "&Date "
  234.       Height          =   315
  235.       Index           =   6
  236.       Left            =   4560
  237.       TabIndex        =   2
  238.       Top             =   120
  239.       Width           =   855
  240.    End
  241.    Begin VB.Label Label1 
  242.       Alignment       =   1  'Right Justify
  243.       BorderStyle     =   1  'Fixed Single
  244.       Caption         =   "&Invoice No. "
  245.       Height          =   315
  246.       Index           =   5
  247.       Left            =   240
  248.       TabIndex        =   0
  249.       Top             =   120
  250.       Width           =   1575
  251.    End
  252.    Begin VB.Label Label1 
  253.       Alignment       =   1  'Right Justify
  254.       BorderStyle     =   1  'Fixed Single
  255.       Caption         =   "&State "
  256.       Height          =   315
  257.       Index           =   4
  258.       Left            =   6720
  259.       TabIndex        =   12
  260.       Top             =   1560
  261.       Width           =   855
  262.    End
  263.    Begin VB.Label Label1 
  264.       Alignment       =   1  'Right Justify
  265.       BorderStyle     =   1  'Fixed Single
  266.       Caption         =   "&ZIP code "
  267.       Height          =   315
  268.       Index           =   3
  269.       Left            =   4560
  270.       TabIndex        =   10
  271.       Top             =   1560
  272.       Width           =   855
  273.    End
  274.    Begin VB.Label Label1 
  275.       Alignment       =   1  'Right Justify
  276.       BorderStyle     =   1  'Fixed Single
  277.       Caption         =   "&City "
  278.       Height          =   315
  279.       Index           =   2
  280.       Left            =   240
  281.       TabIndex        =   8
  282.       Top             =   1560
  283.       Width           =   1575
  284.    End
  285.    Begin VB.Label Label1 
  286.       Alignment       =   1  'Right Justify
  287.       BorderStyle     =   1  'Fixed Single
  288.       Caption         =   "&Address "
  289.       Height          =   315
  290.       Index           =   1
  291.       Left            =   240
  292.       TabIndex        =   6
  293.       Top             =   1080
  294.       Width           =   1575
  295.    End
  296.    Begin VB.Label Label1 
  297.       Alignment       =   1  'Right Justify
  298.       BorderStyle     =   1  'Fixed Single
  299.       Caption         =   "Customer &Name "
  300.       Height          =   315
  301.       Index           =   0
  302.       Left            =   240
  303.       TabIndex        =   4
  304.       Top             =   600
  305.       Width           =   1575
  306.    End
  307. Attribute VB_Name = "Form1"
  308. Attribute VB_GlobalNameSpace = False
  309. Attribute VB_Creatable = False
  310. Attribute VB_PredeclaredId = True
  311. Attribute VB_Exposed = False
  312. Option Explicit
  313. Private Type TProductInfo
  314.     ID As String
  315.     Description As String
  316.     UnitPrice As Currency
  317. End Type
  318. ' max number of detail lines in the invoice
  319. Const LINES_MAX = 8
  320. ' this array holds information on each product
  321. Const PRODUCT_NUM = 10
  322. Dim ProductInfo() As TProductInfo
  323. ' this variable tracks which line the cursor is currently on
  324. ' zero means that it is on the upper portion of the form
  325. Dim currentLine As Integer
  326. Private Sub Form_Load()
  327.     ' load ID, description and price unit for a bunch of products
  328.     ' (in a real application this information would be loaded
  329.     '  from a file or a database table)
  330.     Dim i As Integer
  331.     ReDim ProductInfo(1 To PRODUCT_NUM) As TProductInfo
  332.     ProductInfo(1).ID = "Mouse/S"
  333.     ProductInfo(1).Description = "Serial Mouse"
  334.     ProductInfo(1).UnitPrice = 39.5
  335.     ProductInfo(2).ID = "Mouse/PS2"
  336.     ProductInfo(2).Description = "Mouse with PS/2 connector"
  337.     ProductInfo(2).UnitPrice = 49.99
  338.     ProductInfo(3).ID = "Modem/I"
  339.     ProductInfo(3).Description = "Internal 28.800 baud modem"
  340.     ProductInfo(3).UnitPrice = 105
  341.     ProductInfo(4).ID = "Modem/E"
  342.     ProductInfo(4).Description = "Internal 28.800 baud modem"
  343.     ProductInfo(4).UnitPrice = 105
  344.     ProductInfo(5).ID = "Video/V"
  345.     ProductInfo(5).Description = "VGA Video Card"
  346.     ProductInfo(5).UnitPrice = 45
  347.     ProductInfo(6).ID = "Video/SV"
  348.     ProductInfo(6).Description = "Super-VGA Video Card"
  349.     ProductInfo(6).UnitPrice = 75
  350.     ProductInfo(7).ID = "CdRom/6"
  351.     ProductInfo(7).Description = "CDROM Driver 6x"
  352.     ProductInfo(7).UnitPrice = 109
  353.     ProductInfo(8).ID = "CdRom/12"
  354.     ProductInfo(8).Description = "CDROM Driver 12x"
  355.     ProductInfo(8).UnitPrice = 225
  356.     ProductInfo(9).ID = "Cable/P"
  357.     ProductInfo(9).Description = "Parallel Cable"
  358.     ProductInfo(9).UnitPrice = 7.99
  359.     ProductInfo(10).ID = "Cable/S"
  360.     ProductInfo(10).Description = "Serial Cable"
  361.     ProductInfo(10).UnitPrice = 7.99
  362.     ' load product IDs into the combo box
  363.     For i = 1 To PRODUCT_NUM
  364.         cboProductID(1).AddItem ProductInfo(i).ID
  365.     Next
  366.         
  367. End Sub
  368. Private Sub txtHeader_GotFocus(Index As Integer)
  369.     NewCurrentLine 0
  370. End Sub
  371. Private Sub txtQty_GotFocus(Index As Integer)
  372.     NewCurrentLine Index
  373. End Sub
  374. Private Sub cboProductID_GotFocus(Index As Integer)
  375.     NewCurrentLine Index
  376. End Sub
  377. Private Sub txtDescription_GotFocus(Index As Integer)
  378.     NewCurrentLine Index
  379. End Sub
  380. Private Sub txtUnitPrice_GotFocus(Index As Integer)
  381.     NewCurrentLine Index
  382. End Sub
  383. Private Sub chkBackorder_GotFocus(Index As Integer)
  384.     NewCurrentLine Index
  385. End Sub
  386. Private Sub NewCurrentLine(newLine As Integer)
  387.     ' set a yellow background for the controls on the
  388.     ' current line, and white for all the others
  389.     Dim Index As Integer
  390.     Dim foColor As Long, bkColor As Long
  391.     currentLine = newLine
  392.     For Index = txtQty.LBound To txtQty.UBound
  393.         If Index = currentLine Then
  394.             foColor = vbHighlightText
  395.             bkColor = vbHighlight
  396.         Else
  397.             foColor = vbWindowText
  398.             bkColor = vbWindowBackground
  399.         End If
  400.         txtQty(Index).ForeColor = foColor
  401.         txtQty(Index).BackColor = bkColor
  402.         cboProductID(Index).ForeColor = foColor
  403.         cboProductID(Index).BackColor = bkColor
  404.         txtDescription(Index).ForeColor = foColor
  405.         txtDescription(Index).BackColor = bkColor
  406.         txtUnitPrice(Index).ForeColor = foColor
  407.         txtUnitPrice(Index).BackColor = bkColor
  408.         lblTotal(Index).ForeColor = foColor
  409.         lblTotal(Index).BackColor = bkColor
  410.         ' don't touch checkbox's colors
  411.     Next
  412. End Sub
  413. Private Sub cmdAddItem_Click()
  414.     ' add a new line for Invoice details
  415.     Dim newLine As Integer
  416.     Dim lineTop As Single
  417.     Dim i As Integer
  418.     newLine = txtQty.UBound + 1
  419.     ' exit if too many lines
  420.     If newLine > LINES_MAX Then Exit Sub
  421.     ' load all the controls that make up the row
  422.     ' it is preferable to load all controls *before* acting
  423.     ' on their properties, because otherwise a change event might
  424.     ' rise an error since it would refer to a non existing control
  425.     Load txtQty(newLine)
  426.     Load cboProductID(newLine)
  427.     Load txtDescription(newLine)
  428.     Load txtUnitPrice(newLine)
  429.     Load lblTotal(newLine)
  430.     Load chkBackorder(newLine)
  431.     ' then move controls in the correct position, make
  432.     ' them visible and clear them
  433.     lineTop = txtQty(newLine - 1).top + txtQty(newLine - 1).Height
  434.     ' we don't need to modify the Left property, whose
  435.     ' value is inherited by the control in the above line
  436.     txtQty(newLine).top = lineTop
  437.     txtQty(newLine).Visible = True
  438.     txtQty(newLine).text = ""
  439.     cboProductID(newLine).top = lineTop
  440.     cboProductID(newLine).Visible = True
  441.     cboProductID(newLine).text = ""
  442.     txtDescription(newLine).top = lineTop
  443.     txtDescription(newLine).Visible = True
  444.     txtDescription(newLine).text = ""
  445.     txtUnitPrice(newLine).top = lineTop
  446.     txtUnitPrice(newLine).Visible = True
  447.     txtUnitPrice(newLine).text = ""
  448.     lblTotal(newLine).top = lineTop
  449.     lblTotal(newLine).Visible = True
  450.     lblTotal(newLine).Caption = ""
  451.     chkBackorder(newLine).top = lineTop
  452.     chkBackorder(newLine).Visible = True
  453.     chkBackorder(newLine).Value = 0
  454.     ' load product IDs into the combo box
  455.     cboProductID(newLine).Clear
  456.     For i = 1 To PRODUCT_NUM
  457.         cboProductID(newLine).AddItem ProductInfo(i).ID
  458.     Next
  459.         
  460.     ' set input focus to the Qty textbox
  461.     DoEvents
  462.     txtQty(newLine).SetFocus
  463.      
  464. End Sub
  465. Private Sub cmdDeleteItem_Click()
  466.     ' delete the current line
  467.     Dim Index As Integer
  468.     Dim lastLine As Integer
  469.     lastLine = txtQty.UBound
  470.     ' exit if the cursor is not on a invoce item or if there
  471.     ' is only one line (these controls are created at design-time
  472.     ' and cannot be unloaded)
  473.     If currentLine = 0 Or lastLine = 1 Then Exit Sub
  474.     ' move all values up one row
  475.     For Index = currentLine To lastLine - 1
  476.         txtQty(Index).text = txtQty(Index + 1).text
  477.         cboProductID(Index).text = cboProductID(Index + 1).text
  478.         txtDescription(Index).text = txtDescription(Index + 1).text
  479.         txtUnitPrice(Index).text = txtUnitPrice(Index + 1).text
  480.         lblTotal(Index).Caption = lblTotal(Index + 1).Caption
  481.         chkBackorder(Index).Value = chkBackorder(Index + 1).Value
  482.     Next
  483.     ' clear the lblTotal value for the last line
  484.     '  (this forces the evaluation of grand total)
  485.     lblTotal(lastLine).Caption = ""
  486.     ' if we are about to delete the control that has the
  487.     ' input focus, move the focus elsewhere
  488.     If currentLine = lastLine Then
  489.         txtQty(lastLine - 1).SetFocus
  490.     End If
  491.     ' unload the last line of controls
  492.     Unload txtQty(lastLine)
  493.     Unload cboProductID(lastLine)
  494.     Unload txtDescription(lastLine)
  495.     Unload txtUnitPrice(lastLine)
  496.     Unload lblTotal(lastLine)
  497.     Unload chkBackorder(lastLine)
  498. End Sub
  499. Private Sub cboProductID_Click(Index As Integer)
  500.     ' the user has selected a product
  501.     Dim i As Integer
  502.     i = cboProductID(Index).ListIndex
  503.     If i >= 0 Then
  504.         txtDescription(Index).text = ProductInfo(i + 1).Description
  505.         txtUnitPrice(Index).text = Format$(ProductInfo(i + 1).UnitPrice, "###.00")
  506.     End If
  507. End Sub
  508. Private Sub txtQty_Change(Index As Integer)
  509.     UpdateLineTotal Index
  510. End Sub
  511. Private Sub txtUnitPrice_Change(Index As Integer)
  512.     UpdateLineTotal Index
  513. End Sub
  514. Private Sub UpdateLineTotal(Index As Integer)
  515.     ' update the total value of current line
  516.     If txtQty(Index).text <> "" And txtUnitPrice(Index).text <> "" Then
  517.         lblTotal(Index).Caption = Format$(CCur(txtQty(Index).text) * CCur(txtUnitPrice(Index).text), "###,###.00")
  518.     Else
  519.         lblTotal(Index).Caption = ""
  520.     End If
  521. End Sub
  522. Private Sub lblTotal_Change(Index As Integer)
  523.     ' update the grand total
  524.     Dim i As Integer, result As Currency
  525.     For i = lblTotal.LBound To lblTotal.UBound
  526.         If lblTotal(i).Caption <> "" Then
  527.             result = result + CCur(lblTotal(i).Caption)
  528.         End If
  529.     Next
  530.     lblGrandTotal.Caption = Format$(result, "###,###.00")
  531. End Sub
  532. Private Sub txtHeader_KeyPress(Index As Integer, KeyAscii As Integer)
  533.     ' ensure that numeric fields only get numeric keys
  534.     If KeyAscii < 32 Then Exit Sub
  535.     Select Case Index
  536.         Case 0, 5   ' Invoice number & ZIP code
  537.             If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
  538.         Case 1      ' invoce date
  539.             If (KeyAscii < 48 Or KeyAscii > 57) Then
  540.                 If KeyAscii <> Asc("/") Then KeyAscii = 0
  541.             End If
  542.     End Select
  543.     ' protest loudly
  544.     If KeyAscii = 0 Then Beep
  545. End Sub
  546. Private Sub txtQty_KeyPress(Index As Integer, KeyAscii As Integer)
  547.     ' ignore non-numeric input
  548.     If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii >= 32 Then
  549.         KeyAscii = 0
  550.         Beep
  551.     End If
  552. End Sub
  553. Private Sub txtUnitPrice_KeyPress(Index As Integer, KeyAscii As Integer)
  554.     ' ignore non-numeric input, but accept decimal separator
  555.     If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii >= 32 And KeyAscii <> Asc(".") Then
  556.         KeyAscii = 0
  557.         Beep
  558.     End If
  559. End Sub
  560. Private Sub lblColumn_Click(Index As Integer)
  561.     ' change the header of this column
  562.     Dim newCaption As String
  563.     newCaption = InputBox$("Enter a new label for this column", "My Grid", lblColumn(Index).Caption)
  564.     If newCaption <> "" Then
  565.         lblColumn(Index).Caption = newCaption
  566.     End If
  567. End Sub
  568.