2020年9月6日 星期日
2020年9月3日 星期四
Vb.net 讀寫到txt檔 (Function) [Write Read txt File (Function)]
1 2 3 4 5 6 7 8 9 10 11 | Private Sub write2txt(ByVal strPath As String, ByVal strInfo As String) Dim FileWriter As StreamWriter 'Dim encFormat As Encoding 'encFormat = Encoding.Default 'FileWriter = New StreamWriter(strPath, False, encFormat) FileWriter = New StreamWriter(strPath, False) FileWriter.Write(strInfo) FileWriter.Close() End Sub |
vb.net 讓兩個Richtextbox 相同的水平 [SetScrollPos on two RichTextBox]
1 2 3 4 5 6 7 8 9 10 11 12 13 | Imports System.Runtime.InteropServices.Marshal ' Scrollbar direction ' Const SBS_HORZ = 0 Const SBS_VERT = 1 ' Windows Messages ' Const WM_VSCROLL = &H115 Const WM_HSCROLL = &H114 Const SB_THUMBPOSITION = 4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | Private Declare Function GetScrollPos Lib "user32.dll" ( _ ByVal hWnd As IntPtr, _ ByVal nBar As Integer) As Integer 'Example: position = GetScrollPos(textbox1.handle, SBS_HORZ) Private Declare Function SetScrollPos Lib "user32.dll" ( _ ByVal hWnd As IntPtr, _ ByVal nBar As Integer, _ ByVal nPos As Integer, _ ByVal bRedraw As Boolean) As Integer 'Example: SetScrollPos(hWnd, SBS_HORZ, position, True Private Declare Function PostMessageA Lib "user32.dll" ( _ ByVal hwnd As IntPtr, _ ByVal wMsg As Integer, _ ByVal wParam As Integer, _ ByVal lParam As Integer) As Boolean 'Example: PostMessageA(hWnd, WM_HSCROLL, SB_THUMBPOSITION _ ' + &H10000 * position, Nothing) If (SetScrollPos(hWnd, SBS_HORZ, position, True) <> -1) Then PostMessageA(hWnd, WM_HSCROLL, SB_THUMBPOSITION +_ &H10000 * position, Nothing) Else MsgBox("Can't set info (Err: " & GetLastWin32Error() & ")") End If Dim Position = GetScrollPos(hWnd, SBS_VERT) + 20 If (SetScrollPos(hWnd, SBS_VERT, position, True) <> -1) Then PostMessageA(hWnd, WM_VSCROLL, SB_THUMBPOSITION + _ &H10000 * position, Nothing) Else MsgBox("Can't set info (Err: " & GetLastWin32Error() & ")") End If |
vb.net 讓Listview不會閃爍 [Fix Listview Flashing]
1 2 3 4 5 6 7 | Public Class ffListView Inherits ListView Public Sub New() Me.DoubleBuffered = True End Sub End Class |
vb.net 讓Class執行完删除 [Dispose class after finish]
1 2 3 4 5 6 | Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim clsUse As New Class1 clsUse.Dispose() End Sub |
vb.net 顯示richtextbox所選擇的line [Get Select RichTextBox line content]
1 | MsgBox(RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)) |
VB.NET 隱藏檔案、隱藏資料夾 [Hide File and Directory]
1 2 | '設定多個屬性,可使用以下方式 fileInfo.Attributes = FileAttributes.Hidden | FileAttributes.ReadOnly; |
2020年9月2日 星期三
vb.net 檢測字串裡是否有中文字 [Check String contain Chinese]
1 2 3 4 5 6 7 8 | Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Dim test As String = "111" If StrLenB(test) <> test.Length Then MsgBox("含中文字") Else MsgBox("不含中文字") End If End Sub |
vb.net 檢查無效路徑 [Check Path is available]
1 2 3 | Private Function NameCheck(ByVal strName As String) As String Return String.Join("_", strName.Split(IO.Path.GetInvalidFileNameChars)) End Function |
vb.net 檢查是64位元還是86位元 [Get System is x64 or x86]
1 2 3 4 5 | If System.Environment.Is64BitOperatingSystem = True Then MessageBox.Show("OS System : 64 Bit Operating System") Else MessageBox.Show("OS System : 32 Bit Operating System") End If |
vb.net 檔案移除 [Remove File]
1 2 3 4 5 6 7 8 9 10 | Dim FileToDelete As String FileToDelete = "C:\Users\Owner\Documents\testDelete.txt" If System.IO.File.Exists( FileToDelete ) = True Then System.IO.File.Delete( FileToDelete ) MessageBox.Show("File Deleted") End If |
vb.net 簡體轉繁體 [Convert Chinese Simplified to Chinese Traditional]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Dim strSC, strTC As String Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click '繁-->簡 strTC = RichTextBox1.Text '繁體系統 If RadioButton3.Checked = True Then strSC = StrConv(strTC, VbStrConv.SimplifiedChinese, 2052) '繁轉簡 End If '簡體系統 If RadioButton4.Checked = True Then strSC = StrConv(strTC, VbStrConv.SimplifiedChinese, 1028) '繁轉簡 End If RichTextBox2.Text = strSC End Sub |
vb.net 檔案的MD5敲打 [Match file to MD5]
1 2 3 4 | Imports System.Security.Cryptography Imports System.IO Dim strFile As String = "C:\test.exe" |
2020年9月1日 星期二
vb.net 隨機更換ArrayList的順序 [Random ArrayList Index]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Private Function RandomSort(aryInfo As ArrayList) As ArrayList Dim sortedList As ArrayList = New ArrayList() Dim generator As Random = New Random() Do While aryInfo.Count > 0 Dim position As Integer = generator.Next(aryInfo.Count - 1) Dim strInfo As String = Trim(aryInfo(position)) strInfo = strInfo.Replace(vbCrLf, "") strInfo = strInfo.Replace(vbLf, "") If strInfo <> "" Then sortedList.Add(strInfo) aryInfo.RemoveAt(position) Loop Return sortedList End Function |
vb.net 輸入時套用指定的字串 [Add String Mask]
1 2 3 4 5 6 7 8 9 10 | Private Sub cboScript_KeyDown(sender As Object, e As KeyEventArgs) Handles cboScript.KeyDown Dim strSufijo As String = ".ini" If cboScript.SelectionStart > cboScript.Text.Length - strSufijo.Length Then cboScript.Text = cboScript.Text.ToString.Replace(strSufijo, "") & strSufijo cboScript.Select(cboScript.Text.Length - strSufijo.Length, 0) Else cboScript.Text = cboScript.Text.ToString.Replace(strSufijo, "") & strSufijo End If End Sub |
Vb.net 整合 pdf [PDF Merge]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | Public Shared Function MergePdfFiles(ByVal pdfFiles() As String, ByVal outputPath As String) As Boolean Dim result As Boolean = False Dim pdfCount As Integer = 0 'total input pdf file count Dim f As Integer = 0 'pointer to current input pdf file Dim fileName As String Dim reader As iTextSharp.text.pdf.PdfReader = Nothing Dim pageCount As Integer = 0 Dim pdfDoc As iTextSharp.text.Document = Nothing 'the output pdf document Dim writer As PdfWriter = Nothing Dim cb As PdfContentByte = Nothing Dim page As PdfImportedPage = Nothing Dim rotation As Integer = 0 Try pdfCount = pdfFiles.Length If pdfCount > 1 Then 'Open the 1st item in the array PDFFiles fileName = pdfFiles(f) reader = New iTextSharp.text.pdf.PdfReader(fileName) 'Get page count pageCount = reader.NumberOfPages pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18) writer = PdfWriter.GetInstance(pdfDoc, New FileStream(outputPath, FileMode.OpenOrCreate)) With pdfDoc .Open() End With 'Instantiate a PdfContentByte object cb = writer.DirectContent 'Now loop thru the input pdfs While f < pdfCount 'Declare a page counter variable Dim i As Integer = 0 'Loop thru the current input pdf's pages starting at page 1 While i < pageCount i += 1 'Get the input page size pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i)) 'Create a new page on the output document pdfDoc.NewPage() 'If it is the 1st page, we add bookmarks to the page 'Now we get the imported page page = writer.GetImportedPage(reader, i) 'Read the imported page's rotation rotation = reader.GetPageRotation(i) 'Then add the imported page to the PdfContentByte object as a template based on the page's rotation If rotation = 90 Then cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height) ElseIf rotation = 270 Then cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30) Else cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0) End If End While 'Increment f and read the next input pdf file f += 1 If f < pdfCount Then fileName = pdfFiles(f) reader = New iTextSharp.text.pdf.PdfReader(fileName) pageCount = reader.NumberOfPages End If End While 'When all done, we close the document so that the pdfwriter object can write it to the output file pdfDoc.Close() result = True End If Catch ex As Exception Return False End Try Return result End Function |
vb.net 複製檔案時程式暫停 [Stop process before copy finish]
1 2 3 4 | SyncLock Me File.Copy(strFileOld, strFileNew, vbYes) Application.DoEvents() End SyncLock |
vb.net 複製RichTextBox內容 [Copy RichTextBox Content]
1 2 3 4 5 6 7 8 9 10 11 | Private Sub btnCopys_Click(sender As Object, e As EventArgs) Handles btnCopys.Click Dim strText As String = "" For Each strL As String In rtbIbf.Lines If strL <> "" Then strText &= strL & vbCrLf End If Next Clipboard.Clear() Clipboard.SetText(strText) End Sub |
訂閱:
文章 (Atom)