Danh mục

Các bài tập Microsoft .NET 1 phần 10

Số trang: 12      Loại file: pdf      Dung lượng: 212.19 KB      Lượt xem: 13      Lượt tải: 0    
tailieu_vip

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Kế đó chúng ta cho đặt một ContextMenu tên ContextMenu1 vào form và assign nó vào property DropDownMenu của button thứ ba như trong hình dưới đây:
Nội dung trích xuất từ tài liệu:
Các bài tập Microsoft .NET 1 phần 10Các bài tập Microsoft .NET 163 MessageBox.Show(You clicked the third button)End IfKế đó chúng ta cho đặt một ContextMenu tên ContextMenu1 vào formvà assign nó vào property DropDownMenu của button thứ ba như tronghình dưới đây:Nếu không muốn assign ContextMenu1 vào button thứ ba trong lúc thiếtkế, bạn có thể thực hiện việc ấy bằng code lúc form mới load như sau:Private Sub frmToolbar_Load( ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load ToolBarButton3.DropDownMenu = ContextMenu1End SubBạn hãy edit hai menuItems cho ContextMenu1: một cái tên mnuXámvới Text là Xám và cái kia tên mnuTrắng với Text là Trắng.Khi chạy chương trình, nếu bạn click cái thanh có dấu tam giác đen nằmbên phải button thứ ba, ContextMenu1 sẽ hiện ra để bạn dùng. Nếu bạnclick button thứ ba, chương trình cũng generate một Click Event nhưnghiện giờ ta không dùng nó, chỉ hiển thị một sứ điệp nhỏ để xác định là cóEvent Click ấy.Như thế, ta thấy .NET ghép một ContextMenu vào một ToolbarButton đểbiến nó thành một DropDownMenu. Có điều sau khi user đã chọn mộtItem trong ContextMenu/DropDownMenu, Text của Item đó không đượchiển thị giống như trong một ComboBox. Nếu bạn khó tính và muốn cóchuyện đó thì phải tự làm lấy như cho thấy trong code dưới đây:Private Sub frmToolbar_Load( ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load ToolBarButton3.DropDownMenu = ContextMenu1 ToolBarButton3.Text = XámEnd SubPrivate Sub mnuXám_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesmnuXám.Click MessageBox.Show(Bạn chọn màu Xám)Các bài tập Microsoft .NET 164 ToolBarButton3.Text = XámEnd SubPrivate Sub mnuTrắng_Click( ByVal sender As System.Object, ByVal e As System.EventArgs)Handles mnuTrắng.Click MessageBox.Show(Bạn chọn màu Trắng) ToolBarButton3.Text = TrắngEnd SubKhi chạy chương trình bạn sẽ thấy như sau:ListBoxItems là một collection of StringsMới dùng đến, ta sẽ thấy .NET ListBox rất giống ListBox trong VB6.Tiện ở chỗ bây giờ ta có thể edit các string Items của ListBox trong mộteditor nho nhỏ sẽ hiện ra khi ta click vào chữ (Collection) của propertyItems:Các bài tập Microsoft .NET 165Các Items được chứa trong một collection tên Items, do đó ta có thể làmviệc với mọi chức năng của một collection như Add, Clear, Insert,Remove, RemoveAt, Count .v.v..Thí dụ như ta cho thêm bốn Items vào Listbox1 lúc Form_Load như sau:Private Sub frmListbox_Load( ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load Add individual items ListBox1.Items.Add(Kăng-gu-ru) ListBox1.Items.Add(Công) Add more than one items by instantiating an object with items list enclosed in curly brackets {} ListBox1.Items.AddRange(New Object() {Đà điểu, Gấu Panda})End SubNếu trong khi chạy chương trình, bạn thêm nhiều Items vào ListBox vàmuốn tránh update display Listbox nhiều lần, bạn có thể kẹp code giữahai statements BeginUpdate và EndUpdate như sau: Shutdown the painting of the ListBox as items are added.Cá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 ...

Tài liệu được xem nhiều: