home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 5 / MasteringVisualBasic5.iso / ch_code / ch05 / keylist / keylist.frm (.txt) next >
Encoding:
Visual Basic Form  |  1997-02-20  |  10.3 KB  |  347 lines

  1. VERSION 5.00
  2. Begin VB.Form Lists 
  3.    Caption         =   "Key List"
  4.    ClientHeight    =   3750
  5.    ClientLeft      =   2400
  6.    ClientTop       =   1650
  7.    ClientWidth     =   6330
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   3750
  10.    ScaleWidth      =   6330
  11.    Begin VB.CommandButton DeleteButton 
  12.       Caption         =   "Delete"
  13.       BeginProperty Font 
  14.          Name            =   "MS Sans Serif"
  15.          Size            =   9.75
  16.          Charset         =   0
  17.          Weight          =   400
  18.          Underline       =   0   'False
  19.          Italic          =   0   'False
  20.          Strikethrough   =   0   'False
  21.       EndProperty
  22.       Height          =   495
  23.       Left            =   3000
  24.       TabIndex        =   2
  25.       Top             =   3000
  26.       Width           =   1215
  27.    End
  28.    Begin VB.CommandButton AddButton 
  29.       Caption         =   "Add New"
  30.       BeginProperty Font 
  31.          Name            =   "MS Sans Serif"
  32.          Size            =   9.75
  33.          Charset         =   0
  34.          Weight          =   400
  35.          Underline       =   0   'False
  36.          Italic          =   0   'False
  37.          Strikethrough   =   0   'False
  38.       EndProperty
  39.       Height          =   495
  40.       Left            =   4920
  41.       TabIndex        =   1
  42.       Top             =   3000
  43.       Width           =   1215
  44.    End
  45.    Begin VB.ListBox List1 
  46.       BeginProperty Font 
  47.          Name            =   "MS Sans Serif"
  48.          Size            =   9.75
  49.          Charset         =   0
  50.          Weight          =   400
  51.          Underline       =   0   'False
  52.          Italic          =   0   'False
  53.          Strikethrough   =   0   'False
  54.       EndProperty
  55.       Height          =   3210
  56.       Left            =   120
  57.       Sorted          =   -1  'True
  58.       TabIndex        =   0
  59.       Top             =   210
  60.       Width           =   2655
  61.    End
  62.    Begin VB.CommandButton CancelButton 
  63.       Caption         =   "Cancel"
  64.       BeginProperty Font 
  65.          Name            =   "MS Sans Serif"
  66.          Size            =   9.75
  67.          Charset         =   0
  68.          Weight          =   400
  69.          Underline       =   0   'False
  70.          Italic          =   0   'False
  71.          Strikethrough   =   0   'False
  72.       EndProperty
  73.       Height          =   495
  74.       Left            =   3000
  75.       TabIndex        =   4
  76.       Top             =   3000
  77.       Width           =   1215
  78.    End
  79.    Begin VB.CommandButton OKButton 
  80.       Caption         =   "OK"
  81.       BeginProperty Font 
  82.          Name            =   "MS Sans Serif"
  83.          Size            =   9.75
  84.          Charset         =   0
  85.          Weight          =   400
  86.          Underline       =   0   'False
  87.          Italic          =   0   'False
  88.          Strikethrough   =   0   'False
  89.       EndProperty
  90.       Height          =   495
  91.       Left            =   4920
  92.       TabIndex        =   3
  93.       Top             =   3000
  94.       Width           =   1215
  95.    End
  96.    Begin VB.TextBox Text1 
  97.       BeginProperty Font 
  98.          Name            =   "MS Sans Serif"
  99.          Size            =   9.75
  100.          Charset         =   0
  101.          Weight          =   400
  102.          Underline       =   0   'False
  103.          Italic          =   0   'False
  104.          Strikethrough   =   0   'False
  105.       EndProperty
  106.       Height          =   360
  107.       Left            =   2880
  108.       TabIndex        =   5
  109.       Top             =   390
  110.       Width           =   1455
  111.    End
  112.    Begin VB.TextBox Text2 
  113.       BeginProperty Font 
  114.          Name            =   "MS Sans Serif"
  115.          Size            =   9.75
  116.          Charset         =   0
  117.          Weight          =   400
  118.          Underline       =   0   'False
  119.          Italic          =   0   'False
  120.          Strikethrough   =   0   'False
  121.       EndProperty
  122.       Height          =   315
  123.       Left            =   4530
  124.       Locked          =   -1  'True
  125.       TabIndex        =   8
  126.       Top             =   390
  127.       Width           =   1455
  128.    End
  129.    Begin VB.TextBox Text3 
  130.       BeginProperty Font 
  131.          Name            =   "MS Sans Serif"
  132.          Size            =   9.75
  133.          Charset         =   0
  134.          Weight          =   400
  135.          Underline       =   0   'False
  136.          Italic          =   0   'False
  137.          Strikethrough   =   0   'False
  138.       EndProperty
  139.       Height          =   360
  140.       Left            =   2880
  141.       Locked          =   -1  'True
  142.       TabIndex        =   7
  143.       Top             =   1050
  144.       Width           =   3135
  145.    End
  146.    Begin VB.TextBox Text4 
  147.       BeginProperty Font 
  148.          Name            =   "MS Sans Serif"
  149.          Size            =   9.75
  150.          Charset         =   0
  151.          Weight          =   400
  152.          Underline       =   0   'False
  153.          Italic          =   0   'False
  154.          Strikethrough   =   0   'False
  155.       EndProperty
  156.       Height          =   675
  157.       Left            =   2880
  158.       Locked          =   -1  'True
  159.       MultiLine       =   -1  'True
  160.       TabIndex        =   6
  161.       Top             =   1815
  162.       Width           =   3135
  163.    End
  164.    Begin VB.Label Label4 
  165.       Caption         =   "Author"
  166.       BeginProperty Font 
  167.          Name            =   "MS Sans Serif"
  168.          Size            =   9.75
  169.          Charset         =   0
  170.          Weight          =   400
  171.          Underline       =   0   'False
  172.          Italic          =   0   'False
  173.          Strikethrough   =   0   'False
  174.       EndProperty
  175.       Height          =   225
  176.       Left            =   2880
  177.       TabIndex        =   12
  178.       Top             =   795
  179.       Width           =   1425
  180.    End
  181.    Begin VB.Label Label3 
  182.       Caption         =   "Published by"
  183.       BeginProperty Font 
  184.          Name            =   "MS Sans Serif"
  185.          Size            =   9.75
  186.          Charset         =   0
  187.          Weight          =   400
  188.          Underline       =   0   'False
  189.          Italic          =   0   'False
  190.          Strikethrough   =   0   'False
  191.       EndProperty
  192.       Height          =   270
  193.       Left            =   4515
  194.       TabIndex        =   11
  195.       Top             =   120
  196.       Width           =   1380
  197.    End
  198.    Begin VB.Label Label2 
  199.       Caption         =   "Title"
  200.       BeginProperty Font 
  201.          Name            =   "MS Sans Serif"
  202.          Size            =   9.75
  203.          Charset         =   0
  204.          Weight          =   400
  205.          Underline       =   0   'False
  206.          Italic          =   0   'False
  207.          Strikethrough   =   0   'False
  208.       EndProperty
  209.       Height          =   210
  210.       Left            =   2880
  211.       TabIndex        =   10
  212.       Top             =   1545
  213.       Width           =   1455
  214.    End
  215.    Begin VB.Label Label1 
  216.       Caption         =   "ISBN"
  217.       BeginProperty Font 
  218.          Name            =   "MS Sans Serif"
  219.          Size            =   9.75
  220.          Charset         =   0
  221.          Weight          =   400
  222.          Underline       =   0   'False
  223.          Italic          =   0   'False
  224.          Strikethrough   =   0   'False
  225.       EndProperty
  226.       Height          =   225
  227.       Left            =   2880
  228.       TabIndex        =   9
  229.       Top             =   120
  230.       Width           =   1440
  231.    End
  232. Attribute VB_Name = "Lists"
  233. Attribute VB_GlobalNameSpace = False
  234. Attribute VB_Creatable = False
  235. Attribute VB_PredeclaredId = True
  236. Attribute VB_Exposed = False
  237. Dim DataArray(999, 3) As String
  238. Dim ArrayIndex As Integer
  239. Function BSearch(KeyField) As Integer
  240. Dim Lower, Upper, Middle
  241. Lower = 0
  242. Upper = List1.ListCount - 1
  243. While 1
  244.     Middle = Fix((Lower + Upper) / 2)
  245.     If Upper < Lower Then
  246.         BSearch = -1
  247.         Exit Function
  248.     End If
  249.     If StrComp(KeyField, List1.List(Middle)) > 0 Then
  250.         Lower = Middle + 1
  251.     Else
  252.         If StrComp(KeyField, List1.List(Middle)) < 0 Then
  253.             Upper = Middle - 1
  254.         Else
  255.             BSearch = Middle
  256.             Exit Function
  257.         End If
  258.     End If
  259. End Function
  260. Sub ClearFields()
  261.     Text1.Text = ""
  262.     Text2.Text = ""
  263.     Text2.Locked = False
  264.     Text3.Text = ""
  265.     Text3.Locked = False
  266.     Text4.Text = ""
  267.     Text4.Locked = False
  268.     DeleteButton.Visible = False
  269.     AddButton.Visible = False
  270.     OKButton.Visible = True
  271.     CancelButton.Visible = True
  272. End Sub
  273. Sub ShowButtons()
  274.     OKButton.Visible = False
  275.     CancelButton.Visible = False
  276.     AddButton.Visible = True
  277.     DeleteButton.Visible = True
  278.     Text2.Locked = True
  279.     Text3.Locked = True
  280.     Text4.Locked = True
  281. End Sub
  282. Private Sub AddButton_Click()
  283.     ClearFields
  284. End Sub
  285. Private Sub CancelButton_Click()
  286.     ShowButtons
  287.     List1_Click
  288. End Sub
  289. Private Sub DeleteButton_Click()
  290. If List1.ListIndex < 0 Then
  291.     MsgBox "No item selected in the list"
  292.     List1.RemoveItem List1.ListIndex
  293. End If
  294. End Sub
  295. Private Sub Form_Load()
  296.     List1.Clear
  297. End Sub
  298. Private Sub List1_Click()
  299.     If List1.ListIndex < 0 Then
  300.         Text1.Text = ""
  301.         Text2.Text = ""
  302.         Text3.Text = ""
  303.         Text4.Text = ""
  304.         Exit Sub
  305.     End If
  306.     ItemIndex = List1.ItemData(List1.ListIndex)
  307.     Text1.Text = List1.List(List1.ListIndex)
  308.     Text2.Text = DataArray(ItemIndex, 1)
  309.     Text3.Text = DataArray(ItemIndex, 2)
  310.     Text4.Text = DataArray(ItemIndex, 3)
  311. End Sub
  312. Private Sub OKButton_Click()
  313.     Key = Trim(Text1.Text)
  314.     If Key = "" Then
  315.         MsgBox "Key field must be non-mepty"
  316.         Exit Sub
  317.     End If
  318.     position = BSearch(Trim(Text1.Text))
  319.     If position >= 0 Then
  320.          reply = MsgBox("Key exists. Replace existing record?", vbYesNo)
  321.         If reply = vbYes Then
  322.             List1.RemoveItem position
  323.         Else
  324.             Text1.SetFocus
  325.             Exit Sub
  326.         End If
  327.     End If
  328.     ArrayIndex = ArrayIndex + 1
  329.     List1.AddItem Key
  330.     List1.ItemData(List1.NewIndex) = ArrayIndex
  331.     DataArray(ArrayIndex, 1) = Text2.Text
  332.     DataArray(ArrayIndex, 2) = Text3.Text
  333.     DataArray(ArrayIndex, 3) = Text4.Text
  334.     List1.ListIndex = List1.NewIndex
  335.     ShowButtons
  336. End Sub
  337. Private Sub Text1_Change()
  338. position = BSearch(Trim$(Text1.Text))
  339. If position >= 0 Then
  340.     List1.ListIndex = position
  341.     List1_Click
  342.     Text2.Text = ""
  343.     Text3.Text = ""
  344.     Text4.Text = ""
  345. End If
  346. End Sub
  347.