Visual Basic - Graphical Component Examples           

   Contact
   C
   C++
   Visual Basic
   Java
   JavaScript
   DHTML
   Style Sheets
   About
   Normalization
   Active X
   TDC Binding
   PHP
   Perl and CGI
   Flash
   XML
   SQL
   Chat
   MCSE
   Linux
   Cabling   
 

   
 
    
    
1. Displaying Text with a label

Private Sub Command1_Click()
Label1.Caption = "Welcome"
Label2.Caption = "Please enter your name below:"
lblDigitalClock.Caption = Time
End Sub


2. Controlling text box with code

Private Sub Command1_Click()
'The following line places text in a text box:
Text1.Text = "Enter your delivery note here."
End Sub


Private Sub Command2_Click()
'This line saves input from a text box in a variable:
DeliveryNote = Text1.Text
End Sub


3. Creating a list

Private Sub Form_Load()
List1.AddItem "Extra hard disk"
List1.AddItem "Printer"
List1.AddItem "Satellite dish"
End Sub

4. Combo box

Private Sub Form_Load()
Combo1.AddItem "U.S. Dollars"
Combo1.AddItem "Check"
Combo1.AddItem "English Pounds"
End Sub


5. Opening Dialog Box

Private Sub mnuOpenItem_Click()
CommonDialog1.Filter = "Metafiles (*.WMF)|*.WMF"
CommonDialog1.ShowOpen
Image1.Picture = LoadPicture(CommonDialog1.FileName)
mnuCloseItem.Enabled = True
End Sub


6. Color Dialog Box

Private Sub mnuTextColorItem_Click()
CommonDialog1.Flags = &H1&
CommonDialog1.ShowColor
Label1.ForeColor = CommonDialog1.Color
End Sub


7. Variables and Assignment

Dim LastName 'declare LastName variable
LastName = "Smart" 'store "Smart" in variable
Label1.Caption = LastName 'display "Smart" in Label1 object

LastName = 99 'now place 99 in variable
Label2.Caption = LastName 'display this value in Label2 obj


8. Switch Statement

Private Sub List1_Click()
'Variable declaration section
Dim Birds%, Loan&, Price!, Pie#, Debt@, Dog$, Total
Dim Flag As Boolean
Dim Birthday As Date

'Select Case processes the user's choice
Select Case List1.ListIndex
Case 0
Birds% = 37
Label4.Caption = Birds%
Case 1
Loan& = 350000
Label4.Caption = Loan&
Case 2
Price! = -1234.123
Label4.Caption = Price!
Case 3
Pie# = 3.1415926535
Label4.Caption = Pie#
Case 4
Debt@ = 299950.95
Label4.Caption = Debt@
Case 5
Dog$ = "German Wire-haired Pointer"
Label4.Caption = Dog$
Case 6 'True is stored as -1 in code, False as 0
Flag = True
Label4.Caption = Flag
Case 7 'Note # symbol and Format function here
Birthday = #11/19/63#
Label4.Caption = Format$(Birthday, "dddd, mmmm dd, yyyy")
Case 8
Price = 99.95
Label4.Caption = Price
End Select
End Sub

9. User Defined Data Types

Type Employee
Name As String
DateOfBirth As Date
HireDate As Date
End Type


10. Multiple If Then Statements

If AdjustedIncome <= 24650 Then '15% tax bracket
TaxDue = AdjustedIncome * 0.15
ElseIf AdjustedIncome <= 59750 Then '28% tax bracket
TaxDue = 3697 + ((AdjustedIncome - 24650) * 0.28)
ElseIf AdjustedIncome <= 124650 Then '31% tax bracket
TaxDue = 13525 + ((AdjustedIncome - 59750) * 0.31)
ElseIf AdjustedIncome <= 271050 Then '36% tax bracket
TaxDue = 33644 + ((AdjustedIncome - 124650) * 0.36)
Else '39.6% tax bracket
TaxDue = 86348 + ((AdjustedIncome - 271050) * 0.396)
End If


11. Select Case Statement

Select Case Age
Case 16
Label1.Caption = "You can drive now!"
Case 18
Label1.Caption = "You can vote now!"
Case 21
Label1.Caption = "You can drink wine with your meals."
Case 65
Label1.Caption = "Time to retire and have fun!"
End Select


12. Select Case with Else

Select Case Age
Case 16
Label1.Caption = "You can drive now!"
Case 18
Label1.Caption = "You can vote now!"
Case 21
Label1.Caption = "You can drink wine with your meals."
Case 65
Label1.Caption = "Time to retire and have fun!"
Case Else
Label1.Caption = "You're a great age! Enjoy it!"
End Select

13. Select Case With Range

Select Case Age
Case Is < 13
Label1.Caption = "Enjoy your youth!"
Case 13 To 19
Label1.Caption = "Enjoy your teens!"
Case 21
Label1.Caption = "You can drink wine with your meals."
Case Is > 100
Label1.Caption = "Looking good!"
Case Else
Label1.Caption = "That's a nice age to be."
End Select


14. Entering Break Mode

Private Sub Command1_Click()
Stop 'enter break mode
Age = Text1.Text

If Age > 13 And Age < 20 Then
Text2.Text = "You're a teenager."
Else
Text2.Text = "You're not a teenager."
End If
End Sub

15. Error Handler

Retries = 0 'initialize counter variable
On Error GoTo DiskError
Image1.Picture = LoadPicture("a:\prntout2.wmf")
Exit Sub 'exit the procedure

DiskError:
MsgBox (Err.Description), , "Loading Error"
Retries = Retries + 1 'increment counter on error
If Retries >= 2 Then
Resume Next
Else
Resume
End If


16. Total Tax Function

Function TotalTax(Cost)
StateTax = Cost * 0.05 'State tax is 5%
CityTax = Cost * 0.015 'City tax is 1.5%
TotalTax = StateTax + CityTax
End Function

17. Entering Names in a list

AddNameToListBox "Kimberly" 'always add two names
AddNameToListBox "Rachel"
Do 'then let user add extra names
NewName$ = InputBox("Enter a list box name.", "Add Name")
AddNameToListBox NewName$
Loop Until NewName$ = ""


18. Text Browser

Private Sub mnuItemClose_Click() 'when user clicks Close command
txtFile.Text = "" 'clear text box
lblFile.Caption = "Load a text file with the Open command."
mnuItemClose.Enabled = False 'dim Close command
mnuItemOpen.Enabled = True 'enable Open command
txtFile.Enabled = False 'disable text box
End Sub

Private Sub mnuItemExit_Click() 'when user clicks Exit command
End 'quit program
End Sub

Private Sub mnuItemOpen_Click() 'when user clicks Open command
Wrap$ = Chr$(13) + Chr$(10) 'create wrap character
CommonDialog1.Filter = "Text files (*.TXT)|*.TXT"
CommonDialog1.ShowOpen 'display Open dialog box
If CommonDialog1.Filename <> "" Then
Form1.MousePointer = 11 'display hour glass
Open CommonDialog1.Filename For Input As #1
On Error GoTo TooBig: 'set error handler
Do Until EOF(1) 'then read lines from file
Line Input #1, LineOfText$
AllText$ = AllText$ & LineOfText$ & Wrap$
Loop
lblFile.Caption = CommonDialog1.Filename
txtFile.Text = AllText$ 'display file
txtFile.Enabled = True
mnuItemClose.Enabled = True
mnuItemOpen.Enabled = False 'enable scroll
CleanUp:
Form1.MousePointer = 0 'reset mouse
Close #1 'close file
End If
Exit Sub
TooBig: 'error handler displays message
MsgBox ("The specified file is too large.")
Resume CleanUp: 'then jumps to CleanUp routine
End Sub

19. Saving a text file

Private Sub mnuItemSave_Click() 'When user clicks Save
'note: the entire file is stored in a string
CommonDialog1.Filter = "Text files (*.TXT)|*.TXT"
CommonDialog1.ShowSave 'display Save dialog
If CommonDialog1.Filename <> "" Then
Open CommonDialog1.Filename For Output As #1
Print #1, txtNote.Text 'save string to file
Close #1 'close file
End If
End Sub


20. Searching Database

prompt$ = "Enter the full (complete) book title."
'get the string to be used in the Title field search
SearchStr$ = InputBox(prompt$, "Book Search")
datBiblio.Recordset.Index = "Title" 'use Title table
datBiblio.Recordset.Seek "=", SearchStr$ 'and search
If datBiblio.Recordset.NoMatch Then 'if no match
MsgBox ("Sorry, I couldn’t find your book.")
datBiblio.Recordset.MoveFirst 'go to first record
End If


21. Adding a Record

prompt$ = "Enter new record, and click left arrow button."
reply = MsgBox(prompt$, vbOKCancel, "Add Record")
If reply = vbOK Then 'if the user clicks OK
txtTitle.SetFocus 'move cursor to Title box
datBiblio.Recordset.AddNew 'and get new record
'set PubID field to 14 (this field is required
datBiblio.Recordset.PubID = 14 'by Biblio.mdb)
End If


22. Delete Method

prompt$ = "Do you really want to delete this record?"
reply = MsgBox(prompt$, vbOKCancel, "Delete Record")
If reply = vbOK Then 'if the user clicks OK
datBiblio.Recordset.Delete 'delete current record
datBiblio.Recordset.MoveFirst 'move to first record
End If


23. Backing Up with FileCopy

prompt$ = "Would you like to create a backup copy of the database?"
reply = MsgBox(prompt$, vbOKCancel, datBiblio.DatabaseName)
If reply = vbOK Then 'copy the database if the user clicks OK
FileNm$ = InputBox$("Enter the pathname for the backup copy.")
If FileNm$ <> "" Then FileCopy datBiblio.DatabaseName, FileNm$
End If


24 . Other

Private Sub mnuTextColorItem_Click()
CommonDialog1.Flags = &H1&
CommonDialog1.ShowColor
Label1.ForeColor = CommonDialog1.Color
End Sub