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

Dạng bài tập về Micosoft.NET- P7 potx

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 (169.95 KB, 5 trang )

Các bài tập Microsoft .NET 31
Mỗi class đều có ít nhất một Sub New, gọi là Constructor (giống như
Class_Initialize của VB6 class) và Sub Dispose, gọi là Destructor. Đó
là hai Sub dùng để tạo ra và phá hủy Object. Vì Form1 thừa kế từ
Standard Form nên trong Sub New trước hết phải gọi constructor
MyBase.New() của cha nó, và trong Sub Dispose sau hết phải gọi
destructor MyBase.Dispose của cha nó.
Mở Class View Window
Bạn có thể Navigate trong Code qua Class View. DoubleClick lên tên của
Object hay Sub/Function trong Class View để mang cursor đến code của
nó trong trang Edit.
Các bài tập Microsoft .NET 32
Biu din DragDrop
Code của DragDrop, lưu ý ta phải viết thêm Sub ListBox2_DragEnter
để handle Event DragEnter.
Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles
ListBox1.MouseDown
' Start the DragDrop process
Dim ItemIndex As Integer
Các bài tập Microsoft .NET 33
' Identify the Listbox item that has mousedown using mouse coordinates
ItemIndex = ListBox1.IndexFromPoint(New System.Drawing.Point(e.X, e.Y))
' Start the DragDrop process passing along the ListboxItem as
ListBox1.Items(ItemIndex)
ListBox1.DoDragDrop(ListBox1.Items(ItemIndex),
DragDropEffects.Copy Or DragDropEffects.Move)
End Sub
Private Sub ListBox2_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListBox2.DragEnter
' Apply the copy effect


' AND remember to set the property Allow Drop of Listbox2 to TRUE
If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub ListBox2_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListBox2.DragDrop
Dim LItem As String
Các bài tập Microsoft .NET 34
' Obtain the Source ListItem String
LItem = e.Data.GetData(DataFormats.Text).ToString
' Add it to Listbox2
ListBox2.Items.Add(LItem)
' Remove the Item from Listbox1
ListBox1.Items.RemoveAt(ListBox1.FindString(LItem))
End Sub
Khi Load data vào Listbox ta dùng StreamReader để Open một File as
Input.
Khi Save data của Listbox vào một Text file ta dùng StreamWriter để
Open một File as Output (hay Append nếu ta cho thêm Option
Append=True):
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MenuItem2.Click
' Read the list of animals from a text file into Listbox2
Dim sr As StreamReader
Dim Pos As Integer
Dim TStr As String
ListBox2.Items.Clear() ' Clear Listbox2

' Use a StreamReader to open the UTF-8 file to read.
sr = New StreamReader(AppPath & "animals.txt")
' Read each line in the file.
' When the end of the file is reached, return the value "-1".
Dim x As String
Các bài tập Microsoft .NET 35
While sr.Peek <> -1
x = sr.ReadLine() ' Read a line
ListBox2.Items.Add(x) ' Add it to Listbox2
End While
sr.Close() ' Close the file
End Sub
Private Sub SaveAnimalList()
' Save the content of Listbox2 into a UTF-8 Text file
Dim i As Integer
Dim sw As StreamWriter
' Open the file to write in UTF-8 mode, using a StreamWriter.
sw = New StreamWriter(AppPath & "Animals.txt")
' Write each line in the Listbox.
For i = 0 To ListBox2.Items.Count - 1
sw.WriteLine(ListBox2.Items(i))
Next
sw.Close() ' Close the file
End Sub
Ta hiển thị ngày và giờ bằng cách dùng Timer1 và Shared Function DateTime.Now
formated bằng hai Functions có sẵn ToLongDateString và ToLongTimeString.
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Timer1.Tick
' Display Date and Time every half a second
Label3.Text = DateTime.Now.ToLongDateString & " " & DateTime.Now.ToLongTimeString

End Sub
Bạn cũng có thể hiển thị ngày giờ trong format khác bằng cách viết:
Label3.Text = DateTime.Now.ToString("ddd dd/MM/yyyy hh:mm:ss")
để có: WED 18/07/2001 09:16:42

×