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

  1. VERSION 5.00
  2. Object = "{0BA686C6-F7D3-101A-993E-0000C0EF6F5E}#1.0#0"; "THREED32.OCX"
  3. Begin VB.Form Form1 
  4.    Caption         =   "List Search"
  5.    ClientHeight    =   3915
  6.    ClientLeft      =   2400
  7.    ClientTop       =   1650
  8.    ClientWidth     =   6990
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   3915
  11.    ScaleWidth      =   6990
  12.    Begin VB.CommandButton DeleteButton 
  13.       Caption         =   "Delete"
  14.       BeginProperty Font 
  15.          Name            =   "Verdana"
  16.          Size            =   9.75
  17.          Charset         =   0
  18.          Weight          =   400
  19.          Underline       =   0   'False
  20.          Italic          =   0   'False
  21.          Strikethrough   =   0   'False
  22.       EndProperty
  23.       Height          =   495
  24.       Left            =   3000
  25.       TabIndex        =   2
  26.       Top             =   3240
  27.       Width           =   1215
  28.    End
  29.    Begin VB.CommandButton AddButton 
  30.       Caption         =   "Add New"
  31.       BeginProperty Font 
  32.          Name            =   "Verdana"
  33.          Size            =   9.75
  34.          Charset         =   0
  35.          Weight          =   400
  36.          Underline       =   0   'False
  37.          Italic          =   0   'False
  38.          Strikethrough   =   0   'False
  39.       EndProperty
  40.       Height          =   495
  41.       Left            =   5640
  42.       TabIndex        =   1
  43.       Top             =   3240
  44.       Width           =   1215
  45.    End
  46.    Begin VB.ListBox List1 
  47.       BeginProperty Font 
  48.          Name            =   "Verdana"
  49.          Size            =   9
  50.          Charset         =   0
  51.          Weight          =   400
  52.          Underline       =   0   'False
  53.          Italic          =   0   'False
  54.          Strikethrough   =   0   'False
  55.       EndProperty
  56.       Height          =   3450
  57.       Left            =   120
  58.       Sorted          =   -1  'True
  59.       TabIndex        =   0
  60.       Top             =   240
  61.       Width           =   2655
  62.    End
  63.    Begin VB.CommandButton CancelButton 
  64.       Caption         =   "Cancel"
  65.       BeginProperty Font 
  66.          Name            =   "Verdana"
  67.          Size            =   9.75
  68.          Charset         =   0
  69.          Weight          =   400
  70.          Underline       =   0   'False
  71.          Italic          =   0   'False
  72.          Strikethrough   =   0   'False
  73.       EndProperty
  74.       Height          =   495
  75.       Left            =   3000
  76.       TabIndex        =   4
  77.       Top             =   3240
  78.       Width           =   1215
  79.    End
  80.    Begin VB.CommandButton OKButton 
  81.       Caption         =   "OK"
  82.       BeginProperty Font 
  83.          Name            =   "Verdana"
  84.          Size            =   9.75
  85.          Charset         =   0
  86.          Weight          =   400
  87.          Underline       =   0   'False
  88.          Italic          =   0   'False
  89.          Strikethrough   =   0   'False
  90.       EndProperty
  91.       Height          =   495
  92.       Left            =   5640
  93.       TabIndex        =   3
  94.       Top             =   3255
  95.       Width           =   1215
  96.    End
  97.    Begin Threed.SSPanel SSPanel1 
  98.       Height          =   2775
  99.       Left            =   2880
  100.       TabIndex        =   5
  101.       Top             =   240
  102.       Width           =   3975
  103.       _Version        =   65536
  104.       _ExtentX        =   7011
  105.       _ExtentY        =   4895
  106.       _StockProps     =   15
  107.       BackColor       =   12632256
  108.       BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  109.          Name            =   "MS Sans Serif"
  110.          Size            =   8.25
  111.          Charset         =   0
  112.          Weight          =   400
  113.          Underline       =   0   'False
  114.          Italic          =   0   'False
  115.          Strikethrough   =   0   'False
  116.       EndProperty
  117.       BevelOuter      =   1
  118.       BevelInner      =   1
  119.       Begin VB.TextBox Text1 
  120.          BeginProperty Font 
  121.             Name            =   "Verdana"
  122.             Size            =   9
  123.             Charset         =   0
  124.             Weight          =   400
  125.             Underline       =   0   'False
  126.             Italic          =   0   'False
  127.             Strikethrough   =   0   'False
  128.          EndProperty
  129.          Height          =   315
  130.          Left            =   120
  131.          TabIndex        =   9
  132.          Top             =   480
  133.          Width           =   1665
  134.       End
  135.       Begin VB.TextBox Text2 
  136.          BeginProperty Font 
  137.             Name            =   "Verdana"
  138.             Size            =   9
  139.             Charset         =   0
  140.             Weight          =   400
  141.             Underline       =   0   'False
  142.             Italic          =   0   'False
  143.             Strikethrough   =   0   'False
  144.          EndProperty
  145.          Height          =   315
  146.          Left            =   2040
  147.          Locked          =   -1  'True
  148.          TabIndex        =   8
  149.          Top             =   480
  150.          Width           =   1725
  151.       End
  152.       Begin VB.TextBox Text3 
  153.          BeginProperty Font 
  154.             Name            =   "Verdana"
  155.             Size            =   9
  156.             Charset         =   0
  157.             Weight          =   400
  158.             Underline       =   0   'False
  159.             Italic          =   0   'False
  160.             Strikethrough   =   0   'False
  161.          EndProperty
  162.          Height          =   315
  163.          Left            =   120
  164.          Locked          =   -1  'True
  165.          TabIndex        =   7
  166.          Top             =   1200
  167.          Width           =   3660
  168.       End
  169.       Begin VB.TextBox Text4 
  170.          BeginProperty Font 
  171.             Name            =   "Verdana"
  172.             Size            =   9
  173.             Charset         =   0
  174.             Weight          =   400
  175.             Underline       =   0   'False
  176.             Italic          =   0   'False
  177.             Strikethrough   =   0   'False
  178.          EndProperty
  179.          Height          =   675
  180.          Left            =   120
  181.          Locked          =   -1  'True
  182.          MultiLine       =   -1  'True
  183.          TabIndex        =   6
  184.          Top             =   1905
  185.          Width           =   3645
  186.       End
  187.       Begin Threed.SSPanel SSPanel2 
  188.          Height          =   255
  189.          Left            =   120
  190.          TabIndex        =   10
  191.          Top             =   240
  192.          Width           =   1215
  193.          _Version        =   65536
  194.          _ExtentX        =   2143
  195.          _ExtentY        =   450
  196.          _StockProps     =   15
  197.          Caption         =   "I S B N"
  198.          BackColor       =   12632256
  199.          BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  200.             Name            =   "Verdana"
  201.             Size            =   9.01
  202.             Charset         =   0
  203.             Weight          =   400
  204.             Underline       =   0   'False
  205.             Italic          =   0   'False
  206.             Strikethrough   =   0   'False
  207.          EndProperty
  208.          BevelOuter      =   0
  209.          Font3D          =   3
  210.          Alignment       =   1
  211.       End
  212.       Begin Threed.SSPanel SSPanel3 
  213.          Height          =   255
  214.          Left            =   2010
  215.          TabIndex        =   11
  216.          Top             =   240
  217.          Width           =   1215
  218.          _Version        =   65536
  219.          _ExtentX        =   2143
  220.          _ExtentY        =   450
  221.          _StockProps     =   15
  222.          Caption         =   "Published by"
  223.          BackColor       =   12632256
  224.          BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  225.             Name            =   "Verdana"
  226.             Size            =   9.01
  227.             Charset         =   0
  228.             Weight          =   400
  229.             Underline       =   0   'False
  230.             Italic          =   0   'False
  231.             Strikethrough   =   0   'False
  232.          EndProperty
  233.          BevelOuter      =   0
  234.          Font3D          =   3
  235.          Alignment       =   1
  236.       End
  237.       Begin Threed.SSPanel SSPanel4 
  238.          Height          =   255
  239.          Left            =   120
  240.          TabIndex        =   12
  241.          Top             =   960
  242.          Width           =   1095
  243.          _Version        =   65536
  244.          _ExtentX        =   1931
  245.          _ExtentY        =   450
  246.          _StockProps     =   15
  247.          Caption         =   "Auhor"
  248.          BackColor       =   12632256
  249.          BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  250.             Name            =   "Verdana"
  251.             Size            =   9.01
  252.             Charset         =   0
  253.             Weight          =   400
  254.             Underline       =   0   'False
  255.             Italic          =   0   'False
  256.             Strikethrough   =   0   'False
  257.          EndProperty
  258.          BevelOuter      =   0
  259.          Font3D          =   3
  260.          Alignment       =   1
  261.       End
  262.       Begin Threed.SSPanel SSPanel5 
  263.          Height          =   255
  264.          Left            =   120
  265.          TabIndex        =   13
  266.          Top             =   1680
  267.          Width           =   1215
  268.          _Version        =   65536
  269.          _ExtentX        =   2143
  270.          _ExtentY        =   450
  271.          _StockProps     =   15
  272.          Caption         =   "Title"
  273.          BackColor       =   12632256
  274.          BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  275.             Name            =   "Verdana"
  276.             Size            =   9.01
  277.             Charset         =   0
  278.             Weight          =   400
  279.             Underline       =   0   'False
  280.             Italic          =   0   'False
  281.             Strikethrough   =   0   'False
  282.          EndProperty
  283.          BevelOuter      =   0
  284.          Font3D          =   3
  285.          Alignment       =   1
  286.       End
  287.    End
  288. Attribute VB_Name = "Form1"
  289. Attribute VB_GlobalNameSpace = False
  290. Attribute VB_Creatable = False
  291. Attribute VB_PredeclaredId = True
  292. Attribute VB_Exposed = False
  293. Dim DataArray(999, 3) As String
  294. Dim ArrayIndex As Integer
  295. Function BSearch(lower As Integer, upper As Integer, KeyField As String) As Integer
  296. Dim middle As Integer
  297.     middle = Fix(lower + upper) / 2
  298.     If List1.List(middle) = KeyField Then
  299.         BSearch = middle
  300.         Exit Function
  301.     End If
  302.     If lower >= upper Then
  303.         BSearch = -1
  304.         Exit Function
  305.     End If
  306.     If KeyField < List1.List(middle) Then
  307.         upper = middle - 1
  308.     Else
  309.         lower = middle + 1
  310.     End If
  311.     BSearch = BSearch(lower, upper, KeyField)
  312. End Function
  313. Sub ClearFields()
  314.     Text1.Text = ""
  315.     Text2.Text = ""
  316.     Text2.Locked = False
  317.     Text3.Text = ""
  318.     Text3.Locked = False
  319.     Text4.Text = ""
  320.     Text4.Locked = False
  321.     DeleteButton.Visible = False
  322.     AddButton.Visible = False
  323.     OKButton.Visible = True
  324.     CancelButton.Visible = True
  325. End Sub
  326. Sub ShowButtons()
  327.     OKButton.Visible = False
  328.     CancelButton.Visible = False
  329.     AddButton.Visible = True
  330.     DeleteButton.Visible = True
  331.     Text2.Locked = True
  332.     Text3.Locked = True
  333.     Text4.Locked = True
  334. End Sub
  335. Private Sub AddButton_Click()
  336.     ClearFields
  337.     Text1.SetFocus
  338. End Sub
  339. Private Sub CancelButton_Click()
  340.     ShowButtons
  341.     List1_Click
  342. End Sub
  343. Private Sub DeleteButton_Click()
  344. If List1.ListIndex < 0 Then
  345.     MsgBox "No item selected in the list"
  346.     List1.RemoveItem List1.ListIndex
  347. End If
  348. End Sub
  349. Private Sub Form_Load()
  350.     List1.Clear
  351. End Sub
  352. Private Sub List1_Click()
  353.     If List1.ListIndex < 0 Then
  354.         Text1.Text = ""
  355.         Text2.Text = ""
  356.         Text3.Text = ""
  357.         Text4.Text = ""
  358.         Exit Sub
  359.     End If
  360.     ItemIndex = List1.ItemData(List1.ListIndex)
  361.     Text1.Text = List1.List(List1.ListIndex)
  362.     Text2.Text = DataArray(ItemIndex, 1)
  363.     Text3.Text = DataArray(ItemIndex, 2)
  364.     Text4.Text = DataArray(ItemIndex, 3)
  365. End Sub
  366. Private Sub OKButton_Click()
  367.     Key = Trim(Text1.Text)
  368.     If Key = "" Then
  369.         MsgBox "Key field must be non-mepty"
  370.         Exit Sub
  371.     End If
  372.     position = BSearch(0, List1.ListCount - 1, Trim(Text1.Text))
  373.     If position >= 0 Then
  374.          reply = MsgBox("Key exists. Replace existing record?", vbYesNo)
  375.         If reply = vbYes Then
  376.             List1.RemoveItem position
  377.         Else
  378.             Text1.SetFocus
  379.             Exit Sub
  380.         End If
  381.     End If
  382.     ArrayIndex = ArrayIndex + 1
  383.     List1.AddItem Key
  384.     List1.ItemData(List1.NewIndex) = ArrayIndex
  385.     DataArray(ArrayIndex, 1) = Text2.Text
  386.     DataArray(ArrayIndex, 2) = Text3.Text
  387.     DataArray(ArrayIndex, 3) = Text4.Text
  388.     List1.ListIndex = List1.NewIndex
  389.     ShowButtons
  390. End Sub
  391. Private Sub Text1_Change()
  392. If List1.ListCount = 0 Then Exit Sub
  393. position = BSearch(0, List1.ListCount - 1, (Text1.Text))
  394. If position >= 0 Then
  395.     List1.ListIndex = position
  396.     List1_Click
  397.     Text2.Text = ""
  398.     Text3.Text = ""
  399.     Text4.Text = ""
  400. End If
  401. End Sub
  402.