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

String Related Functions

Posted by Psycho Programer On Friday, December 10, 2010 0 comments

Visual Basic 6.0 has various string handling functions for different string related operations. These functions can be used to solve different string related problems without using loops. Some of the string related functions are listed below:

Function

Description

Return Type

Len (String) Returns the Length of the string. Integer
LenB (String) Returns the number of bytes in a string. Integer
Asc (String) Returns the ASCII value of the first character in String. Integer
Chr (Integer) Returns a String with the corresponding ASCII code. String
Left$ (String, x)  Left (String, x) Returns x characters from the start of String. String
Right$ (String,x)
Right (String, x)
Returns x characters from the end of String. String
Mid$(String, x, y)
Mid (String, x, y)
Returns y characters from String, starting at the x'th character. String
Cstr (x) Returns the string representation of x (localized). String
Str$ (x)
Str (x)
Returns the string representation of x (not localized). String
UCase$ (String)
UCase (String)
Coverts String to Upper Case. String
LCase$ (String)
LCase (String)
Converts String to Lower Case. String
InStr ([start,] string1, string2 [, compare]) Returns a Long specifying the position of one string within another. Long
InStrRev(string1, string2[, start, [, compare]]) Returns a Long specifying the position of one string within another from backward. Long
String$(number,character) Returns a string containing a repeating character string of the length specified. String
StrComp(String1, String2,
 Attribute)
Returns 0 if strings are equal, 1 if String1 sorts after String2 and –1 if String1 sorts ahead of String2. Integer
Replace$ (or Replace)(expression, find, replacewith[, start[, count[, compare]]]) Returns a string in which a specified substring has been replaced with another substring a specified number of times. String
StrReverse$ (string)
StrReverse (string)
Reverse the String. String
LTrim$ (String)
LTrim (String)
Removes leading blank spaces from a string. String
RTrim$ (String)
RTrim (String)
Removes trailing blank spaces from a string. String
Trim$ (String)
Trim (String)
Removes both leading and trailing blank spaces from a string. String
Split(expression[, delimiter[, count[, compare]]]) Splits a string into separate elements based on a delimiter (such as a comma or space) and stores the resulting elements in a zero-based array zero-based, one-dimensional array containing a specified number of substrings.
Join (list[, delimiter]) Joins (Concatenates) elements of an array into an output string. String
Filter(InputStrings, Value[, Include[, Compare]]) Returns a zero-based array containing subset of a string array based on a specified filter criteria. String

The Attribute Field of StrComp Function takes

Binary : Performs a binary comparison, based on a sort order derived from the internal binary representations of the characters.

Text : Performs a text comparison, based on a case-insensitive text sort order determined by your application's current culture information.

Try following piece of Code. This Function will Trim all spaces from the sentence.

 
   1:  Function TrimAllSpaces(data As String)
   2:  Dim str As String
   3:  Dim length As Integer
   4:  data = Trim(data)
   5:  length = Len(data)
   6:  For i = 1 To length Step 2
   7:      str = str & Trim(Mid(data, i, 2))
   8:  Next i
   9:  lblOutput.Caption = str
  10:  End Function

The 4th line data = Trim(data) trims all leading and trailing spaces and store the result to data. The 5th line stores the length of the string (data) to variable length. The 7th line str = str & Trim(Mid(data, i, 2)) extracts 2 characters from the ith position of the string, trim leading and trailing spaces and concatenate the result to variable str.

Call function TrimAllSpaces on Click event of Button Trim

   1:  Private Sub cmdTrim_Click()
   2:  TrimAllSpaces txtInput.Text
   3:  End Sub

Run the project , enter a string of your choice and click the "Trim" button.

The screen shot shows a run of the project using the code above.

Trim

Using Function UCase, LCase and StrReverse

Function to convert Lower Case to Upper Case

   1:  Function upperCase(data As String)
   2:  data = UCase(data)
   3:  lblOutput.Caption = data
   4:  End Function

Function to convert Upper Case to Lower Case

   1:  Function lowerCase(data As String)
   2:  data = LCase(data)
   3:  lblOutput.Caption = data
   4:  End Function

Function to reverse a string

   1:  Function stringReverse(data As String)
   2:  data = StrReverse(data)
   3:  lblOutput.Caption = data
   4:  End Function

Linking Two Forms

Posted by Psycho Programer On Monday, December 6, 2010 3 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 frmFirst  and Caption to First.

3. Double-Click the Command Button control in the toolbox to create a command button of default size in the center of the form. Drag the button near the bottom center of the form.

4. Select Command Button and change its Name property to cmdSecond and Caption to &Second Form.

Form First

5. Now Right-Click Project1 (Project1) from Project Window, select Add >> Form to add a New Form.

Add Form

6. Select Form and click Open.

Add

7. Select the Form and change its property Name to frmSecond  and Caption to Second.

8. Double-Click the Command Button control in the toolbox to create a command button of default size in the center of the form. Drag the button near the bottom center of the form.

9. Select Command Button and change its Name property to cmdFirst and Caption to &First Form

Second Form

10. Now double-click the cmdSecond  button from frmFirst. Double-clicking a control (or a form) opens the Code window at the default event from the control. The default event for a command button is the Click event. This means that any code entered in the procedure will be executed when the user clicks the cmdFirst button.

Add the following lines of code between Private Sub and End Sub :

   1:  frmSecond.Show
   2:  Unload Me

11. The line of code frmSecond.Show  tells the program to show frmSecond (Second Form we created earlier)  and Unload Me  tells the form to unload itself whenever the user clicks the cmdFirst button.

cmdSecond_Click

12. Double click frmSecond (frmSecond) from the Project Window. Double-click the cmdFirst button to open the code window.

Add the following lines of code between Private Sub and End Sub :

   1:  frmFirst.Show
   2:  Unload Me

13. The line of code frmFirst.Show  tells the program to show frmFirst (First Form we created) and Unload Me tells the form to unload itself whenever the user clicks the cmdSecond button.

cmdFirst_Click

14 Save the project by pressing Ctrl + S from the keyboard or by clicking File >> Save and Run the program by pressing F5 from the keyboard. You should see a form, similar to the one in figure below.

Runtime

15. Click button Second Form. This will show second form as shown in figure below and unload itself. Click button First Form to go back to First Form.

After Click

Creating Your First Visual Basic Applet : A Hello World Application

Posted by Psycho Programer On Sunday, December 5, 2010 0 comments

The Hello World  program is historically the first application when one learns a new programming language. While very simple, it gives you tangible results with a minimal amount of coding, and it will give you a feel for working with Visual Basic and using IDE environment.

To create the Hello World application, follow these steps:

  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.
Form

2. Resize the form by dragging its borders.

3. Select Form and change its property Name to frmHelloWorld and Caption to My First Application.

Property

4. Double-Click the Command Button control in the toolbox to create a command button of default size in the center of the form. Drag the button near the bottom center of the form.

5. Double-Click the Label control in the toolbox to create a label on the form. Drag the label so it sits just on the top of the Command Button.

Controls

6. Select Label on the form and change its Name property to lblHelloWorld, AutoSize property to True and Caption to Hello World.

7. Similarly select Command Button and change its Name property to cmdOk and Caption to Ok.

Name

8. Now double-click the cmdOk button. Double-clicking a control (or a form) opens the Code window at the default event from the control. The default event for a command button is the Click event. This means that any code entered in the procedure will be executed when the user clicks the cmdOk button.

cmdOk_Click

9. The line of code Unload Me between the Private Sub and End Sub lines tells the form to unload itself whenever the user clicks the cmdOk button. Because this is the only form in the application, it also tells the application to end.

10. Save the project by pressing Ctrl + S from the keyboard or by clicking File >> Save.

11. Click Run >> Start. If you’ve followed the directions correctly, you should see a form, similar to the one in figure below, that simply says “Hello World”.

Hello World

12. Click the Ok button to end. If things go wrong, check through the previous steps to find your mistake.

13. Now click cmdOk button and change the Caption property to &Ok . The ampersand character “&” before the first letter adds an underline to the letter O; this provides a quick keyboard alternative to a mouse-click to activate the button (here it’s Alt + O).

Shortcut

14. Run the program and now you can access the cmdOk button by pressing Alt + O on the keyboard.

HelloWorld

Installing Visual Basic 6.0 Professional on Windows 7

Posted by Psycho Programer On Saturday, December 4, 2010 1 comments

  1. Open Folder Containing Microsoft Visual Basic 6.0 Professional

Folder

2. Find and Click Setup.exe

Click Setup.exe

3. A Program Compatibility Assistant  may appear as shown in figure below. Check Don’t show this message again and then click Run program

Program Compatibility Assitant

4. Click Next on Installation Wizard for Visual Basic 6.0 Professional Edition

Installation Wizard

5. A Program Compatibility Assistant  may appear again as shown in figure below. Check Don’t show this message again and then click Run program

PCA

6. Now, the Visual Basic 6.0 Professional Setup will start as shown in Figure. Click Continue and then Ok

VB Setup

7. Wait until the setup is complete

Loading

8. Restart Windows after the setup is complete

Restart

9. After Restarting your computer, find folder named Microsoft Visual Studio inside C:\Program Files

ProgramFiles

10. Open folder and find VB6 inside VB98 folder

VB6

11. Right Click on VB6 and click Troubleshoot compatibility

Troubleshoot

12. Click Troubleshoot program and then Click Next. Make sure that This program worked in earlier version of Windows but wont install or run now is checked

Troubleshoot Program

13. Select Windows 2000 from Which version of Windows  did this program work on before? list

Win2000

14. Click Start the program

Start

15. Now Click Yes, save these settings for this program and then Close the troubleshooter

Yes

16. Now open the program from C:\Program Files\Microsoft Visual Studio\VB98\VB6  it will run smoothly

Done