|
txtContent.Text = "" '清除內容 Open sFile$ For Input As #1 '開啟文字檔 While Not EOF(1) '未到檔案尾端 Line Input #1, a$ '逐行讀取 txtContent.Text = txtContent.Text & a$ & vbCrLf '逐行加入txtContent及換行符號 Wend '反覆執行 Close #1 '關閉檔案 |
若是將 TextBox(txtContent) 顯示內容 的處理敘述略作變更,將可加快處理速度
|
txtContent.Text = "" '清除內容 Open sFile$ For Input As #1 '開啟文字檔 While Not EOF(1) '未到檔案尾端 Line Input #1, a$ '逐行讀取 txtContent.SelText = a$ & vbCrLf '逐行加入txtContent及換行符號 Wend '反覆執行 Close #1 '關閉檔案 |
其中的道理是:
因為 txtContent.Text = txtContent.Text & a$ & vbCrLf 會安排一個記憶體位置儲存等式右側的運算結果,再清除
txtContent 原有內容,以此一運算結果取代之,其間包含許多的動作;而 txtContent.SelText = a$ & vbCrLf
是直接把右側的運算結果放入 txtContent 裡,少了很多不必要的的動作,所以比較快。
此外,以單一敘述一次讀入整個檔案更可加快完成
|
txtContent.Text = "" '清除內容 Open sFile$ For Input As #1 '開啟文字檔 txtContent.Text = StrConv(InputB(LOF(F), #1), vbUnicode) '一次讀入整個檔案 Close #1 '關閉檔案 |