Thông tin tài liệu:
Dạng bài tập về Micosoft.NET- P34: Sau khi biết qua về .NET, câu hỏi bạn sẽ đặt ra là bạn có nên học lập trìnhtrên .NET hay không. Nói chung, về lập trình có nhiều tôn giáo nhưVC++/Java, VB6, Delphi ..v.v.. Bạn có thể chọn giữa C# và VB.NET.Đối với VB6 programmers, học lập trình VB.NET sẽ mất một thời gian,nhưng không khó. Nên nhớ rằng .NET không phải chỉ cho ta các ngônngữ lập trình, mà cả một hệ thống triển khai phần mềm chú trọng vàomục tiêu hơn là cách thức....
Nội dung trích xuất từ tài liệu:
Dạng bài tập về Micosoft.NET- P34Các bài tập Microsoft .NET 166ListBox1.BeginUpdate() Loop through and add 50 items to the ListBox.Dim x As IntegerFor x = 1 To 50 ListBox1.Items.Add(Item & x.ToString())Next x Allow the ListBox to repaint and display the new items.ListBox1.EndUpdate()Giống như trong VB6, property MultiColumn hiển thị Items trongnhiều cột nếu được set thành True, property SelectionMode nếu bằngMultiExtended thì cho ta select nhiều Items cùng một lúc.Tuy nhiên, các Items được chọn sẽ có mặt trong một collection chớkhông phải có Selected(i)=True như trong VB6.Muốn select một Item lúc run-time ta dùng code như sau: Select three items (2nd, fourth and sixth) from the ListBox.ListBox1.SetSelected(1, True) 1 is index of 2nd itemListBox1.SetSelected(3, True)ListBox1.SetSelected(5, True)Trong thí dụ tại đây ta có ListBox1 với danh sách các con vật trong SởThú Saigon. Button List Items sẽ liệt kê danh sách này. Để ý cách ta hiểnthị một Item với expression Listbox1.Items(i).ToString.Private Sub BtnListItems_Click( ByVal sender As System.Object, ByVal e As System.EventArgs)Handles BtnListItems.Click Dim i As Integer Dim Mess As String make up the list of Items separated by CarriageReturn/LineFeed For i = 0 To ListBox1.Items.Count - 1 Mess &= (ListBox1.Items(i).ToString) & vbCrLf Next Show the listCác bài tập Microsoft .NET 167 MessageBox.Show(Mess)End SubSau khi set property SelectionMode của Listbox1 ra MultiExtended,code dưới đây sẽ liệt kê danh sách các items được chọn với index củachúng:Private Sub BtnListSelectedItems_Click( ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles BtnListSelectedItems.Click Dim i As Integer Dim Mess As String make up the list of Selected Items separated by CarriageReturn/LineFeed Collection SelectedIndices contains the index of selecteditems For i = 0 To ListBox1.SelectedItems.Count - 1 Mess &= (ListBox1.SelectedIndices(i).ToString) & : & (ListBox1.SelectedItems(i).ToString) &vbCrLf Next Show the list MessageBox.Show(Mess, Selected Items, MessageBoxButtons.OK, MessageBoxIcon.Information)End SubCác bài tập Microsoft .NET 168Items là một Array of ObjectsListBox của .NET không hổ trợ ItemData như trong VB6. ItemData làmột array chứa các con số tương ứng với những Items trong List arraycủa ListBox trong VB6. Tức là mỗi ListBox Item trong Vb6 có thể đượcchỉ định trước một con số đại diện nó. Khi user select List(i), ta có thể lấyra ItemData(i) của List Item ấy.Thật ra Items của .NET Listbox cũng có thể là một Array of Objects,không nhất thiết phải là một collection of Strings như ta đã dùng.Dưới đây là code ta định nghĩa một Class tên LBItem, đoạn dùng codethể Add một Array of Objects loại LBItem vào Listbox1:Public Class LBItem Private mList As String Private mItemData As Integer List Item of Listbox Public Property List() As String Get Return mList End Get Set ( ByVal Value As String)Các bài tập Microsoft .NET 169 mList = Value End Set End Property ItemData of Listbox Public Property ItemData() As Integer Get Return mItemData End Get Set ( ByVal Value As Integer) mItemData = Value End Set End Property Function to return a string representing this item for display Overrides Function ToString() As String Return mList End FunctionEnd ClassSau khi Add một Array of Objects vào ListBox1 ta phải chỉ định làm thếnào để hiển thị một Item. Thí dụ như dùng property List của LBItem nhưdưới đây: Indicate that Property List of LBItem will be used to displayListBox1.DisplayMember = ListNếu ta không chỉ định DisplayMember, tức là ListBox1.DisplayMember= thì ListBox1 sẽ dùng Function ToString của LBItem để hiển thị.Ngoài ra, để trả về một value giống như ItemData của List Item ta chỉđịnh ValueMember như dưới đây:Private Sub BtnAddOjects_Click( ByVal sender As System.Object, ByVal e As System.EventArgs)Handles BtnAddOjects.Click Clear all items in Listbox1 ListBox1.Items.Clear()Các bài tập Microsoft .NET 170 Dim Objs(5) As LBItem Create an array of 6 Objects of LBItem Dim i As Integer For i = 0 To 5 Objs(i) = New LBItem() Objs(i).List = Line & i.ToString Objs(i).ItemData = i + 100 Next Add the array of objects to Listbox1 ListBox1.DataSource = Objs Indicate that Property List of LBItem will be used to display ListBox1.DisplayMember = List Indicate that Property ItemData of LBItem will be used to return a value ListBox1.ValueMember = ItemDataEnd SubKhi chạy ...