tailieunhanh - Beginning Visual Basic 2005 phần 4

Bây giờ là lúc để sử dụng một số các đoạn mã dựng sẵn đi kèm với Visual Studio 2005. Nhấp chuột phải vào trong không gian trống giữa Nếu Cuối Nếu báo cáo và chọn Thủ thuật Insert từ menu ngữ cảnh. Trong trình đơn thả xuống xuất hiện,cũng như kiểm soát phức tạp khác như kiểm soát động, ví dụ, có khả năng để chơi | Chapter 7 Show the Open dialog and if the user clicks the Open button load the file If Then End If End Sub 8. Now it s time to use some of the prebuilt code snippets that come with Visual Studio 2005. Right click in the blank space between the If and End If statements and choose Insert Snippet from the context menu. In the drop-down menu that appears double-click File System - Processing Drives Folders and Files and then scroll down the new list and double-click Read Text from a File. Your code should now look like this and you ll notice that the filename is highlighted indicating that this code needs to be changed If Then Dim allText As String Try allText Catch fileException As Exception Throw fileException End Try End If 9. Modify the code in the Try block as shown here Try Save the file name strFileName Read the contents of the file allText strFileName Display the file contents in the TextBox allText Catch fileException As Exception 10. Now run your project and once your form is displayed click the Open button to have the Open File dialog box displayed. Notice the custom caption in the title bar of the dialog box you specified this in your code. If you click the Files of type combo box you will see two filters. Click the second filter to see all of the files in the current directory. 11. Now locate a text file on your computer and select it. Then click the Open button to have the file opened and the contents of that file placed in the text box on the form as shown in Figure 7-8. 12. For the final test close your application and then start it again. Click the Open button on the form and notice that the Open File dialog box has opened in the last directory where you selected the last file from. How It Works Before displaying the

TỪ KHÓA LIÊN QUAN