Hari ini, saya ingin menunjukkan sedikit trik dalam pemrograman VB.NET, periksa koneksi internet, dapatkan serial hard drive, salin teks dari clipboard, dapatkan resolusi layar, dll.
1. Periksa koneksi internet.
Public Function IsConnectionAvailable() As Boolean
Dim objUrl As New System.Uri("http://www.youtube.com")
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objresp As System.Net.WebResponse
Try
objresp = objWebReq.GetResponse
objresp.Close()
objresp = Nothing
Return True
Catch ex As Exception
objresp = Nothing
objWebReq = Nothing
Return False
End Try
End Function
Gunakan fungsi tes koneksi internet sebagai berikut:
If IsConnectionAvailable() = TrueThen Do Action Here Else Do Action Here End If
2. Dapatkan resolusi layar yang digunakan
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = Screen.PrimaryScreen.WorkingArea.Width
TextBox2.Text = Screen.PrimaryScreen.WorkingArea.Height
End Sub
3. Dapatkan serial hard drive HDD
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim hd As New ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia")
For Each dvs As ManagementObject In hd.Get()
Dim serial As String = dvs("SerialNumber").ToString()
TextBox1.Text = serial
Next
End Sub
4. Jalankan aplikasi dari program
Misalnya, di bawah ini ketika mengklik tombol akan membuka bab saya microsoft paint
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Shell("mspaint")
End Sub
5. Buka file musik di drive
My.Computer.Audio.Play("C:MusicMusic.wav")
6. Salin teks dari clipboard
If My.Computer.Clipboard.ContainsText Then TextBox1.Text = My.Computer.Clipboard.GetText End If
7. Buka bip dari suara sistem windows
My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
8. Buka situs web dari LabelLink
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
System.Diagnostics.Process.Start("http://www.domain.com")
End Sub
9. Hasilkan nomor acak
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim number As Integer
Randomize()
' The program will generate a number from 0 to 50
number = Int(Rnd() * 50) + 1
TextBox1.Text = number
End Sub
10. Tekan Alt + PrintScreen untuk mengambil tangkapan layar dan menyimpan file gambar ke drive
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'send keys with Alt key
SendKeys.SendWait("%({PRTSC})")
My.Computer.Clipboard.GetImage().Save("C:form.jpg")
End Sub
11. Dapatkan informasi Ram dari komputer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TextBox1.Text = My.Computer.Info.TotalPhysicalMemory
TextBox2.Text = My.Computer.Info.AvailablePhysicalMemory
TextBox3.Text = My.Computer.Info.TotalVirtualMemory
TextBox4.Text = My.Computer.Info.AvailableVirtualMemory
End Sub
12. SendKeys
Misalnya, tekan tombol BackSpace.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
My.Computer.Keyboard.SendKeys("{BACKSPACE}")
End Sub
Kode lain yang bisa anda gunakan.
BACKSPACE {BACKSPACE} or {BS}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
CLEAR {CLEAR}
DELETE {DELETE} or {DEL}
DOWN ARROW {DOWN}
END {END}
ENTER (numeric keypad) {ENTER}
ENTER ~ ESC {ESCAPE} or {ESC}
HELP {HELP}
HOME {HOME}
INS {INSERT}
LEFT ARROW {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
RETURN {RETURN}
RIGHT ARROW {RIGHT}
SCROLL LOCK {SCROLLLOCK} T
AB {TAB}
UP ARROW {UP}
F1 ke F15 {F1} ke {F15}
13. fungsi enkripsi SHA1.
Public Function SHA1(ByVal number As String) As String
Dim ASCIIENC As New ASCIIEncoding
Dim strreturn As String
strreturn = vbNullString
Dim bytesourcetxt() As Byte = ASCIIENC.GetBytes(number)
Dim SHA1Hash As New SHA1CryptoServiceProvider
Dim bytehash() As Byte = SHA1Hash.ComputeHash(bytesourcetxt)
For Each b As Byte In bytehash
strreturn &= b.ToString("X8")
Next
Return strreturn
End Function
14. Tampilkan Desktop
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_LWIN = &H5B
Public Sub ShowDesktop()
keybd_event(VK_LWIN, 0, 0, 0)
keybd_event(77, 0, 0, 0)
keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0)
End Sub
15. Dapatkan jalur dari folder khusus misalnya: Desktop
Dim DesktopPath As String = CreateObject("WScript.Shell").Specialfolders(10)
'OR
Dim DesktopPath As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
MsgBox(DesktopPath)
16. Dapatkan event KeyPress saat tombol ditekan
Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If Asc(e.KeyChar) > 1 Then
MsgBox("You have pressed " & e.KeyChar & " key")
e.Handled = True
End If
End Sub
17. Kosongkan semua TextBox didalam form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Control
For Each a In Me.Controls
If TypeOf a Is TextBox Then
a.Text = Nothing
End If
Next
End Sub
18. Hapus nomor dari TextBox
mis: nguyenthao2803 => nguyenthao
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim rep As String = String.Empty
For i As Integer = 0 To TextBox1.Text.Length - 1
If Not IsNumeric(TextBox1.Text.Substring(i, 1)) Then
rep += TextBox1.Text.Substring(i, 1)
End If
Next
TextBox1.Text = rep
End Sub
19. Teks ukuran font otomatis agar sesuai dengan TextBox
Dim h As String
Dim w As String
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim orgFont As New Font(TextBox1.Font.Name, TextBox1.Font.Size, TextBox1.Font.Style)
Dim textSize As New SizeF
textSize = e.Graphics.MeasureString(TextBox1.Text, orgFont)
h = textSize.Height
w = textSize.Width
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TextBox1.Font = New Font(TextBox1.Font.Name, 8, TextBox1.Font.Style)
TextBox1.BorderStyle = BorderStyle.Fixed3D
Do Until w > TextBox1.Size.Width - 5 Or TextBox1.Text = Nothing Or h > 182
TextBox1.Font = New Font(TextBox1.Font.Name, TextBox1.Font.Size + 2, TextBox1.Font.Style)
Loop
TextBox1.BorderStyle = BorderStyle.None
End Sub
20. Hanya boleh input angka di TextBox
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
MessageBox.Show("Please enter numbers only")
e.Handled = True
End If
End Sub
21. Unduh file dari situs web
My.Computer.Network.DownloadFile("http://www.domain.com/file.txt", _
"C:yourpathfile.txt")
22. Unggah file ke situs web
My.Computer.Network.UploadFile("C:locationfile.xxx", _
"ftp://domain.com/www/file.xxx", _
"username", "password", True, 500)
23. Dapatkan semua link/url dari situs web
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer
Dim b As String
For a = 1 To WebBrowser1.Document.Links.Count - 1
b = b & WebBrowser1.Document.Links(a).InnerHtml & vbCrLf
Next
TextBox1.Text = b
Label1.Text = WebBrowser1.Document.Links.Count & " links."
End Sub
24. Periksa apakah file tersebut ada atau tidak
If My.Computer.FileSystem.FileExists("C:locationfile.xxx") Then
'file does exist
'/ Do Action
Else
' file doesn't exist
End If
25. Salin file dari satu folder ke folder lain
My.Computer.FileSystem.CopyFile("C:yourLocationfile.xxx", "C:NewLocationfile.xxx")
26. Baca file teks ke dalam kotak teks
TextBox1.Text = My.Computer.FileSystem.ReadAllText("C:your pathfile.txt")
27. Tulis file teks dari Textbox
My.Computer.FileSystem.WriteAllText("C:locationfile.txt", TextBox1.Text, True)
28. Menghapus folder folder
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
My.Computer.FileSystem.DeleteDirectory ("C:PathFolder", FileIO.DeleteDirectoryOption.DeleteAllContents)
End Sub
29. Membalikkan string
Contoh: Halo => olleH
Public Class Form1
Dim textToReverse As String
Dim Letters() As Char
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
textToReverse = TextBox1.Text
Letters = textToReverse.ToCharArray()
Array.Reverse(Letters)
Dim ReversedText As New String(Letters, 0, Letters.Length)
TextBox2.Text = ReversedText
End Sub
End Class
30. Membalikkan gambar
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pic As New Bitmap(PictureBox1.Image)
For y As Integer = 0 To pic.Height - 1
For x As Integer = 0 To pic.Width - 1
Dim inv As Color = pic.GetPixel(x, y)
inv = Color.FromArgb(255, 255 - inv.R, 255 - inv.G, 255 - inv.B)
pic.SetPixel(x, y, inv)
PictureBox2.Image = pic
Next x
Next y
End Sub
31. Menampilkan gambar dari clipboard ke dalam kotak Gambar
If My.Computer.Clipboard.ContainsImage Then
PictureBox1.Image = My.Computer.Clipboard.GetImage
End If
Clipboard.Clear()
SEMOGA BERMANFAAT.

