Академический Документы
Профессиональный Документы
Культура Документы
You can create an audio player that enables you to play midi and wave files in your hard drives
or DVDROM. It allows you to change drives and search for the particular sound file that you
wish to play.
In this program, you have to add a Combo box, a List Box, a Text Box, a Drive List Box and
three command buttons. Change the label of one of the buttons to "PLAY", another command
button to "STOP", and the last command button to "EXIT". Besides, you need to add the
MMControl and make it invisible.
The code
Sub Combo1_Change( )
if ListIndex=0 Then
File1.Pattern=("*.wav")
ElseIf ListIndex=1 Then
File1.Pattern=("*.mid")
Else
File1.Pattern=("*.*)
End If
End Sub
End If
File1.Path=Dir1.Path
If Combo1.ListIndex=0 Then
File1.Pattern=("*.wav")
ElseIf Combo1.ListIndex=1 Then
File1.Pattern=("*.mid")
Else
File1.Pattern=("*.*)
End If
Text1.Text=filenam
End Sub
Dir1.Path=Drive1.Drive
End Sub
Else
filenam=File1.Path+File1.Filename
End Sub
Private Sub Dir1_Change( )
Top=(Screen.Height-Height)\2
Left=(Screen.Width-Width)\2
Combo1.Text="*.wav"
Combo1.AddItem "*.wav"
Combo1.AddItem "*.mid"
Combo1.AddItem "All files"
End Sub
KASUS 2
This is a multimedia player that can play all kinds of media files such as wav, midi, mp3, mpeg
video, avi video and so on. When you launch the program, you can select files of different types
from different drives. After you have selected a particular file, you can play it using the
customized button or you can use the buttons of the multimedia control
In this program, you need to insert the Microsoft Multimedia Control, a combo box, a
dirListBox, a DriveListBox, a fileListbox, a picture box(to play the media files) , a text box and
four command buttons which you label as "Play", "Open","Stop" and "Exit".
The combo box is to allow the user to select different media files from his or her hard drive or
from other sources. The code is:
Private Sub Combo1_Change()
If ListIndex = 0 Then
File1.Pattern = ("*.wav")
ElseIf ListIndex = 1 Then
File1.Pattern = ("*.mid")
ElseIf ListIndex = 2 Then
File1.Pattern = ("*.avi")
ElseIf ListIndex = 3 Then
File1.Pattern = ("*.mpeg;*.mpg;*.mp3")
Else
Fiel1.Pattern = ("*.*")
End If
End Sub
The procedure to allow the user to search for a particular media file in a certain folder is as
follow:
Private Sub File1_Click()
If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.wav")
ElseIf Combo1.ListIndex = 1 Then
File1.Pattern = ("*.mid")
ElseIf Combo1.ListIndex = 2 Then
File1.Pattern = ("*.avi")
ElseIf Combo1.ListIndex = 3 Then
File1.Pattern = ("*.mpeg;*.mpg;*.mp3")
Else
File1.Pattern = ("*.*")
End If
If Right(File1.Path, 1) <> "\" Then
filenam = File1.Path + "\" + File1.FileName
Else
filenam = File1.Path + File1.FileName
End If
Text1.Text = filenam
End Sub
The Interface
The code
Private Sub Form_Load()
Left = (Screen.Width - Width) \ 2
Top = (Screen.Height - Height) \ 2
'To offer the user selection of media files
Combo1.Text = "*.wav"
Combo1.AddItem "*.wav"
Combo1.AddItem "*.mid"
Combo1.AddItem "*.avi"
Combo1.AddItem "*.mpeg;*.mpg;*.mp3"
Combo1.AddItem "All files"
End Sub
Private Sub Combo1_Change()
'To allow the user to select a particular media file type
If ListIndex = 0 Then
File1.Pattern = ("*.wav")
ElseIf ListIndex = 1 Then
File1.Pattern = ("*.mid")
ElseIf ListIndex = 2 Then
File1.Pattern = ("*.avi")
ElseIf ListIndex = 3 Then
File1.Pattern = ("*.mpeg;*.mpg;*.mp3")
Else
Fiel1.Pattern = ("*.*")
End If
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.wav")
ElseIf Combo1.ListIndex = 1 Then
File1.Pattern = ("*.mid")
ElseIf Combo1.ListIndex = 2 Then
File1.Pattern = ("*.avi")
ElseIf Combo1.ListIndex = 3 Then
File1.Pattern = ("*.mpeg;*.mpg;*.mp3")
Else
File1.Pattern = ("*.*")
End If
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub File1_Click()
'Allows the user to select a particular media file
If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.wav")
ElseIf Combo1.ListIndex = 1 Then
File1.Pattern = ("*.mid")
ElseIf Combo1.ListIndex = 2 Then
File1.Pattern = ("*.avi")
ElseIf Combo1.ListIndex = 3 Then
File1.Pattern = ("*.mpeg;*.mpg;*.mp3")
Else
File1.Pattern = ("*.*")
End If
If Right(File1.Path, 1) <> "\" Then
filenam = File1.Path + "\" + File1.FileName
Else
filenam = File1.Path + File1.FileName
End If
Text1.Text = filenam
End Sub
Private Sub Open_Click()
If Combo1.ListIndex = 0 Then
MMControl1.DeviceType = "WaveAudio"
End If
If Combo1.ListIndex = 1 Then
MMControl1.DeviceType = "Sequencer"
End If
If Combo1.ListIndex = 2 Then
MMControl1.DeviceType = "AVIVideo"
End If
If Combo1.ListIndex = 3 Then
MMControl1.DeviceType = ""
End If
MMControl1.FileName = Text1.Text
MMControl1.Command = "Open"
End Sub
Private Sub play_Click()
Timer1.Enabled = True
MMControl1.Command = "Play"
MMControl1.hWndDisplay = Picture1.hWnd
End Sub
Private Sub Stop_Click()
If MMControl1.Mode = 524 Then Exit Sub
If MMControl1.Mode <> 525 Then
MMControl1.Wait = True
MMControl1.Command = "Stop"
End If
MMControl1.Wait = True
MMControl1.Command = "Close"
End Sub
Kasus 3
We can create a simple virtual drawing program using Visual Basic 6 as shown in the
figure below. We called it drawing pad.
In this program, the user needs to fill in all the coordinates and selects a color before
proceeding to draw the required shape. If the user forgets to fill in the coordinates or
selects a color, he or she will be prompted to do so.
To create the drawing pad, you need to insert a common dialog control, a picture box,
four text boxes , six command buttons and the necessary labels. The function of the
common dialog control is to assist the users to choose colors. The text boxes are for the
user to enter the coordinates and the picture box is to display the pictures drawn.
The syntax to draw a straight line is Line, and the syntax is as follows:
Picture1.Line (x1, y1)-(x2, y2), color
where picture1 is the picture box, (x1,y1) is the coordinates of the starting point, (x2,
y2) is the ending point and color understandably is the color of the line.
The syntax to draw a non-solid rectangle is
Picture1.Line (x1, y1)-(x2, y2), color, B
The syntax to draw a solid rectangle is
Picture1.Line (x1, y1)-(x2, y2), color, BF
The syntax to draw a circle is
The Interface
The code
Private Sub cmd_Rectangle_Click()
x1 = Text1.Text
y1 = Text2.Text
x2 = Text3.Text
y2 = Text4.Text
Picture1.Line (x1, y1)-(x2, y2), color, B
End Sub
Kasus 4
This is a program that enables the user to search for an image file in his or her computer and
view it in a picture box. There are two ways to implement the program. The first is to build the
program from the ground up and the second makes use of the common dialog box.
The first way is a little more complicated but you can learn a great deal of programming
techniques. To create this program, you need to insert a a drive list box(DriveListBox) , a
directory list box (DirListBox), a file list box(FileListBox) and a combo box . The drive list box
is for the user to select a drive, the directory list box is for the user to choose a folder and the file
list box is display the files in the selected folder. Besides that, the combo box allows the user to
select all graphics files or all files. You also need to insert a picture box to display the image.
The Interface
The code
Private Sub Combo1_Change()
'To list all graphics files or all files
If ListIndex = 0 Then
File1.Pattern = ("*.bmp;*.wmf;*.jpg;*.gif")
Else
Fiel1.Pattern = ("*.*")
End If
End Sub
Private Sub Dir1_Change()
'To choose drive
File1.Path = Dir1.Path
File1.Pattern = ("*.bmp;*.wmf;*.jpg;*.gif")
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
The second way is much easier to program, you need to insert an image control, a common
dialog box and an icon that resembles on opened file. You need to set the stretchable property of
the image control to true. The procedure to open the common dialog box to browse the image
files as well as to load the selected picture into the image control is
CommonDialog1.Filter = "Bitmaps(*.BMP)|*.BMP|Metafiles(*.WMF)|*.WMF|Jpeg Files(*.jpg)|
*.jpg|GIF Files(*.gif)|*.gif|Icon Files(*.ico)|*.ico|All Files(*.*)|*.*"
CommonDialog1.ShowOpen
Picture1.Picture = LoadPicture(CommonDialog1.FileName)
The filter property of the common dialog box uses the format as shown below
Bitmaps(*.BMP)|*.BMP
to specify the file type, and uses the pipe line | to separate different file types.
Visual Basic supports most of the picture formats namely bmp, wmf, jpg, gif, ico(icon) and
cur(cursor) files.
The command CommonDialog1.ShowOpen is to open the common dialog box and the
command.
Picture1.Picture = LoadPicture (CommonDialog1.FileName)
is to load the selected picture file into the picture box.
The whole program is shown below and the output is shown in the figure below:
Private Sub Image1_Click()
CommonDialog1.Filter = "Bitmaps(*.BMP)|*.BMP|Metafiles(*.WMF)|*.WMF|Jpeg Files(*.jpg)|
*.jpg|GIF Files(*.gif)|*.gif|Icon Files(*.ico)|*.ico|All Files(*.*)|*.*"
CommonDialog1.ShowOpen
image2.Picture = LoadPicture (CommonDialog1.FileName)
End Sub
When the user clicks the opened file icon, the following dialog will appear. The user then can
select the file he or she wish to view.
The Interface
Kasus 5
Blog
1109
google Plus
o
o
o
o
o
o
o
o
expand
This is a trial to read records from MDB database file using TreeView control. My project is a
phone index to display some fields from Customers table in Northwind database file:
CompanyName, ContactName, Address, City, Country and Phone.
My project has one form (frmPhone), this form has following controls:
TreeView: tvData
Four TextBoxes
txtCompany for Company Name, txtAddress for Address, txtCity for City and , txtCountry
for Country.
Two Buttons: For load data and exit application.
Code
There is five procedures:
1. Private Sub LoadCustomerData()
2. ' for open the database file
3.
4.
5.
6.
datCon.ConnectionString = MyCon
7.
8.
9.
10.
Try
11.
MyTable = "Customers"
12.
13.
14.
cmdSelect.CommandText = strSql
15.
cmdSelect.CommandType = CommandType.Text
16.
17.
cmdSelect.Connection = datCon
18.
datAdp.SelectCommand = cmdSelect
19.
20.
datCon.Close()
21.
22.
23.
If RecCount = 0 Then
24.
25.
Exit Sub
26.
27.
28.
End If
Catch ex As Exception
MessageBox.Show(ex.ToString())
29.
End Try
30.
SetRootNode()
31.
End Sub
32.
33.
34.
35.
tvData.Nodes.Add("Phone Index")
36.
tvData.Nodes(0).Tag = "RootDB"
37.
tvData.Nodes(0).ImageIndex = 0
38.
tvData.Nodes(0).SelectedImageIndex = 0
39.
40.
SetContactName()
41.
42.
SetCustPhone()
43.
End Sub
44.
45.Private Sub SetContactName()
46.'set ContactName field as node
47.
itmNumber = datSet.Tables(MyTable).Rows.Count
48.
49.tvData.Nodes(0).Nodes.Add(datSet.Tables(MyTable).Rows(i).ItemArray(2).ToSt
ring())
50.
tvData.Nodes(0).Nodes(i).Tag = "Name"
51.
tvData.Nodes(0).Nodes(i).ImageIndex = 2
52.
tvData.Nodes(0).Nodes(i).SelectedImageIndex = 2
53.
54.
Next
End Sub
55.
56.Private Sub SetCustPhone()
57.'set Phone field as node
58.
59.tvData.Nodes(0).Nodes(i).Nodes.Add(datSet.Tables(MyTable).Rows(i).ItemArra
y(9).ToString())
60.
tvData.Nodes(0).Nodes(i).Nodes(0).Tag = "Phone"
61.
tvData.Nodes(0).Nodes(i).Nodes(0).ImageIndex = 4
62.
tvData.Nodes(0).Nodes(i).Nodes(0).SelectedImageIndex = 4
63.
64.
Next
End Sub
65.
66.
67.
68.
Try
69.
70.
71.
dv.Sort = "ContactName"
72.
73.
Me.BindingContext(datSet, MyTable).Position = i
74.
txtCompany.Text = datSet.Tables(MyTable).Rows(i).ItemArray(1).ToStri
ng()
75.
txtAddress.Text = datSet.Tables(MyTable).Rows(i).ItemArray(4).ToStrin
g()
76.
txtCity.Text = datSet.Tables(MyTable).Rows(i).ItemArray(5).ToString()
77.
txtCountry.Text = datSet.Tables(MyTable).Rows(i).ItemArray(8).ToStrin
g()
78.
Catch ex As Exception
79.
80.
81.
MessageBox.Show(ex.ToString())
End Try
End Sub
Kasus 6
In this program I calculate the approximate age of the moon in days and did not give attention to
the parts of the day of the hours and minutes.
For the program to be more useful, I added a PictureBox control to display the lighted part of the
moon and darkness part of the moon commensurate with the age of the moon.
There is a small probability of a one-day error for any calculation to convert a date.
7. End Function
3.
Dim jd As Double
4.
Dim j, L, n As Double
5.
Dim d, m, y As Integer
6.
7.
8.
d = Val(SolarDay.Text)
9.
10.
m = SolarMonth.SelectedIndex + 1
11.
12.
y = Val(SolarYear.Text)
13.
If (y > 1582) Or ((y = 1582) And (m > 10)) Or ((y = 1582) And (m = 10) An
d (d > 14)) Then
14.
15.
Else
16.
17.
End If
18.
19.
20.
DateMinError()
21.
Exit Sub
22.
End If
23.
24.
25.
DateMaxError()
26.
Exit Sub
27.
End If
28.
29.
theDay = jd Mod 7
30.
lblDay.Text = WeekDays(theDay)
31.
L = jd - 1948440 + 10632
32.
n = getInt((L - 1) / 10631)
33.
L = L - 10631 * n + 354
34.
35.
36.
m = Int(getInt((24 * L) / 709))
37.
38.
y = Int(30 * n + j - 30)
39.
40.
LunarDay.Text = Str(d)
41.
LunarMonth.Text = LunarMonths(m - 1)
42.
LunarYear.Text = Str(y)
43.
ShowMoonPhase()
44.
45.
46.
47.
48.
If d = 1 Then
lblAge.Text = Str(d) & " day"
Else
lblAge.Text = Str(d) & " days"
End If
49.End Sub
3.
Dim jd As Double
4.
Dim i, j, k, L, n As Double
5.
Dim d, m, y As Integer
6.
7.
8.
d = Val(LunarDay.Text)
9.
If d = 1 Then
10.
11.
12.
13.
End If
14.
15.
m = LunarMonth.SelectedIndex + 1
16.
17.
y = Val(LunarYear.Text)
18.
19.
20.
theDay = jd Mod 7
21.
lblDay.Text = WeekDays(theDay)
22.
23.
L = jd + 68569
24.
n = getInt((4 * L) / 146097)
25.
L = L - getInt((146097 * n + 3) / 4)
26.
27.
L = L - getInt((1461 * i) / 4) + 31
28.
j = getInt((80 * L) / 2447)
29.
30.
L = getInt(j / 11)
31.
m = Int(j + 2 - 12 * L)
32.
y = Int(100 * (n - 49) + i + L)
33.
Else
34.
j = jd + 1402
35.
k = getInt((j - 1) / 1461)
36.
L = j - 1461 * k
37.
38.
i = L - 365 * n + 30
39.
j = getInt((80 * i) / 2447)
40.
41.
i = getInt(j / 11)
42.
m = Int(j + 2 - 12 * i)
43.
y = Int(4 * k + n + i - 4716)
44.
End If
45.
46.
SolarDay.Text = Str(d)
47.
SolarMonth.Text = SolarMonths(m - 1)
48.
SolarYear.Text = Str(y)
49.
ShowMoonPhase()
50.End Sub
3.
4.
5.
6.
7.
8.
9.
10.
11.
' Initiate 'ImageToDraw' Object with size = size of control 'PicMoon' control
12.
13.
14.
15.
Dim PenW As Pen = New Pen(Color.White) ' For lighted part of the moon
16.
Dim PenB As Pen = New Pen(Color.Black) ' For darkness part of the moon
17.
For Ypos = 0 To 45
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
Rpos = 2 * Xpos
28.
29.
Xpos1 = -Xpos
30.
31.
Else
32.
Xpos1 = Xpos
33.
34.
End If
35.
36.
37.
38.
39.
40.
41.
42.
Next
43.
44.
Me.MoonShape.Image = ImageToDraw
45.
46.
PenW.Dispose()
47.
PenB.Dispose()
48.
newGraphics.Dispose()
49.
ImageToDraw = Nothing
50.End Sub