ComponentOne List for WinForms
In This Topic
    Tutorial 17 – Conduct Searching using the Find Method
    In This Topic

    In this tutorial you will utilize the Find method in C1List. The Find method allows you to perform custom searches within the control.

    1. Create a new .NET 2.0 project.
    2. Double-click the C1List icon in Visual Studio's Toolbox to add it to the Form. Note: See Adding the C1List Components to a Project for information on adding a component to the Toolbox.
    3. From the Visual Studio Toolbox, place the following controls on the form as shown in the illustration below:
      • Three C1Combo boxes (C1Combo1, 2 and 3)
      • A text box (TextBox1)
      • Two command buttons (Button1, 2)
      • Four labels

    4. Go to the DataSource property for C1List and select Add Project Data Source from the drop-down. In the adapter's Data Source Configuration Wizard, either select a connection to C1NWind.mdb or create a new connection to this database. On the Choose your database objects page of the wizard, select all fields in the Customers table and type "DsCustomers" into the DataSet name box, and then finish out the wizard.
    5. 5. Define some variables at the Form level:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim matchCompare As C1.Win.C1List.MatchCompareEnum
      Dim fromStart As Boolean
      

      To write code in C#

      C#
      Copy Code
      C1.Win.C1List.MatchCompareEnum matchCompare;
      bool fromStart;
      

      To connect to the datasource and fill the three C1Combo boxes with values at run time, add the following code to the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          ' The following line is added by Visual Studio 2005.    
          Me.CustomersTableAdapter.Fill(Me.DsCustomers.Customers)
      
          ' Fill Combo1.    
          With Me.C1Combo1    
       .DataMode = C1.Win.C1List.DataModeEnum.AddItem   
       .AddItem("Company")   
       .AddItem("Contacted")    
       .AddItem("CustType")    
       .AddItem("FirstName")    
       .AddItem("LastName")    
       .AddItem("Phone")    
       .AddItem("UserCode")    
       .SelectedIndex = 0    
          End With
      
          ' Fill Combo2.    
          With Me.C1Combo2    
       .DataMode = C1.Win.C1List.DataModeEnum.AddItem    
       .AddItem("Partial Include")    
       .AddItem("Equal")    
       .AddItem("Less Than")    
       .AddItem("Greater Than")    
       .SelectedIndex = 0   
          End With
      
          ' Fill Combo3.    
          With Me.C1Combo3    
       .DataMode = C1.Win.C1List.DataModeEnum.AddItem    
       .AddItem("Start From Beginning")    
       .AddItem("Start After Current Row")   
       .SelectedIndex = 0   
          End With
      
          Me.TextBox1.Text = ""    
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void Form1_Load( System.object sender, System.EventArgs e) 
      { 
          // The following line is added by Visual Studio 2005.    
          this.CustomersTableAdapter.Fill(this.DsCustomers.Customers);
      
          // Fill Combo1.    
          this.c1Combo1.DataMode = C1.Win.C1List.DataModeEnum.AddItem;    
          this.c1Combo1.AddItem("Company");    
          this.c1Combo1.AddItem("Contacted");    
          this.c1Combo1.AddItem("CustType");    
          this.c1Combo1.AddItem("FirstName");    
          this.c1Combo1.AddItem("LastName");    
          this.c1Combo1.AddItem("Phone");    
          this.c1Combo1.AddItem("UserCode");    
          this.c1Combo1.SelectedIndex = 0;    
       
          // Fill Combo2.    
          this.c1Combo2.DataMode = C1.Win.C1List.DataModeEnum.AddItem;    
          this.c1Combo2.AddItem("Partial Include");    
          this.c1Combo2.AddItem("Equal");
      
          
          this.c1Combo2.AddItem("Less Than");    
          this.c1Combo2.AddItem("Greater Than");    
          this.c1Combo2.SelectedIndex = 0;
      
          // Fill Combo3.    
          this.c1Combo3.DataMode = C1.Win.C1List.DataModeEnum.AddItem;    
          this.c1Combo3.AddItem("Start From Beginning");    
          this.c1Combo3.AddItem("Start After Current Row");    
          this.c1Combo3.SelectedIndex = 0;
      
          this.TextBox1.Text = "";    
      }
      

    6. To handle the Button1_Click event, add the following code:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          ' If there is no search string, do nothing.
          If Me.TextBox1.Text.Trim() = "" Then
      Exit Sub    
          End If  
          Select Case Me.C1Combo2.Text    
       Case "Partial Include"    
           matchCompare = C1.Win.C1List.MatchCompareEnum.PartiallyEqual    
       Case "Equal"    
           matchCompare = C1.Win.C1List.MatchCompareEnum.Equal    
       Case "Less Than"    
           matchCompare = C1.Win.C1List.MatchCompareEnum.LessThan    
       Case "Greater Than"    
           matchCompare = C1.Win.C1List.MatchCompareEnum.GreaterThan    
          End Select
      
          Select Case Me.C1Combo3.Text    
       Case "Start From Beginning"    
           Me.fromStart = True    
       Case "Start After Current Row"    
           Me.fromStart = False    
          End Select
      
          Dim found As Integer    
          If Me.fromStart Then    
       found = Me.C1List1.Find(Me.TextBox1.Text.Trim(), Me.matchCompare, True, 0, Me.C1Combo1.Text)
          Else    
       found = Me.C1List1.Find(Me.TextBox1.Text.Trim(), Me.matchCompare, False, Me.C1List1.Bookmark, Me.C1Combo1.Text)    
          End If
      
          If found >= 0 Then    
       Me.C1List1.SelectedIndex = found    
          Else   
       MessageBox.Show("No further record is found", "List")    
          End If    
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void Button1_Click( System.object sender, System.EventArgs e)    
      {     
          // If there is no search string, do nothing.    
          if ( this.TextBox1.Text.Trim() == "" )    
          {    
       return;    
          }   
       
          switch (this.c1Combo2.Text)    
          {    
       case "Partial Include":    
           matchCompare = C1.Win.C1List.MatchCompareEnum.PartiallyEqual;    
           break;    
       case "Equal":    
           matchCompare = C1.Win.C1List.MatchCompareEnum.Equal;    
           break;    
       case "Less Than":    
           matchCompare = C1.Win.C1List.MatchCompareEnum.LessThan;    
           break;    
       case "Greater Than":    
           matchCompare = C1.Win.C1List.MatchCompareEnum.GreaterThan;    
       break;    
          }    
       
          switch (this.C1Combo3.Text)    
          {   
       case "Start From Beginning":    
           this.fromStart = true;    
           break;    
       case "Start After Current Row":   
           this.fromStart = false;    
           break;   
          }
      
          int found;    
          if ( this.fromStart )    
          {    
       found = this.C1List1.Find(this.TextBox1.Text.Trim(), this.matchCompare, true, 0, this.C1Combo1.Text);    
          }    
          else    
          {    
       found = this.c1List1.Find(this.TextBox1.Text.Trim(),    
       this.matchCompare, false, this.C1List1.Bookmark, this.C1Combo1.Text);    
          }  
          if ( found >= 0 )    
          {    
       this.c1List1.SelectedIndex = found;    
          }    
          else    
          {    
       MessageBox.Show("No further record is found", "List");    
          }    
      }
      
    7. Finally, add the code below to clear the selected rows:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
          Me.C1List1.ClearSelected()
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void Button2_Click( System.object sender, System.EventArgs e)
      {     
          this.c1List1.ClearSelected();    
      }
      

    Run the program and observe the following:

    Set the Column box to LastName and the Compare Mode box to Partial Include. Then place a letter in the Search String box and press the Find button. Notice how the first item in the LastName column beginning with the letter you entered is found. 

    Note: When C1Combo has the focus, press F4 to open the drop-down box.

    Next, set the Search Pos box to Start After Current Row and press the Find button. Notice how the next item in the LastName column beginning with the letter you entered is found.

    The Find method will also let you search numeric strings. Set the Column box to Contacted and the Compare Mode box to Partial Include. Then place a date from the Contacted column in the Search String box and press the Find button. Notice how the first item in the Contacted column with the date you entered is found.

    This concludes the tutorial.