Visual Basic 6.0

All About Visual Basic 6 Programming

Creating a Stop Watch

Posted by Psycho Programer On Saturday, December 25, 2010 2 comments

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

Select the Form, change its Caption property to Stop Watch  and MaxButton to False.

Insert 6 TextBox, a Command Button and a Timer on the form. Arrange the form as shown in figure. The text Hour(s), Minute(s) and Second(s)  are written using a small text box  inside a bigger TextBox. The name of the Bigger TextBox are txtHour, txtMinute and txtSecond  respectively to display Hour, Minute and Second. The Smaller Text Box inside txtHour, txtMinute and txtSecond is named as txtHr, txtMin and txtSec repectively.

Name Command Button as cmdStartStop and Timer as tmrTimer. Set the Caption property of cmdStartStop to Start/Stop.

Design

Now, Double-Click on the Form to open code window with Form Load event. Copy the following code inside the Form Load event to change Font, FontSize, Alignment and other properties.

   1:  Private Sub Form_Load()
   2:  txtHour.Font = "Lucidia Console"
   3:  txtHour.FontSize = 55
   4:  txtHour.Alignment = vbCenter
   5:  txtHour.Text = "0"
   6:  txtHour.Enabled = False
   7:  txtHr.FontBold = True
   8:   
   9:  txtMinute.Font = "Lucidia Console"
  10:  txtMinute.FontSize = 55
  11:  txtMinute.Alignment = vbCenter
  12:  txtMinute.Text = "0"
  13:  txtMinute.Enabled = False
  14:  txtMin.FontBold = True
  15:   
  16:  txtSecond.Font = "Lucidia Console"
  17:  txtSecond.FontSize = 55
  18:  txtSecond.Alignment = vbCenter
  19:  txtSecond.Text = "0"
  20:  txtSecond.Enabled = False
  21:  txtSec.FontBold = True
  22:   
  23:  tmrTimer.Enabled = False
  24:  End Sub

The statement on line number 5, 12 and 19 sets the text of the TextBox to Zero and the statement on line number 6, 13 and 20 disable the controls so that user cannot access these controls. Line #23 enables the Timer.

Set the Interval property of Timer tmrTimer to 1000 and paste the following code on the Timer event of tmrTimer. The code inside this Timer event will execute in every 1 second as defined by its Interval property.

   1:  Private Sub tmrTimer_Timer()
   2:  txtSecond.Text = txtSecond.Text + 1
   3:  If txtSecond.Text > 59 Then
   4:   txtMinute.Text = txtMinute.Text + 1
   5:   txtSecond.Text = "0"
   6:   If txtMinute.Text > 59 Then
   7:    txtHour.Text = txtHour.Text + 1
   8:    txtMinute.Text = "0"
   9:   End If
  10:  End If
  11:  End Sub

The statement on line #2 increase the text of the txtSecond by 1 (Every Second). When the text on txtSecond is greater than 59, the text of txtMinute TextBox is increased by 1 and the txtSecond is reset to Zero. Similarly when the text on txtMinute is greater than 59, the text of txtHour is increased by 1 and the txtMinute is reset to Zero.

Now, Double-Click the cmdStartStop button and write following code.

   1:  Private Sub cmdStartStop_Click()
   2:  If tmrTimer.Enabled Then
   3:   tmrTimer.Enabled = False
   4:  Else
   5:   tmrTimer.Enabled = True
   6:  End If
   7:  End Sub

The above line of statements disable the Timer if it is enabled and vice-versa.

Run the program by pressing F5 from the keyboard. Click Start/Stop button to Start and Stop the Stop Watch.

Run

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