ComponentOne List 8.0 for ActiveX
Tutorial 17 - Sorting and Searching

In this tutorial, you will learn how to use the sorting and searching features of the XArrayDB object. The QuickSort method sorts a range of rows in an XArrayDB object according to one or more columns (up to ten). The Find method searches for a specified value within a column of an XArrayDB object, starting at a particular row

  1. Start a new project.

  2. Add the following controls on the form (Form1) as shown in the following figure: two text box controls (Text1 and Text2), a command button (Command1), two labels (Label1 and Label2), and a TDBList control (TDBList1).

    Initializing the array data

    Next, initialize the XArrayDB object so that it contains 100 rows and 4 columns of random integers.

  3. Place the following code in the Form_Load (Visual Basic) event:

    Example Title
    Copy Code
    Private Sub Form_Load()
    
    ' Allocate space for 100 rows, 4 columns
    
        x.ReDim 0, 99, 0, 3
    
     
    
        Dim row As Long, col As Integer
    
     
    
    ' The LowerBound and UpperBound properties correspond
    
    ' to the LBound and UBound functions in Visual Basic.
    
    ' Hard-coded dimensions can be used instead, if known.
    
        For row = x.LowerBound(1) To x.UpperBound(1)
    
            For col = x.LowerBound(2) To x.UpperBound(2)
    
                x(row, col) = CInt(99 * Rnd + 1)
    
            Next col
    
        Next row
    
     
    
    ' Bind True DBList Control to this XArrayDB instance
    
        Set TDBList1.Array = x
    
       
    
    ' Enable footers
    
        TDBList1.ColumnFooters = True
    
       
    
    ' Display headers and footers as buttons
    
        Dim obcol As TrueDBList80.Column
    
        For Each obcol In TDBList1.Columns
    
            obcol.ButtonFooter = True
    
            obcol.ButtonHeader = True
    
        Next obcol
    
    End Sub
    
  4. Add the following code to Command1:

    Example Title
    Copy Code
    Private Sub Command1_Click()
    
        Dim RowFound As Long
    
    ' Execute Find
    
        RowFound = x.Find(x.LowerBound(1), CInt(Text2.Text), _
    
                     CInt(Text1.Text), _
    
                     XORDER_ASCEND, XCOMP_EQ, XTYPE_NUMBER)
    
     
    
    ' Successful Find will return a row number
    
    ' Set focus to the row
    
        If RowFound >= 0 Then TDBList1.Bookmark = RowFound
    
        TDBList1.SetFocus
    
    End Sub
    
  5. Add the following code to the HeadClick and FootClick events:

    Example Title
    Copy Code
    Private Sub TDBList1_HeadClick(ByVal ColIndex As Integer)
    
    ' Ascending sort
    
        x.QuickSort x.LowerBound(1), x.UpperBound(1), ColIndex, _
    
            XORDER_ASCEND, XTYPE_INTEGER
    
        TDBList1.Refresh
    
    End Sub
    
    Private Sub TDBList1_FootClick(ByVal ColIndex As Integer)
    
    ' Descending sort
    
        x.QuickSort x.LowerBound(1), x.UpperBound(1), ColIndex, _
    
            XORDER_DESCEND, XTYPE_INTEGER
    
        TDBList1.Refresh
    
    End Sub
    

Run the program and observe the following:

This concludes Tutorial 17.

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback