All About Visual Basic 6 Programming

Creating a Search Box

Posted by Psycho Programer On Monday, December 13, 2010 0 comments

  1. Click File >> New Project. You might be asked to save changes to the current project if you’ve been experimenting. If so, click the No button. If you want to keep your work, click Yes. Select Standard EXE from the New Project Window

Standard EXE

2. Select the Form and change its property Name to frmSearch  and Caption to Search Box.

3. Insert 2 Text Box, 2 Labels, a Command Button and a Check Box on the form. Arrange the form as shown in figure.Form

4. Set Name Property as

Bigger Text Box Name – txtInput
Smaller Text Box Name – txtSearch
Command Button Name – cmdSearch
Check Box Name – chkCase

Set MultiLine property of txtINput to True.

5. Double-Click the Command Button to open code window and insert the following code.

   1:  Private Sub cmdSearch_Click()
   2:  Dim searchStringLength, inputLength, index As Integer, str, searchString As String
   3:  searchString = Trim(txtSearch.Text) 'Trim Leading and Trailing Spaces
   4:  inputLength = Len(txtInput.Text) 'Length of the Input
   5:  searchStringLength = Len(searchString) 'Length of the Search String
   6:  index = inputLength - searchStringLength + 1
   7:  For i = 1 To index Step 1
   8:      str = Mid(txtInput.Text, i, searchStringLength) 'Extract String of
                                                          'length searchStringLength
   9:      If chkCase Then 'If Check Box is Checked
  10:       If StrComp(searchString, str, vbBinaryCompare) = 0 Then'Case Sensitive Search
  11:          Counter = Counter + 1
  12:       End If
  13:      Else
  14:       If StrComp(searchString, str, vbTextCompare) = 0 Then
  15:          Counter = Counter + 1
  16:       End If
  17:      End If
  18:  Next i
  19:   
  20:  If Counter <> 0 Then 

21: MsgBox "The String " & searchString & " is found " & Counter & " times.",

vbOKOnly, "Result"

  22:      Counter = 0 
  23:  Else
  24:      MsgBox "No string found", vbOKOnly, "Result"
  25:  End If
  26:  End Sub

Variable searchStringLength stores the length of the string to be searched, variable inputLength stores the length of the input string and the line index = inputLength - searchStringLength + 1 sets the value of the variable index .

For example if the input string is “Learning Visual Basic is Interesting”  (without quotes) and search string is basic then,

inputLength = 36

searchStringLength = 5

index = 36 – 5 + 1 = 32

The value of txtSearch Text Box is stored in a string variable searchString by removing leading and trailing spaces using function Trim.

The variable chkCase used in line 9 is the name of the Check Box used in form.  So if the check box is checked then a Case Sensitive search is performed else case insensitive search is performed. The Attributes vbBinaryCompare and vbTextCompare in function strComp is for Case Sensitive and Case Insensitive search respectively.

The For loop goes from 1 to index i.e upto character s of word interesting, extracts 5 (searchStringLength) characters at a time using function Mid() and compares it with a search string (Basic in this example). If a match is found then the variable counter is increased by one. The result is displayed in a Message Box later.

Run the program by pressing F5 from the keyboard. Enter Input String, Search String and click Search Button.

Search 1

6. Check Check Box for Case Sensitive search.

Case Sensitive

For String Handling Funtions Click Here

0 comments:

Post a Comment