Tải bản đầy đủ (.docx) (5 trang)

Giáo trình Visual Basic 7

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (89.99 KB, 5 trang )

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 ReadAllText
Ví 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 Sub
4- Ghi file text nhờ phương pháp WriteAllText
Vidụ
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 Sub
5- Ghi chèn vào file text nhờ phương pháp WriteAllText với tham số
append=true
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 Sub
6- Đọ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 binary
Ví dụ
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim 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 Sub
7- 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ại
Ví 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 Sub
8- 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ạy
Dạng hàm
InputBox(prompt, title, DefaultResponse,x,y) – hàm trả lại string đã gõ
Prompt – dấu nhắc , kiểu dữ liệu string
Title- Tiêu đề trên hộp thoại- kiểu string
DefaultResponse – để 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ọn
Ví 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 Sub
9.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ụ 2
Private 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 Sub
9.3- Hộp thoại ghi file SaveFileDialog
Các tính chất tương tự như OpenFileDialog
Ví 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 Sub
9.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 Sub
10- Phương pháp tách dòng Split
Phươ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 txt
Ngữ pháp
Dim mang() as string
Mang=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

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×