- [C#] Hướng dẫn đăng nhập zalo login sử dụng API v4 trên winform
- [SOFTWARE] Phần mềm gởi tin nhắn Zalo Marketing Pro giá rẻ mềm nhất thị trường
- [C#] Việt hóa Text Button trên MessageBox Dialog Winform
- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
Thủ thuật kiểm tra kết nối internet, độ phân giải màn hình, lấy serial ổ cứng, lấy dữ liệu từ clipboard
Hôm nay, mình xin hướng dẫn các bạn các thủ thuật nhỏ trong lập trình vb.net: kiểm tra kết nối internet, lấy serial ổ đĩa cứng, copy text từ clipboard, lấy độ phân giải màn hình....
1. Kiểm tra kết nối 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
Sử dụng hàm kiểm tra kết nối internet như sau:
If IsConnectionAvailable() = TrueThen
Do Action Here
Else
Do Action Here
End If
2. Lấy độ phân giải màn hình đang sử dụng
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. Lấy serial ổ đĩa cứng 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. Chạy ứng dụng từ chương trình
Ví dụ: dưới đây khi click vào button sẽ mở chương mình microsoft paint
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Shell("mspaint")
End Sub
5. Mở một file nhạc trong ổ đĩa
My.Computer.Audio.Play("C:MusicMusic.wav")
6. Copy text từ clipboard
If My.Computer.Clipboard.ContainsText Then
TextBox1.Text = My.Computer.Clipboard.GetText
End If
7. Mở tiếng beep từ âm thanh của hệ thống windows
My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
8. Mở trang web từ 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. Tạo một số ngẫu nhiên
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. Nhấn Alt + PrintScreen để chụp hình màn hình và lưu file ảnh xuống ổ đĩa
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. Lấy thông tin Ram từ máy tính
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. Send key
Ví dụ: bấm phím backspace.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
My.Computer.Keyboard.SendKeys("{BACKSPACE}")
End Sub
Các mã send key
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 đến F15 {F1} đến {F15}
12. Hàm mã hóa 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
13. Show 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
14. Lấy đường dẫn từ folder special vd: Desktop
Dim DesktopPath As String = CreateObject("WScript.Shell").Specialfolders(10)
'OR
Dim DesktopPath As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
MsgBox(DesktopPath)
15. Lấy sự kiện phím vừa nhấn
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
16. Xóa tất cả text từ các text box trong một 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
17. Xóa số (number) từ textbox
vd: 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
18. Auto font size text để vừa 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
19. Chỉ cho phép nhập số trong 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
20. Download 1 file từ website
My.Computer.Network.DownloadFile("http://www.domain.com/file.txt", _
"C:yourpathfile.txt")
21. Upload 1 file lên website
My.Computer.Network.UploadFile("C:locationfile.xxx", _
"ftp://domain.com/www/file.xxx", _
"username", "password", True, 500)
22. Lấy tất cả link từ một trang website
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
23. Kiểm tra file có tồn tại hay không
If My.Computer.FileSystem.FileExists("C:locationfile.xxx") Then
'file does exist
'/ Do Action
Else
' file doesn't exist
End If
24. Copy một file từ folder này sang folder khác
My.Computer.FileSystem.CopyFile("C:yourLocationfile.xxx", "C:NewLocationfile.xxx")
25. Đọc một file text vào text box
TextBox1.Text = My.Computer.FileSystem.ReadAllText("C:your pathfile.txt")
26. Ghi ra file text từ Textbox
My.Computer.FileSystem.WriteAllText("C:locationfile.txt", TextBox1.Text, True)
27. Xóa một thư mục 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
28. Reveser một chuỗi
Vd: Hello => 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
29. Invert một hình ảnh
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
30. Hiển thị hình ảnh từ clipboard vào Picture box
If My.Computer.Clipboard.ContainsImage Then
PictureBox1.Image = My.Computer.Clipboard.GetImage
End If
Clipboard.Clear()
HAVE FUN :)