
Dạng bài tập về Micosoft.NET- P35
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Dạng bài tập về Micosoft.NET- P35Các bài tập Microsoft .NET 171Private Sub ListBox1_SelectedIndexChanged( ByVal sender As System.Object, ByVal e AsSystem.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 TryEnd SubNhư thế ta đã implemented (thi hành) cho .NET ListBox một chức năngtương đương với ItemData của ListBox trong VB6..NET ListBox không hổ trợ Style Checkbox, nhưng ta có thể dùngCheckedListBox.ComboBoxVì 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ó propertyMaxDropDownItems cho ta quyết định hiển thị bao nhiêu items khidanh sách được mở ra.Kèm theo đây là một chương trình biểu diễn ComboBox trong đó ta dùngProperty ValueMember của ComboBox để trả về một trị số đại diệnItem. Data trong ComboBox1 được loaded từ một Access2000 databasetable 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 DatasetCá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 WithEnd SubChúng ta chỉ định record datafield FullName làm DisplayMember củaComboBox1 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ạiOleDbDataAdapter khi cho nó một SQL CommandText: Select *from Authors và một connection string, trong đó có cho biết databasedriver: 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 parentfolder 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àmviệc này tương tự như ADO (Active Data Object) trong VB6. Có điểmkhá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ểnthị AuthorId, là ValueMember trong Label1.Private Sub ComboBox1_SelectedIndexChanged( ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles ComboBox1.SelectedIndexChanged Try Display the selected valueMember Label1.Text = ComboBox1.SelectedValue Catch End TryCác bài tập Microsoft .NET 173End SubỞ đây có hai cách để ta select một ComboBox item bằng coding. Cáchthứ nhất là cho biết AuthorId (ValueMember), user clicks button Selectby AuthorId để thấy kết quả:Private Sub BtnSelectbyAuthorId_Click_1( ByVal sender As System.Object, ByVal e AsSystem.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 TryEnd Subvà cách thứ hai là cho biết FullName (DisplayMember), user clicksbutton 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 TryEnd SubKhi 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
Tìm kiếm theo từ khóa liên quan:
tài liệu dot net visual studio thủ thuật .net lập trình .net lập trình visual tài liệu .net 2010Tài liệu có liên quan:
-
Lập trình .net 4.0 và visual studio 2010 part 63
7 trang 140 0 0 -
Hướng dẫn thực hành lập trình C trên Visual Studio
9 trang 138 0 0 -
Lập trình .Net - (Tập 3: Lập trình giao diện người dùng theo C#): Phần 2
56 trang 38 0 0 -
HƯỚNG DẪN VẼ ĐỒ THỊ VỚI VISUAL STUDIO C# 2008
12 trang 30 0 0 -
37 trang 30 0 0
-
Lập trình .net 4.0 và visual studio 2010 part 11
6 trang 30 0 0 -
Lập trình .net 4.0 và visual studio 2010 part 3
10 trang 28 0 0 -
Lập trình .net 4.0 và visual studio 2010 part 60
9 trang 28 0 0 -
Lập trình .Net - (Tập 3: Lập trình giao diện người dùng theo C#): Phần 1
43 trang 27 0 0 -
Dạng bài tập về Micosoft.NET- P8
5 trang 26 0 0 -
24 trang 24 0 0
-
Dạng bài tập về Micosoft.NET- P16
5 trang 24 0 0 -
Lập trình .net 4.0 và visual studio 2010 part 50
7 trang 23 0 0 -
8 trang 23 0 0
-
Tài liệu Lập trình C# 2008 cơ bản
100 trang 23 0 0 -
Dạng bài tập về Micosoft.NET- P15
5 trang 22 0 0 -
Dạng bài tập về Micosoft.NET- P14
5 trang 21 0 0 -
Lập trình .net 4.0 và visual studio 2010 part 59
8 trang 21 0 0 -
Dạng bài tập về Micosoft.NET- P10
5 trang 21 0 0 -
Dạng bài tập về Micosoft.NET- P28
5 trang 21 0 0