Dạng bài tập về Micosoft.NET- P34 docx

5 249 0
Dạng bài tập về Micosoft.NET- P34 docx

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

Thông tin tài liệu

Các bài tập Microsoft .NET 166 ListBox1.BeginUpdate() ' Loop through and add 50 items to the ListBox. Dim x As Integer For 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 trong nhiều cột nếu được set thành True, property SelectionMode nếu bằng MultiExtended 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 item ListBox1.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ển thị 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 list Các bài tập Microsoft .NET 167 MessageBox.Show(Mess) End Sub Sau 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ủa chúng: Private Sub BtnListSelectedItems_Click( ByVal sender As System.Object, ByVal e As System.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 Sub Các bài tập Microsoft .NET 168 Items là một Array of Objects ListBox 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 array của ListBox trong VB6. Tức là mỗi ListBox Item trong Vb6 có thể được chỉ định trước một con số đại diện nó. Khi user select List(i), ta có thể lấy ra 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 code thể 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 Function End Class Sau 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 display ListBox1.DisplayMember = "List" Nế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 = "ItemData" End Sub Khi chạy chương trình này, sau khi click nút Add Objects để clear ListBox1 và Add 6 Objects mới, nếu bạn click hàng thứ 4 trong ListBox sẽ thấy hình dưới đây: Code xử lý Event SelectedIndexChanged (tức là Event Click trước đây) của ListBox1 giống như dưới đây: . Các bài tập Microsoft .NET 166 ListBox1.BeginUpdate() ' Loop through and add 50 items to the ListBox. Dim. ListBox1.Items.Count - 1 Mess &= (ListBox1.Items(i).ToString) & vbCrLf Next ' Show the list Các bài tập Microsoft .NET 167 MessageBox.Show(Mess) End Sub Sau khi set property SelectionMode của Listbox1. MessageBox.Show(Mess, "Selected Items", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub Các bài tập Microsoft .NET 168 Items là một Array of Objects ListBox của .NET không hổ trợ ItemData như

Ngày đăng: 08/07/2014, 08:20

Từ khóa liên quan

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

Tài liệu liên quan