Giáo trình Visual Basic 7

5 477 3
Giáo trình Visual Basic 7

Đang tải... (xem toàn văn)

Thông tin tài liệu

Giáo trình Visual Basic

Input/output data <Tiếp theo>Trong VB ngoài lớp stream đã nói ở trên, nó còn cho chúng ta sử dụng module FileSystem , module này cung cấp các thủ tục cho phép làm việc với đĩa, thư mục ,file rất tiện lời có thể tìm hiểu module này qua My.Computer.FileSystem Object.1- Hàm đổi thư mục ChDir(tenthumuc)2- Hàm lấy thư mục hiện thời CurDir()3- Đọc file text nhờ phương pháp ReadAllTextVí dụPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dongtext As String dongtext = My.Computer.FileSystem.ReadAllText("C:\vidu.txt") MsgBox( dongtext ) End Sub4- Ghi file text nhờ phương pháp WriteAllTextVidụPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dong As String = "aaabbnnnmccddeee" My.Computer.FileSystem.WriteAllText("C:\vidu1.txt", dong, True) End Sub5- Ghi chèn vào file text nhờ phương pháp WriteAllText với tham số append=truePrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dong As String = "aaabbnnnmccddeee" My.Computer.FileSystem.WriteAllText("C:\vidu1.txt", dong, True) End Sub6- Đọc các byte từ file vào mảng nhờ phương pháp ReadAllBytesĐây là cách đọc dữ liệu dạng binaryVí dụ Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim dong(100) As Byte Dim i As Int16 dong = My.Computer.FileSystem.ReadAllBytes("C:\vidu1.txt") For i = 0 To 10 MsgBox(Convert.ToChar(dong(i))) Next i End Sub7- Ghi các byte vào file nhờ phương pháp writeallBytesĐây là cách ghi dữ liệu dạng binary, cách này cho chế độ ghi chèn (append) nếu file đã tồn tạiVí dụPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dong() As Byte = {1, 2, 3, 4, 5, 6} My.Computer.FileSystem.WriteAllBytes("C:\vidu3.01", dong, True) End Sub8- Hàm InputBoxĐây là hàm rất tiện lợi trong VB dùng để vào dữ liệu hoặc trao đổi với chương trình trong quá trình chạyDạng hàmInputBox(prompt, title, DefaultResponse,x,y) – hàm trả lại string đã gõPrompt – dấu nhắc , kiểu dữ liệu stringTitle- Tiêu đề trên hộp thoại- kiểu stringDefaultResponse – để rỗng “”X,Y – Tọa độ góc trên bên trái của hộp thoại Ví dụ Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click InputBox("nhập vào mã HD", "phần kế toan", "", 100, 100) End Sub 9- Lớp các hộp thoạiĐể tiện cho người lập trình trong VB cung cấp một số loại hộp thoại, giúp cho người sử dụng có thể có các lựa chọn tùy ý như chọn màu, chọn font, chọn file…9.1- Hộp thoại chọn màu ColorDialog+ Phương pháp : ShowDialog dùng để hiện hộp thoại+ Các tính chất : - ColorDialog.AllowFullOpen =true/False; cho phép chọn đặt các bảng màu- ColorDialog.Anycolor=True/False ; Cho phép chọn màu bất kỳ trong bảng màu cơ bản (mặc định)- ColorDialog.Color – trả lại màu đã chọnVí dụPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If (ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then Label1.BackColor = ColorDialog1.Color End If End Sub9.2- Hộp thoại chọn file OpenFileDialog+ Phương pháp : ShowDialog+ Tính chất Filter- để lọc các định dạng file + Tính chất FilterIndex cho chỉ số của thành phần trong trong Filter+ Tính chất filename trả lại tên file được chọn để mở Ví dụ 1 OpenFileDialog.filter(“file txt(*.txt)| *.text| all file (*.*)|*.*”) OpenFileDialog.filter(“ Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt|Office Files|*.doc;*.xls;*.ppt|All Files|*.*”) Ngữ pháp viết dòng lọc : đó là một dọng gồm các thành dạng : Dong_tbao(*.mỏrộng1; *.mỏrộng2;… )Nếu có nhiều thành phần thì phân biệt bằng dấu hoặc “|” Ví dụ 2Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim openFileDialog1 As New OpenFileDialog Dim ten_file As String openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" If (openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then ten_file = openFileDialog1.FileName MsgBox(ten_file) End If End Sub9.3- Hộp thoại ghi file SaveFileDialogCác tính chất tương tự như OpenFileDialogVí dụ:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim SaveFileDialog1 As New SaveFileDialog Dim ten_file As String Dim dong As String = "123456789" SaveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then ten_file = SaveFileDialog1.FileName My.Computer.FileSystem.WriteAllText(ten_file, dong, True) End If End Sub9.4- Hộp thoại chọn font fontDialog + Phương pháp ShowDialog dùng để hiện hộp thoại và danh sách font+ Tính chất font chứa tên loại font đã chọn+ Tính chất color cho màu chữVí dụPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim fontDialog1 As New FontDialog fontDialog1.ShowColor = True fontDialog1.Font = textBox1.Font fontDialog1.Color = textBox1.ForeColor If fontDialog1.ShowDialog() <> DialogResult.Cancel Then textBox1.Font = fontDialog1.Font textBox1.ForeColor = fontDialog1.Color End If End Sub10- Phương pháp tách dòng SplitPhương pháp này cho phép tách nhanh một dòng thành các dòng con , được sử dụng rất hiệu quả khi kết hợp với file dạng txtNgữ pháp Dim mang() as stringMang=dong_tach.split( new char (){“,”,”;”,” “})Ví dụPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim mang() As String Dim i As Integer Dim dong_tach As String = "2;3; 4; a;b" mang = dong_tach.Split(New Char() {";"}) For i = 0 To mang.Length - 1 MsgBox(mang(i)) Next i End Sub . rất tiện lợi trong VB dùng để vào dữ liệu hoặc trao đổi với chương trình trong quá trình chạyDạng hàmInputBox(prompt, title, DefaultResponse,x,y) – hàm. = 0 To 10 MsgBox(Convert.ToChar(dong(i))) Next i End Sub7- Ghi các byte vào file nhờ phương pháp writeallBytesĐây là cách ghi dữ liệu

Ngày đăng: 13/11/2012, 10:23

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan