Giáo trình Visual Basic
Trang 1Bai3 Các controls (tiếp theo)
1-Label:
- Dùng để thông báo các kết quả trung gian
- Giải thích chức năng các control khác
- Tính chất quan trọng hay dùng
Labele.text
+PictureBox
- Tính chất cần chú ý :
a/ Image - dùng để chứa ảnh
Có thể đưa ảnh dạng BMP,Ico,Jpg,gif bằng hai cách nạp ảnh lúc design hoặc lúc chạy chương trình
PictureBox1.Image = Image.FromFile(filename)
Ví dụ PictureBox1.Image = Image.FromFile("c:\h2.bmp")
b/ sizemode – dùng để xác định chế độ hiện ảnh trong pictuerebox
có các chế độ sau :Nomal, StrechImage, AutoSize, CenterImage, Zoom
+ Một số sự kiện cần chú ý
a/ Sự kiện DragEnter
sự kiện này xảy ra khi đối tượng của hành động Drag- Drop được kéo vào vùng của control sẽ nhận đối tượng
Ví dụ: đối tượng được kéo vào TextBox2
Private Sub TextBox2_DragEnter( ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles
TextBox2.DragEnter
Trang 2
Các câu lệnh
End Sub
b/Class DragDropEffects
xác định hiệu quả của hành động Drag-Drop, nó có thể có các giá trị sau:
None The drop target does not accept the data.
Copy The data is copied to the drop target.
Move The data from the drag source is moved to the drop
target
Link The data from the drag source is linked to the drop
target
Scrol
l Scrolling is about to start or is currently occurring in the drop target
All The data is copied, removed from the drag source, and
scrolled in the drop target
Ví dụ:
CDragDropEffects.Move
+ Phương pháp DoDragDrop
Dạng Control.DoDragDrop
Phương pháp này sử dụng khi bắt đầu hành động Drag-Drop
Ví dụ
Trang 3TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy Or
DragDropEffects.Move)
Hai tham số sử dụng ở đây là :
- Đối tượng của hành động Drag-Drop
- Tác động mà nó có thể chịu
Chú ý thuộc tính AlloweDrop của đối tượng nhận phải đặt true
Ví dụ kéo- thả text
Private Sub TextBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy Or
DragDropEffects.Move)
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
e.Effect = DragDropEffects.Move
End Sub
Private Sub TextBox2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub
Trang 4Ví dụ 2 : kéo – thả ảnh
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
PictureBox2.AllowDrop = True
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal
e As System.Windows.Forms.MouseEventArgs) Handles
PictureBox1.MouseDown
If Not PictureBox1.Image Is Nothing Then
PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy Or DragDropEffects.Move)
Else
End If
End Sub
Private Sub PictureBox2_DragEnter(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragEnter
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
e.Effect = DragDropEffects.Copy
End If
Trang 5End Sub
Private Sub PictureBox2_DragDrop(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragDrop PictureBox2.Image = e.Data.GetData(DataFormats.Bitmap)
End Sub
-=======================================
Timer control
+Sư kiện Tick
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
End Sub
+ Tinh chất:
- Interval : khoảng thời gian sự kiện tick xảy ra tính bằng milisecond
- Enabled : true/false
- Start
Vidu
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Trang 6If Label1.Visible = True Then
Label1.Visible = False
Label2.Visible = True
Else
Label1.Visible = True
Label2.Visible = False
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Interval = 300
Label1.Visible = True
Label2.Visible = True
Timer1.Start()
End Sub