Tài liệu Dot Net-Bài 12-Những chức năng mới trong giao diện cửa sổ của VB.NET (phần V) doc - Pdf 95

Bài 12
Những chức năng mới trong giao diện cửa sổ của
VB.NET (phần V)
Toolbars
Toolbars trong .NET đã được nâng cấp bằng cách thêm chức năng cho các
ToolBarButtons trong collection của những buttons ấy.
Để dùng thử Toolbar control, bạn hãy khởi động một Project mới và đặt một Toolbar
vào form chính bằng cách doubleclick lên Toolbar icon trong Toolbox. Một Toolbar
sẽ hiện ra nằm ngay dưới tiêu đề của form. Kế đó rightclick lên Toolbar ấy và chọn
Properties để edit property Buttons Collection bằng cách click lên chữ (Collection)
rồi click ba dấu chấm phía bên phải để hiển thị ToolbarButton Collection Editor.
Bạn hãy Add vào Toolbar ba buttons với những đặc tính sau:
• Đổi property Text của button thứ nhất (ToolbarButton1) ra
Close vì ta muốn đóng chương trình khi user click lên button
ấy. By default Style của ToolbarButton là PushButton.
• Đổi property Style của button thứ nhì (ToolbarButton2) ra
Separator vì ta muốn dùng nó để tạo khoảng cách giữa button
thứ nhất và button thứ ba.
• Đổi property Text của button thứ ba (ToolbarButton3) ra
Background Colour và property Style ra DropDownButton
vì ta muốn dùng nó như một Combobox.
Khi chạy thử chương trình ta sẽ thấy hình giống như dưới đây:
Bây giờ ta sẽ viết code để xử lý Event Click của Toolbar. Chỉ có một handler, Sub
ToolBar1_ButtonClick, được dùng cho tất cả các buttons. Ta phân biệt Button
nào dựa vào Index của nó, giống giống như một array of buttons trong VB6. Nếu
user click button thứ nhất ta sẽ có ToolBar1.Buttons.IndexOf(e.Button) bằng 0,
lúc ấy ta sẽ Close form chính.
Private Sub ToolBar1_ButtonClick( ByVal sender As System.Object, ByVal e As
System.Windows.Forms.ToolBarButtonClickEventArgs) Handles
ToolBar1.ButtonClick
Select Case ToolBar1.Buttons.IndexOf(e.Button)

ContextMenu/DropDownMenu, Text của Item đó không được hiể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ám"
End Sub
Private Sub mnuXám_Click( ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuXám.Click
MessageBox.Show("Bạn chọn màu Xám")
ToolBarButton3.Text = "Xám"
End Sub
Private 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ắng"
End Sub
Khi chạy chương trình bạn sẽ thấy như sau:
Bạn có thể tải về chương trình Toolbar nầy tại đây.
ListBox
Items là một collection of Strings
Mớ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ột editor nho nhỏ sẽ hiện ra
khi ta click vào chữ (Collection) của property Items:
Các Items được chứa trong một collection tên Items, do đó ta có thể làm việ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

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
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

' 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()
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

System.EventArgs) Handles MyBase.Load
Dim ds As New DataSet () ' Instantiate a Dataset
' Instantiate an OleDbDataAdapter for Access2000 database Authors.mdb and
return table Authors
Dim myData As New OleDbDataAdapter("Select * from Authors",
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= \Authors.mdb")
myData.Fill(ds, "Authors") ' Load table Authors into Dataset
With ComboBox1
' Bind Table Authors to ComboBox1
.DataSource = ds.Tables("Authors")
' Make Property/Datafield FullName the DisplayMember of ComboBox1
.DisplayMember = "FullName"
' Make Property/Datafield AuthorID the ValueMember of ComboBox1
.ValueMember = "AuthorID"
End With
End Sub
Chúng ta chỉ định record datafield FullName làm DisplayMember của ComboBox1
và datafield AuthorID làm ValueMember của ComboBox1.
Ta truy cập data của cơ sở dữ liệu bằng cách dùng một DataAdapter loại
OleDbDataAdapter khi cho nó một SQL CommandText: "Select * from
Authors" và một connection string, trong đó có cho biết database driver:
Microsoft.Jet.OLEDB.4.0 và tên của database \Authors.mdb. File Authors.mdb
nằm chung với mã nguồn của chương trình trong parent folder của folder bin, nơi
chứa ComboBox.exe.
Kế đó ta dùng DataAdapter để bỏ table Authors vào dataset ds. Cách làm việc nầy
tương tự như ADO (Active Data Object) trong VB6. Có điểm khác là Dataset có thể
chứa nhiều tables (recordsets) và nó hoạt động như một cached disconnected
database trong bộ nhớ. Kỹ thuật nầy có tên là ADO.NET và ta sẽ bàn thêm nhiều về
nó trong tương lai.
Mỗi lần user select một item mới từ ComboBox1, chương trình sẽ hiển thị AuthorId,

Catch
End Try
End Sub
Khi chạy chương trình, bạn sẽ thấy hình như dưới đây. Trong hình ấy,
MaxDropDownItems của ComboBox1 đã được set bằng 4.
Bạn có thể tải về chương trình ComboBox nầy tại đây.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status