Anda di halaman 1dari 10

Membuat jam&tanggal sesuai system OS

Pada contoh kali ini saya akan membuat program yang memanfaatkan salah satu fungsi
string pada pemrograman Visual Basic 6.0 yaitu membuat waktu, baiklah langkah
pertama susun dulu tool-tool yang dibutuhkan, susun seperti gambar dibawah ini:

Tool Properti Nilai


Form1 Caption Form1
Label1 Nama Label1
  Caption -
Label2 Nama Label2
  Caption -
Timer1 Nama Timer1
  Interval 1000
  Enabled True

Lalu CoPas semua listing program seperti dibawah ini:

Private Sub Form_Load()


Label1.Caption = Format(Date, "DD:MM:YYYY")
End Sub
Private Sub Timer1_Timer()
Label2.Caption = Format(Now, "hh:mm:ss")
End Sub

Selesai Dan jalankan programnya..

Membuat save file dengan format .txt


 

Pada contoh kali ini saya akan menjelaskan cara membaca dan menulis ke
file di dalam Visual Basic. Perogram ini hampir sama dengan notepad, tapi
tanpa ANSII, Dan data dapat kita simpan secara permanent ke dalam disk,
langsung pada pembahasannya. Langkah pertama buka program Visual
Basic 6.0, Lalu setelah terbuka tekan tombol Ctrl+T pada keyboard maka
akan keluar Component, Lalu cari dan centang Microsoft Common Dialog
Control 6.0 lalu OK, Setelah itu desaign tampilan form sebagai berikut ini:

Tool Properti Nilai


Form1 Caption Form1
Text1 Nama txt
  Text Kosongkan
Command1 Nama cmdOpen
  Caption Open
Command2 Nama cmdNew
  Caption New
Command3 Nama cmdSave
  Caption Save
Common
Nama CommonDialog1
Dialog1

Lalu CoPas seluruh coding di bawah ini:

Private Sub cmdOpen_Click()


Dim str As String
Dim out As String
out = ""
CommonDialog1.Filter = "Text File | *.txt"
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
Open CommonDialog1.FileName For Input As #1
Do Until EOF(1)
Input #1, str
out = out & str & Chr(13) & Chr(10)
Loop
Close #1
txt.Text = out
End If
End Sub

Private Sub cmdNew_Click()


txt.Text = ""
End Sub

Private Sub cmdSave_Click()


CommonDialog1.Filter = "Text file | *.txt"
CommonDialog1.ShowSave
If CommonDialog1.FileName <> "" Then
Open CommonDialog1.FileName For Output As #2
Print #2, txt.Text
Close #2
End If
End Sub

Selesai,,dan lihat hasilnya..

 
Membuat teks berjalan di form
 

Pada tutorial kali ini hanya tambahan iseng-iseng dari saya, namun lumayan lah
manfaatnya, yaitu membuat teks di form berjalan sepeti animasi,, Dengan memansaatkan
event timer yang ada di Visual Basic 6.0, Langsung saja pada pembahasannya, pertama
buka Visual Basic 6.0 nya dan pilih Standart Exe, Lalu Isi toolnya seperti ini:

Tool Properti Nilai


Form1 Caption Form1
Label1 Nama Label1
Timer1 Nama Timer1
  Inteval 5

Lalu buka listing program dan CoPas listingnya menjadi seperti ini:

Private Sub Timer1_Timer()


Label1.Caption = "Selamat Datang Di Program Saya "
Label1.Font = "Arial"
Label1.FontSize = 12
Label1.FontBold = True
Label1.ForeColor = vbRed
If (Label1.Left + Label1.Width) <= 0 Then
Label1.Left = Me.Width
End If
Label1.Left = Label1.Left - 12
End Sub

Lalu jalankan dan lihat hasilnya. Terima kasih..

Transfer Control Protocol (TCP)

The Transfer Control Protocol (TCP) is used to create and maintain a connection to a
remote computer. To set the Winsock control to TCP, set Winsock's Protocol Property to
"sckTCPProtocol". The computers are able to share information through the
connection. The Client must know the Server's name or Internet Protocol IP address,
set in the RemoteHost Property of the Winsock control. The Client must also know the
Port number on which the Server will be listening, set in the RemotePort Property of the
Winsock control.
If tcpClient.RemoteHost = "" Then
     tcpClient.RemoteHost = "localhost"
End If
tcpClient.RemotePort = 3456
tcpClient.Connect

The LocalPort Property must be set on the Server to specify which Port it is listening to.

tcpServer.LocalPort = 3456
tcpServer.Listen

When the Client requests a connection, the ConnectionRequest Event occurs. The
Accept method is used within the ConnectionRequest event to complete the connection.

When connected, information is sent and received using the SendData and GetData
methods. When data is received, the DataArrival Event occurs.

SIMPLE TCP CHAT PROGRAM

The following describes how to create a simple TCP based chat program. The program
consists of two parts, a server and a client.

TCP CHAT SERVER

The Server Application has a Winsock control named tcpServer using the Protocol
sckTCPProtocol, and two TextBoxes, named txtOutput and txtSend. txtOutput's
MultiLine Property is set to True.

Option Explicit
Private Sub Form_Load()
     txtSend.Enabled = False
     txtOutput.Enabled = False
     tcpServer.LocalPort = 3456
     tcpServer.Listen
End Sub
Private Sub Form_Unload(Cancel As Integer)
     tcpServer.Close
End Sub
Private Sub tcpServer_Close()
     txtSend.Enabled = False
     txtOutput.Enabled = False
     tcpServer.Close
     txtOutput.Text = txtOutput.Text & "Client closed connection"
& vbCrLf & vbCrLf
     txtOutput.SelStart = Len(txtOutput.Text)
     tcpServer.Listen
End Sub
Private Sub tcpServer_ConnectionRequest(ByVal requestID As
Long)
     If tcpServer.State <> sckClosed Then
          tcpServer.Close
     End If
     txtSend.Enabled = True
     txtOutput.Enabled = True
     tcpServer.Accept requestID
     txtOutput.Text = tcpServer.RemoteHostIP & ":" & _
     tcpServer.RemotePort & _
     " connected" & vbCrLf & vbCrLf
End Sub
Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
     Dim msg As String
     tcpServer.GetData msg
     txtOutput.Text = txtOutput.Text & msg & vbCrLf
     txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpServer_Error(ByVal Number As Integer,
Description As String, _
     ByVal Scode As Long, ByVal Source As String, ByVal
HelpFile As String, _
     ByVal HelpContext As Long, CancelDisplay As Boolean)
     MsgBox Source & ": " & Description, vbExclamation,
"TCP/IP Error"
End Sub
Private Sub txtOutput_GotFocus()
     If txtOutput.Enabled = True Then
          txtSend.SetFocus
     End If
End Sub
Private Sub txtSend_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
          KeyAscii = 0
          tcpServer.SendData "SERVER: " & txtSend.Text
          txtOutput.Text = txtOutput.Text & "SERVER: " &
txtSend.Text & vbCrLf
          txtOutput.SelStart = Len(txtOutput.Text)
          txtSend.Text = ""
     End If
End Sub

TCP CHAT CLIENT:

The Client Application has a Winsock control named tcpClient using the Protocol
sckTCPProtocol, and two TextBoxes named txtOutput and txtSend. txtOutput's
MultiLine Property is set to True.

Option Explicit
Private Sub Form_Load()
     Dim strPrompt As String

     strPrompt = "Enter the IP address of the server"


     txtSend.Enabled = False
     txtOutput.Enabled = False
     tcpClient.RemoteHost = InputBox(strPrompt, "IP Address",
"127.0.0.1")
     If tcpClient.RemoteHost = "" Then
          tcpClient.RemoteHost = "localhost"
     End If
     tcpClient.RemotePort = 3456
     tcpClient.Connect
End Sub
Private Sub Form_Unload(Cancel As Integer)
     tcpClient.Close
End Sub
Private Sub tcpClient_Close()
     txtSend.Enabled = False
     txtOutput.Enabled = False
     tcpClient.Close
     txtOutput.Text = txtOutput.Text & "Server closed
connection" & vbCrLf & vbCrLf
     txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpClient_Connect()
     txtSend.Enabled = True
     txtOutput.Enabled = True
     txtOutput.Text = tcpClient.RemoteHostIP & _
     ":" & tcpClient.RemotePort & _
     " connected" & vbCrLf & vbCrLf
End Sub
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
     Dim msg As String
     tcpClient.GetData msg
     txtOutput.Text = txtOutput.Text & msg & vbCrLf
     txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub tcpClient_Error(ByVal Number As Integer,
Description As String, _
ByVal Scode As Long, ByVal Source As String, ByVal HelpFile
As String, _
ByVal HelpContext As Long, CancelDisplay As Boolean)
     MsgBox Source & ": " & Description, vbExclamation,
"TCP/IP Error"
End Sub
Private Sub txtOutput_GotFocus()
     If txtOutput.Enabled = True Then
          txtSend.SetFocus
     End If
End Sub
Private Sub txtSend_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
          KeyAscii = 0
          tcpClient.SendData "CLIENT: " & txtSend.Text
          txtOutput.Text = txtOutput.Text & "CLIENT: " &
txtSend.Text & vbCrLf
          txtOutput.SelStart = Len(txtOutput.Text)
          txtSend.Text = ""
     End If
End Sub

TCO/IP APPLICATION:

USER DATAGRAM PROTOCOL (UDP)

The User Datagram Protocol (UDP) is a Connectionless protocol and unlike TCP does
not establish a connection. To set the Winsock control to UDP, set Winsock's Protocol
Property to sckUDPProtocol. The computers are able to share information through the
connection. Data is sent by setting the Client's LocalPort property and the Server's
RemoteHost and RemptePort Properties to the Internet address and LocalPort Property of
the Client.

Data is sent and received using the SendData and GetData methods. When data is
received, the DataArrival Event occurs.

SIMPLE UDP CHAT PROGRAM:


The following describes how to create a simple UDP based chat program. Like the
TCP chat program, this example is consists of a server and a client.

UDP CHAT SERVER:

The Server Application has a Winsock control named udpServer using the Protocol
sckUDPProtocol, and two TextBoxes named txtOutput and txtSend. txtOutput's
MultiLine Property is set to True.

Option Explicit
Private Sub Form_Load()
     udpServer.Bind 3456
End Sub
Private Sub Form_Unload(Cancel As Integer)
     udpServer.Close
End Sub
Private Sub udpServer_DataArrival(ByVal bytesTotal As Long)
     Dim msg As String
     udpServer.GetData msg
     txtOutput.Text = txtOutput.Text & udpServer.RemoteHostIP
     txtOutput.Text = txtOutput.Text & ":" &
udpServer.RemotePort
     txtOutput.Text = txtOutput.Text & " " & msg & vbCrLf
     txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub txtOutput_GotFocus()
     txtSend.SetFocus
End Sub
Private Sub txtSend_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
          KeyAscii = 0
          udpServer.SendData txtSend.Text
          txtOutput.Text = txtOutput & "Sent: " & txtSend.Text &
vbCrLf
          txtOutput.SelStart = Len(txtOutput.Text)
          txtSend.Text = ""
     End If
End Sub

UDP CHAT CLIENT:

The Client Application has a Winsock control named udpClient using the Protocol
sckUDPProtocol, and two TextBoxes named txtOutput and txtSend. txtOutput's
MultiLine Property is set to True.
Option Explicit
Private Sub Form_Load()
     Dim msg As String
     msg = "Enter the IP address of the server"
     udpClient.RemoteHost = InputBox(msg, "IP Address",
"localhost")
     If udpClient.RemoteHost = "" Then
          udpClient.RemoteHost = "localhost"
     End If
     udpClient.RemotePort = 3456
End Sub
Private Sub Form_Unload(Cancel As Integer)
     udpClient.Close
End Sub
Private Sub udpClient_DataArrival(ByVal bytesTotal As Long)
     Dim msg As String
     udpClient.GetData msg
     txtOutput.Text = txtOutput.Text & udpClient.RemoteHostIP
     txtOutput.Text = txtOutput.Text & ":" &
udpClient.RemotePort
     txtOutput.Text = txtOutput.Text & " " & msg & vbCrLf
     txtOutput.SelStart = Len(txtOutput.Text)
End Sub
Private Sub txtOutput_GotFocus()
     txtSend.SetFocus
End Sub
Private Sub txtSend_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
          KeyAscii = 0
          udpClient.SendData txtSend.Text
          txtOutput.Text = txtOutput.Text & "Sent: " &
txtSend.Text & vbCrLf
          txtOutput.SelStart = Len(txtOutput.Text)
          txtSend.Text = ""
     End If
End Sub

If you don't find what you are looking for. Please click here to submit your query,