Các bài tập Microsoft .NET

MỤC LỤC

Bài 1 Microsoft .NET Framework .2

Bài 2 Visual Studio.NET .13

Bài 3 Những khác biệt giữa VB.NET với VB6 .37

Bài 4 Những chức năng Đối Tượng mới của VB.NET (phần I).59

Bài 5 Những chức năng Đối Tượng mới của VB.NET (phần II) .68

Bài 6 Những chức năng Đối Tượng mới của VB.NET (phần III) .82

Bài 7 Những chức năng Đối Tượng mới của VB.NET (phần IV) .95

Bài 8 Những chức năng mới trong giao diện cửa sổcủa VB.NET (phần I) .112

Bài 9 Những chức năng mới trong giao diện cửa sổcủa VB.NET (phần II).124

Bài 10 Những chức năng mới trong giao diện cửa sổcủa VB.NET (phần III) .134

Bài 11 Những chức năng mới trong giao diện cửa sổcủa VB.NET (phần IV) .144

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) .161

pdf174 trang | Chuyên mục: Visual Basic 6.0 | Chia sẻ: dkS00TYs | Lượt xem: 1732 | Lượt tải: 4download
Tóm tắt nội dung Các bài tập Microsoft .NET, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
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 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 Sub
Nế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ữa
hai statements BeginUpdate và EndUpdate như sau:
' Shutdown the painting of the ListBox as items are added.
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 171
Private Sub ListBox1_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ListBox1.SelectedIndexChanged
 Try
 If ListBox1.SelectedValue "" Then
 MessageBox.Show(ListBox1.SelectedValue & " of " & ListBox1.SelectedItem.ToString,
"Selected value")
 End If
 Catch ex As Exception
 ' Do nothing, ignore this error
 End Try
End Sub
Như thế ta đã implemented (thi hành) cho .NET ListBox một chức năng
tương đương với ItemData của ListBox trong VB6.
.NET ListBox không hổ trợ Style Checkbox, nhưng ta có thể dùng
CheckedListBox.
ComboBox
Vì ComboBox thừa kế từ ListBox nên tất cả những gì ta biết về ListBox
đều áp dụng cho ComboBox. Đặc biệt bây giờ ComboBox có property
MaxDropDownItems cho ta quyết định hiển thị bao nhiêu items khi
danh sách được mở ra.
Kèm theo đây là một chương trình biểu diễn ComboBox trong đó ta dùng
Property ValueMember của ComboBox để trả về một trị số đại diện
Item. Data trong ComboBox1 được loaded từ một Access2000 database
table bằng code sau đây:
Private Sub frmCombo_Load( ByVal sender As System.Object, ByVal e As 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
Các bài tập Microsoft .NET 172
 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, là ValueMember trong Label1.
Private Sub ComboBox1_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ComboBox1.SelectedIndexChanged
 Try
 'Display the selected valueMember
 Label1.Text = ComboBox1.SelectedValue
 Catch
 End Try
Các bài tập Microsoft .NET 173
End Sub
Ở đây có hai cách để ta select một ComboBox item bằng coding. Cách
thứ nhất là cho biết AuthorId (ValueMember), user clicks button Select
by AuthorId để thấy kết quả:
Private Sub BtnSelectbyAuthorId_Click_1( ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnSelectbyAuthorId.Click
 'Use Try to ignore error if operation fails
 Try
 ' Select the ComboBox Item whose valueMember equal txtAuthorId.Text
 ComboBox1.SelectedValue = txtAuthorId.Text
 Catch
 End Try
End Sub
và cách thứ hai là cho biết FullName (DisplayMember), user clicks
button Select by Name để thấy kết quả:
Private Sub BtnSelectByName_Click( ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles BtnSelectByName.Click
 'Use Try to ignore error if operation fails
 Try
 ' Select the ComboBox Item whose DisplayMember equal txtFullName.Text
 ' FindString returns the index of the found item
 ComboBox1.SelectedIndex = ComboBox1.FindString(txtFullName.Text)
 Catch
 End Try
End Sub
Khi chạy chương trình, bạn sẽ thấy hình như CÁCdưới đây. Trong hình
ấy, MaxDropDownItems của ComboBox1 đã được set bằng 4.
Các bài tập Microsoft .NET 174

File đính kèm:

  • pdfCác bài tập Microsoft .NET.pdf
Tài liệu liên quan