To search for entries in a column using an incremental search, add a Timer component to the form, then set the KeyPress and Tick events.
Complete the following steps:
In the Designer
Locate the Interval property for Timer1 in the Properties window and set it to 1000.
In Code
Add the following code to the Form_Load event:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Me.Timer1.Interval = 1000 |
To write code in C#
C# |
Copy Code
|
---|---|
this.timer1.Interval = 1000; |
Declare the search string variable at the form level:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Dim searchString As String = String.Empty |
To write code in C#
C# |
Copy Code
|
---|---|
string searchString = string.Empty; |
Add the KeyPress event:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub C1TrueDBGrid1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles C1TrueDBGrid1.KeyPress ' Handle the keystroke. e.Handled = True Me.searchString += e.KeyChar Dim count As Integer = Me.C1TrueDBGrid1.Splits(0).Rows.Count Dim start As Integer = Me.C1TrueDBGrid1.Row Dim current As Integer = (start + 1) Mod count ' Stop if search returns to the starting position. While current <> start ' Get the value. Dim s As String = Me.C1TrueDBGrid1(current, Me.C1TrueDBGrid1.Col).ToString() ' If a match is found, exit. If s.Substring(0, Me.searchString.Length).ToUpper() = Me.searchString.ToUpper() Then Exit While End If ' Search the next row, wrapping the column if needed. current = (current + 1) Mod count End While ' Update the grid's current row. Me.C1TrueDBGrid1.Row = current ' Highlight the entry. Me.C1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.HighlightCell ' Clear the search string at 1 second. Me.Timer1.Enabled = True End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void c1TrueDBGrid1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { // Handle the keystroke. e.Handled = true; this.searchString += e.KeyChar; int count = this.c1TrueDBGrid1.Splits[0].Rows.Count; int start = this.c1TrueDBGrid1.Row; int current = (start + 1) % count; // Stop if search returns to the starting position. while( current != start ) { // Get the value. string s = this.c1TrueDBGrid1[current, this.c1TrueDBGrid1.Col].ToString(); // If a match is found, exit. if( s.Substring(0, this.searchString.Length).ToUpper() == this.searchString.ToUpper() ) break; // Search the next row, wrapping the column if needed. current = (current + 1) % count; } // Update the grid's current row. this.c1TrueDBGrid1.Row = current; // Highlight the entry. this.c1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.HighlightCell; // Clear the search string at 1 second. this.timer1.Enabled = true; } |
Add the Tick event for the timer:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Me.searchString = String.Empty Me.Timer1.Enabled = False End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void timer1_Tick(object sender, System.EventArgs e) { this.searchString = string.Empty; this.timer1.Enabled = false; } |
As the user types, the search will highlight the cell containing that letter. In this example, tying V in the Last column highlights "Varese".
If more than one entry begins with the same letter, typing the next letter will highlight the entry with those letters. For example, typing Viv in the Last column will highlight "Vivaldi":